Theo Theo - 5 months ago 21
Android Question

Changing Toolbar text color

I am trying to change the text color of the toolbar,but at no vail. Here is my code.

styles.xml


<!-- Base application theme. -->
<style name="BasicTheme" parent="Theme.AppCompat.Light.NoActionBar">

</style>
<style name="ToolbarTheme" parent="BasicTheme">
<item name="android:textColor">@color/text_color</item>
</style>




tool_lay

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:appo="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:elevation="10dp"
android:background="@color/colorPrimary"
appo:theme="@style/ToolbarTheme"
>




activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="testing.theo.aelfootballclub.MainActivity"
android:background="#3d827e">
<include

android:id="@+id/toolbar"
layout="@layout/tool_lay"

/>
<LinearLayout
android:padding="20dp"
android:background="#6e7398"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">

<EditText
android:id="@+id/user_name"
android:hint="Userame"
android:layout_width="250dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/user_pass"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginTop="20dp"
android:hint="Κωδικός"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:layout_gravity="center_horizontal"
android:text="Εισοδος"
android:layout_marginTop="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="userLogin"/>

<Button
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:text="Εγγραφη"
android:layout_width="0dp"
android:onClick="userReg"
android:layout_weight="1"
android:layout_height="wrap_content" />

</LinearLayout>

<TextView
android:textStyle="bold|italic"
android:textSize="25sp"
android:layout_gravity="center_horizontal"
android:text="Ξεχασες τον κωδικό?"
android:id="@+id/forgotPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>




and finally my MainActivity.

public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setToolbar();
}
private void setToolbar(){
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

}


The background color of the toolbar works fine. I can change it to any color I want to.

Any ideas,

Thanks.

Answer

You can do like this

private void setToolbar(){
    toolbar = (Toolbar)findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(ContextCompat.getColor(getApplicationContext(),R.color.yellow));
setSupportActionBar(toolbar);
  }