DayDayHappy
DayDayHappy

Reputation: 1679

Android: use the same background of toolbar in other activity

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.

Upvotes: 1

Views: 417

Answers (6)

Mr.Sandy
Mr.Sandy

Reputation: 4349

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.

Upvotes: 0

Rajesh
Rajesh

Reputation: 2618

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>

Upvotes: 1

Usman Ghauri
Usman Ghauri

Reputation: 941

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.

Upvotes: 0

RadekJ
RadekJ

Reputation: 3043

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

Upvotes: 1

Hiren Gondaliya
Hiren Gondaliya

Reputation: 156

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

Upvotes: 0

OBX
OBX

Reputation: 6114

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

Upvotes: 0

Related Questions