Reputation: 823
I have two activities, AddActivty
and ViewActivity
. These two activities share a layout view defined in the file layout_form.xml
.
First of all, my app requires to work a minimum sdk version 16. So in the build.gradle
file I specified that.
These is a snipped of the layout_form.xml
. Remaining code is not important.
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="orizontal">
<Button
android:id="@+id/leftToolbarButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_done"
android:drawableStart="@drawable/ic_done"
android:text="ADD"
android:textColor="@android:color/white" />
<Button
android:id="@+id/rightToolbarButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/ic_cancel"
android:drawableRight="@drawable/ic_cancel"
android:text="CANCEL"
android:textColor="@android:color/white" />
</LinearLayout>
</Toolbar>
In both activities I set this layout as the content view by the line setContentView(R.layout.layout_form);
This is how my app toolbar looks when AddActivity
is on screen.
and so far, so good.
Problems arise when I start ViewActivity
.
This is the code of in its onCreate()
method.
Button backButton=(Button)findViewById(R.id.leftToolbarButton);
backButton.setText("BACK");
//PROBLEM HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
backButton.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_back, 0, 0, 0);
Button editButton=(Button)findViewById(R.id.rightToolbarButton);
editButton.setText("EDIT");
//AND PROBLEM HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
editButton.setCompoundDrawablesWithIntrinsicBounds(
0, 0, R.drawable.ic_edit, 0);
Now, if I run my app on android >=21 all is ok. This is how toolbar should be.
Instead, on android 16 it looks in this way.
As you can see, if my app is running on Android 16 (I don't know what happens on Android 17 or above, but lower than 21) Icon are not changed. They still look the same of those set in xml layout file.
Another thing. I add this listener to the editButton
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
backButton.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_back, 0, 0, 0);
}
});
If I click on it, now icon changes.
So, why in the onCreate()
method of ViewActivity
don't icons change?
Upvotes: 1
Views: 533
Reputation: 823
Solved! The problem are
android:drawableStart="@drawable/ic_done"
in leftToolbarButton
, and
android:drawableEnd="@drawable/ic_cancel"
in rightToolbarButton
.
I moved out these two lines from layout_form.xml. I added them because Android Studio suggested me to add them in order to better support left-to-right layout.
Upvotes: 1