How to add an customized button on the actionbar?
Another quick and clean way to go about this is specifying your own layout for that menu button. something like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.MainActivity">
<item
android:id="@+id/action_custom_button"
android:title="Custom Button"
android:icon="@drawable/ic_custom_button"
app:actionLayout="@layout/layout_to_custom_button"
app:showAsAction="always" />
</menu>
there should be
layout_to_custom_button.xml
layout file which contains the padding and style you wanted.
and also do this in your activity for the click event
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_product_detail, menu);
final Menu mMenu = menu;
final MenuItem item = menu.findItem(R.id.action_custom_button);
item.getActionView().setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
mMenu.performIdentifierAction(item.getItemId(), 0);
}
});
return true;
}
Android custom action bar with action buttons
You can achieve this by setting a custom view in place of the action bar title.
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.search_layout);
actionBar.setDisplayShowCustomEnabled(true);
The result is an EditText
that fills the entire width of the action bar, except for the action buttons. It looks exactly like the first image in the question.
How can I implement custom Action Bar with custom buttons in Android?
This is pretty much as close as you'll get if you want to use the ActionBar
APIs. I'm not sure you can place a colorstrip above the ActionBar
without doing some weird Window
hacking, it's not worth the trouble. As far as changing the MenuItems
goes, you can make those tighter via a style. It would be something like this, but I haven't tested it.
<style name="MyTheme" parent="android:Theme.Holo.Light">
<item name="actionButtonStyle">@style/MyActionButtonStyle</item>
</style>
<style name="MyActionButtonStyle" parent="Widget.ActionButton">
<item name="android:minWidth">28dip</item>
</style>
Here's how to inflate and add the custom layout to your ActionBar
.
// Inflate your custom layout
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.action_bar,
null);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
// You customization
final int actionBarColor = getResources().getColor(R.color.action_bar);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
final Button actionBarTitle = (Button) findViewById(R.id.action_bar_title);
actionBarTitle.setText("Index(2)");
final Button actionBarSent = (Button) findViewById(R.id.action_bar_sent);
actionBarSent.setText("Sent");
final Button actionBarStaff = (Button) findViewById(R.id.action_bar_staff);
actionBarStaff.setText("Staff");
final Button actionBarLocations = (Button) findViewById(R.id.action_bar_locations);
actionBarLocations.setText("HIPPA Locations");
Here's the custom layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:orientation="horizontal"
android:paddingEnd="8dip" >
<Button
android:id="@+id/action_bar_title"
style="@style/ActionBarButtonWhite" />
<Button
android:id="@+id/action_bar_sent"
style="@style/ActionBarButtonOffWhite" />
<Button
android:id="@+id/action_bar_staff"
style="@style/ActionBarButtonOffWhite" />
<Button
android:id="@+id/action_bar_locations"
style="@style/ActionBarButtonOffWhite" />
</LinearLayout>
Here's the color strip layout: To use it, just use merge
in whatever layout you inflate in setContentView
.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/colorstrip"
android:background="@android:color/holo_blue_dark" />
Here are the Button
styles:
<style name="ActionBarButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@null</item>
<item name="android:ellipsize">end</item>
<item name="android:singleLine">true</item>
<item name="android:textSize">@dimen/text_size_small</item>
</style>
<style name="ActionBarButtonWhite" parent="@style/ActionBarButton">
<item name="android:textColor">@color/white</item>
</style>
<style name="ActionBarButtonOffWhite" parent="@style/ActionBarButton">
<item name="android:textColor">@color/off_white</item>
</style>
Here are the colors and dimensions I used:
<color name="action_bar">#ff0d0d0d</color>
<color name="white">#ffffffff</color>
<color name="off_white">#99ffffff</color>
<!-- Text sizes -->
<dimen name="text_size_small">14.0sp</dimen>
<dimen name="text_size_medium">16.0sp</dimen>
<!-- ActionBar color strip -->
<dimen name="colorstrip">5dp</dimen>
If you want to customize it more than this, you may consider not using the ActionBar
at all, but I wouldn't recommend that. You may also consider reading through the Android Design Guidelines to get a better idea on how to design your ActionBar.
If you choose to forgo the ActionBar
and use your own layout instead, you should be sure to add action-able Toasts
when users long press your "MenuItems". This can be easily achieved using this Gist.
Add custom actionbar in button dynamically in android
Instead ofsetCustomView(int)
use setCustomView(android.view.View)
Inflate your layout, keep reference, add new views whenever you want.
How to set a custom back button in ActionBar?
Use setHomeAsUpIndicator()
method instead
actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);
customize actionbar with tabs and buttons
Here's an awesome Tutorial about Android Material Tabs
. The tutorial explains about all basic modification that can be done on Tabs
. It also teaches to use CustomAdapter
to build our own Tab
style as you wanted.
How to get a custom toolbar aligned with the action bar?
In your activity_main.xml remove the extra toolbar element:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pm.pmactionbaricon.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:orientation="vertical">
<include layout="@layout/custom_toolbar" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
In custom_toolbar.xml layout make changes as shown below:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_collapseMode="pin"
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:padding="0dp"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="310dp"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:background="@drawable/distance_waiting_small"
android:layout_width="100dp"
android:layout_height="wrap_content"
style="?android:attr/buttonBarButtonStyle"
android:title="@string/Distance"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="on_distance_waiting_Click"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Also, make sure that your activity tag has this attribute set in the manifest file.
android:theme="@style/AppTheme.NoActionBar"
Related Topics
Inconsistent Performance Applying Foregroundactions in a Jeditorpane When Reading Html
Fast Bitmap Blur For Android Sdk
Java_Home and Path Are Set But Java -Version Still Shows the Old One
Why Use Getters and Setters/Accessors
"Implements Runnable" VS "Extends Thread" in Java
How to Resolve Classnotfoundexception
Setting the Default Java Character Encoding
How to Clone Arraylist and Also Clone Its Contents
Why Does Gson Fromjson Throw a Jsonsyntaxexception: Expected Begin_Object But Was Begin_Array
Override Back Button to Act Like Home Button
Set Imageview Width and Height Programmatically
How to Run a Command At Terminal from Java Program
Difference Between Public, Protected, Package-Private and Private in Java
Converting Iso 8601-Compliant String to Java.Util.Date
How to Determine Whether an Array Contains a Particular Value in Java
How to Read a Single Char from the Console in Java (As the User Types It)