1

I am using AppCompatActivity with following

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/red"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:navigationIcon="@mipmap/app_icon"
    />

</android.support.design.widget.AppBarLayout>

Let's say the background is set to red, it is fine in the main activity, but my other activity restore to default blue when open. what did I miss?

edit: I am not saying to set the background of the activity, but only the toolbar.

Cœur
  • 37,241
  • 25
  • 195
  • 267
DayDayHappy
  • 1,679
  • 1
  • 15
  • 26
  • bit unclear to me. *Let's say the background is set to red, it is fine in the main activity, but my other activity restore to default blue when open* can you clear this a bit so i can answer ^_^ Do you want to change the background color of tool bar ? across all activities ? or for each one? – Charuක Feb 04 '17 at 08:11

6 Answers6

1

If you want to set background for all activities in one place, this answer will be helpful: https://stackoverflow.com/a/37799081/2318843

Community
  • 1
  • 1
RadekJ
  • 2,835
  • 1
  • 19
  • 25
  • thank you. but i am not saying to set the background of the activity, but only the toolbar. – DayDayHappy Feb 04 '17 at 07:49
  • 2
    @DayDayHappy but in your question title you say same background in all activity. that is misleading a bit. still you can do that as well just add your custom toolbar style in styles file and add that theme to your toolbar in every activity – Usman Ghauri Feb 04 '17 at 07:52
1

You can add this <item name="android:background">@drawable/background</item> in app style (<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">), if you want background every where in app.

Ok For toolbar create separate xml layout like this

<?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"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@drawable/backgroundImage"//<--Add your background here
    android:elevation="1dp"
    app:layout_collapseMode="pin">

    <com.trptic.driver.widgets.TTextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxLines="1"
        android:textColor="@color/black"
        android:textSize="@dimen/_14sdp" />
</android.support.v7.widget.Toolbar>

Now include it wherever you want it! like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:font="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <include layout="@layout/toolbar" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        ....
    </FrameLayout>
</LinearLayout>
Rajesh
  • 2,618
  • 19
  • 25
0

Do set the : android:background="@drawable/red" in other Activity as well.

OBX
  • 6,044
  • 7
  • 33
  • 77
0

Put this line in all activity : android:background="@drawable/red"

0

if you just want to use one background for all of your activities then just create a tag of background in your app theme and since you'll apply that theme to every activity it will automatically get added there and you wont need to add one more line and if in case you want to change background for just one theme open up xml of that activity and add background tag there it will add that background to that specific activity.

Usman Ghauri
  • 931
  • 1
  • 7
  • 26
0

Need to make some change in your xml file.. You have to put below line in your Toolbar control.

android:background="@drawable/red"

Every where you use Toolbar Control like this.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:navigationIcon="@mipmap/app_icon"
/>

In every xml files to change background of that control.

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54