i want to change only one menu item in the action bar. I tried to change the same icon color instead of making it transparent into the desired color but the edges show the background of the action bar.
Here is my action bar what it looks like:
https://www.dropbox.com/s/1sjnk975dnj17kr/before.png?dl=0
And here is the goal i want to reach:
https://www.dropbox.com/s/ul7hh2gxtfox1pk/goal.png?dl=0
Here are my xml files:
menu_main.xml
<item
android:id= "@+id/location"
android:icon="@drawable/ic_room_white_48dp"
android:title=""
android:orderInCategory="2"
app:showAsAction="always"/>
<item
android:id= "@+id/report"
android:icon="@drawable/report"
android:title=""
android:orderInCategory="3"
app:showAsAction="always"/>
<item
android:id= "@+id/message"
android:icon="@drawable/ic_comment_white_48dp"
android:title=""
android:orderInCategory="4"
app:showAsAction="always"/>
styles.xml
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#B9ACC1</item>
<item name="colorPrimaryDark">#827689</item>
<item name="colorAccent">#827689</item>
<item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
</style>
<style name="MyActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">
<item name="android:padding">15dip</item>
</style>
UPDATE1 : Adding a way to avoid xml and code duplication.
UPDATE2 : Adding static listener.
I don't know if you can achieve your goal just by changing your menu.xml but the Toolbar is a viewGroup so you can achieve your goal with a view.xml which should looks like it :
toolbar.xml :
<LinearLayout
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:layout_marginRight="8dp"
android:background="#000000">
<Button
android:id="@+id/button_first"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:background="@android:drawable/ic_lock_idle_lock" />
</LinearLayout>
<LinearLayout
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:layout_marginRight="8dp"
android:background="#000000">
<Button
android:id="@+id/button_second"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:background="@android:drawable/ic_lock_idle_lock" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
It can be boring to add this code on all of your xml so i advice you to to use thé <include />
attribute like this :
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
</RelativeLayout>
Then, you just have to set the Toolbar and use the Button
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button buttonFirst = (Button) findViewById(R.id.button_first);
final Button buttonSecond = (Button) findViewById(R.id.button_second);
buttonFirst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
buttonSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
If all of your button have the same listener, you can make them static into avoid the code duplication :
IconListener.java :
public class IconListener {
public static View.OnClickListener listenerButtonOne = new View.OnClickListener() {
@Override
public void onClick(View v) {
//DO what you want
}
};
public static View.OnClickListener listenerButtonTwo = new View.OnClickListener() {
@Override
public void onClick(View v) {
//Do what you want
}
};
}
So your MainActivity.java became
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button buttonFirst = (Button) findViewById(R.id.button_first);
final Button buttonSecond = (Button) findViewById(R.id.button_second);
buttonFirst.setOnClickListener(IconListener.listenerButtonOne)
buttonSecond.setOnClickListener(IconListener.listenerButtonTwo);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments