user2350138
user2350138

Reputation: 525

Unable to set the Primary color to the Toolbar in Android Material Design

I just started working with Material Design.I am facing issue in setting primary color to Toolbar and fitting the width of tool bar to screensize. Here is my Code

activity_main.xml

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />

<TextView
    android:layout_below="@id/app_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

app_bar.xml

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">

styles.xml

<resources>

<!-- Base application theme -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>
<!-- Application theme for < v22-->
<style name="AppTheme" parent="AppTheme.Base">
    <item name="colorPrimary">@color/primary_color</item>
    <item name="colorPrimaryDark">@color/primary_colordark</item>
</style>

styles-v22.xml

<resources>

<!-- Application theme For v22 -->
<style name="AppTheme" parent="AppTheme.Base">
    <item name="android:colorPrimary">@color/primary_color</item>
    <item name="android:colorPrimaryDark">@color/primary_colordark</item>
</style>

colors.xml

<?xml version="1.0" encoding="utf-8"?><resources>
<color name="primary_color">#FFC107</color>
<color name="primary_colordark">#FFA000</color>

MainActivity

public class MainActivity extends AppCompatActivity {

Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar)findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

}

output: enter image description here The emulator is 5.1 version. The min sdk is 4.0 for the project. When i tried setting some color to the toolbar background , i see the Toolbar not fitting to screenwidth and primary color didn't got applied. Tried keeping Toolbar in LinearLayout but invain.

Kindly help me in resolving this.

I am hardly seeing any examples with material design. If anyone is familiar with some links kindly provide it.(Seen Androidhive and in youtube slidenr).

Thank you.

Upvotes: 1

Views: 594

Answers (1)

dieter_h
dieter_h

Reputation: 2727

Try app_bar.xml:

<?xml version="1.0" encoding="utf-8"?
   <android.support.v7.widget.Toolbar
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/primary_color">

Upvotes: 2

Related Questions