Custom action bar does not fill parent.
I am using custom action bar for navigation drawer purpose.
But My action bar keep some space left at left side of action bar.
I used code like this.
// to inflate custom view
LayoutInflater inflator= (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=inflator.inflate(R.layout.custom_action_top_bar, null);
getActionBar().setHomeButtonEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
getActionBar().setCustomView(v);
View v1 = getActionBar().getCustomView();
LayoutParams lp = (LayoutParams) v1.getLayoutParams();
lp.width = LayoutParams.FILL_PARENT;
v1.setLayoutParams(lp);
this is action_custom_action_top_bar file for reference
this xml file contains main linear layout with left and right imageviews for opening two sided drawers respectively. but addition space get occured
on left side of the action bar can any one help me out?
this is custom action bar i have used in that some background colour
i have given to the action bar and two image for each side for drawers
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="1.0" >
<ImageView
android:id="#+id/IV_leftIcon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="#color/light_green"
android:src="#drawable/header_menu_btn" />
<TextView
android:id="#+id/actionText"
style="#style/action_bar_top_text_size"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".6"
android:background="#color/light_green"
android:drawableRight="#drawable/header_dropdown_arrow"
android:gravity="center"
android:text="Sample"
android:textColor="#color/white" />
<ImageView
android:id="#+id/IV_rightIcon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="#color/light_green"
android:src="#drawable/plus_btn" />
</LinearLayout>
how make action bar fill width
UPDATED ANSWER
remove android:layout_gravity="center" in root linear to be like this , make height wrap_content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0" >
......
</LinearLayout>
Prefer use getSupportActionBar() as default activity extends ActionBarActivity
import android.support.v7.app.ActionBar.LayoutParams;
import android.support.v7.widget.Toolbar;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
getSupportActionBar().setCustomView(v, layoutParams);
Toolbar parent = (Toolbar) v.getParent();
parent.setContentInsetsAbsolute(0, 0);
}
}
Summary
I understand why you have a problem , you using eclipse with old support library
that dosenot support ToolBar
use appcompat-v21 or later
my advice to you , use Android studio as they stop support eclipse
this code working 100% , prove of concept
Just add these 2 line code into your Toolbar xml code
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
It will solve your problem.
Here is my working code:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp">
<include layout="#layout/your_custom_toolbar_layout"/>
</android.support.v7.widget.Toolbar>
Related
I am working on Android project and I've created a new Settings Activity from Android. When I created the activity, and tried running, it the Settings Activity that Android Studio created, didn't include an action bar for some reason. I googled around and found that it seems to be a common thing and add the action bar manually, so I've done the following:
private void setupActionBar()
{
getLayoutInflater().inflate(R.layout.toolbar, (ViewGroup)findViewById(android.R.id.content));
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
I found that the first preference header is hidden under the action bar, again Googled around, found you need to add padding to the list view which I did using the following in the onCreate()
getListView().setPadding(0, 180, 0, 0);
Doing the above seems a little odd, and it only works on the initial settings activity screen with the list of preference headers. Once you click on the preference headers to view the settings, the first setting is hidden under the action bar as shown in the screenshot below:
I think I figured it out.
I created a toolbar XML as below
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/settings_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
Then in the SettingsActivity in the setupActionBar method as below:
private void setupActionBar()
{
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root);
View view = getLayoutInflater().inflate(R.layout.settings_toolbar, rootView, false);
rootView.addView(view, 0);
Toolbar toolbar = (Toolbar)findViewById(R.id.settings_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
I suggest to remove your Action Bar and make a custom toolbar which will be described by other layout file and will contain a widget that cancels your current activity.
For example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relLayout1">
<include layout="#layout/snippet_comments_toolbar"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relLayout2"
android:layout_below="#+id/relLayout1"
android:layout_marginBottom="60dp">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"></ListView>
</RelativeLayout>
and then snippet_comments_toolbar which is that toolbar I am talking about:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/white_grey_border_bottom"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/profileToolBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="20dp"
android:layout_centerVertical="true"
android:id="#+id/backArrow"
android:src="#drawable/ic_backarrow"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comments"
android:layout_toRightOf="#+id/backArrow"
android:layout_marginRight="15dp"
android:textSize="20sp"
android:textColor="#color/black"
android:layout_centerVertical="true"
android:id="#+id/tvNext"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</merge>
So began researching how to create an action bar with an autocomplete text view for search. Most of the solutions I've found come close to what I'm looking for however they all seem to be focused on cramming any and additional views added to the action bar on one line.
Further complicating this, is the fact that this is all located in a view pager fragment whose parent activity already has an action bar. Thus using the setSupportingActionBar call with a Toolbar throws an illegal state exception.
Not sure if what I am accomplishing is actually supported but here it goes.
The end result I am looking for is this
Here is what I wrote
*custom view containing the autocomplete text view named autocomplete.xml *
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/search_text_view"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:drawableLeft="#drawable/ic_search_blue"
android:background="#drawable/white_edittext"/>
</LinearLayout>
here is the code in my fragment used to setup the action bar
private void initializeActionsBarWithAutocomplete(){
ActionBar actionBar = ((MyParentActivity) getActivity()).getSupportActionBar();
actionBar.setTitle(getResources().getString(R.string.default_title));
View autoCompleteView = LayoutInflater.from(getContext()).inflate(R.layout.autocomplete, null);
mSearchTextView = (AutoCompleteTextView) autoCompleteView.findViewById(R.id.search_text_view);
mSearchTextView.setAdapter(mTypeAheadAdapter);
mSearchTextView.setOnItemClickListener(this);
mSearchTextView.addTextChangedListener(this);
actionBar.setCustomView(autoCompleteView);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
What I end up getting is this
So assuming what I'm trying to do is possible, and I'm at least on the right track, what exactly is going wrong here. I assumed that having the custom view match parent would force the textview to take up its own line and expand the size of the action bar accordingly. That is clearly not the case. I looked at some articles that allowed you to set the height of the action bar explicitly but from what I can comprehend, that method will result in the same appearance with a bunch of white space under views in the action bar.
You can use Toolbar widget in XML and customize it as per your needs.
Here is an layout of Toolbar containing AutoCompleteTextView:
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="#string/app_name"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold" />
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_centerInParent="true"
cardview:cardBackgroundColor="#ffffff"
cardview:cardCornerRadius="2dp"
cardview:cardElevation="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/search_text_view"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:drawableLeft="#drawable/ic_search_blue"
android:background="#drawable/white_edittext"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
In your JAVA code try this:
Toolbar mToolBar;
ActionBar mActionBar;
AutoCompleteTextView mSearchTextView;
private void initializeActionsBarWithAutocomplete(){
// ToolBar
mToolBar = (Toolbar) findViewById(R.id.toolbar);
// AutoCompleteTextView
mSearchTextView = (AutoCompleteTextView) mToolBar.findViewById(R.id.search_text_view);
mSearchTextView.setAdapter(mTypeAheadAdapter);
mSearchTextView.setOnItemClickListener(this);
mSearchTextView.addTextChangedListener(this);
setSupportActionBar(mToolBar);
// ActionBar
mActionBar = getSupportActionBar();
mActionBar.setTitle(getResources().getString(R.string.default_title));
mActionBar.setDisplayHomeAsUpEnabled(true);
}
OUTPUT:
Hope this will help~
I'm trying to display my toolbar title in the center and to do it I use the method which is given in this answer :-Toolbar Center title
However, when I enable back button in my activity by following code:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);
The title of toolbar doesn't show up in the center but slightly off-centered towards the right.
How can I achieve centered title without being affected by the back button or menu bar?
Add a TextView inside the Toolbar & don't forget to set the following attribute inside your TextView.
android:layout_marginRight="?android:attr/actionBarSize"
OR
android:layout_marginEnd="?android:attr/actionBarSize"
code snippet:
<android.support.v7.widget.Toolbar
android:id="#+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#android:color/holo_red_dark">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textColor="#android:color/white"
android:textSize="20sp"
android:layout_marginRight="?android:attr/actionBarSize"
android:gravity="center"/>
</android.support.v7.widget.Toolbar>
Refer to this tutorial for more information.
Having a placeholder image the same size as the back arrow and setting it to be invisible when the back arrow is not shown and gone when it's displayed did the trick for me.
<?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="wrap_content"
android:background="#color/blue"
android:minHeight="?attr/actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView
android:id="#+id/iv_placeholder"
android:layout_width="72dp"
android:layout_height="48dp"
android:src="#drawable/ic_actionbar_hamburger"
android:visibility="invisible"/>
<TextView
android:id="#+id/logo_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="?attr/actionBarSize"
android:gravity="center"
android:text=""
android:textColor="#color/white"
android:textStyle="normal"/>
</android.support.v7.widget.Toolbar>
Just add android:paddingEnd="72dp; to the Toolbar layout.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:contentScrim="#color/colorPrimary"
app:layout_collapseMode="pin"
android:paddingEnd="72dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark"
app:title="Title"/>
Just put your content in a child view inside the Toolbar tag in XML, using the following attributes:
android:layout_width="wrap_content"
android:layout_gravity="center"
Offical docs for Toolbar state:
One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's LayoutParams indicates a Gravity value of Gravity#CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
This works for me, using androidx.appcompat.widget.Toolbar with a child view.
The reason why the title is not centered when you use a back button as navigation icon, is that navigation icon is represented as AppCompatImageButton and is added to the same layout as your title TextView. Using Arshak's answer is not a bad idea, but ?android:attr/actionBarSize is not a good way to define the end margin. As the action bar height is probably the same size as icon's width, it might work, but might not work on all devices. Could be a good idea to specify this size from material design guidelines.
In my case I was using an imageview inside the toolbar which I didnt want shifting around while navigating between fragments of a activity.
I kept it centered by placing it out the toolbar. I used constraintlayouts
<androidx.constraintlayout.widget.ConstraintLayout
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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</com.google.android.material.appbar.MaterialToolbar>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="#id/toolbar"
app:layout_constraintBottom_toBottomOf="#id/toolbar"
android:src="#drawable/ic_logo"
tools:ignore="ContentDescription" />
...
</androidx.constraintlayout.widget.ConstraintLayout>
I think the nicest and most up-to-date method is to have full control over the appbar. This way you can change other things from textview location.
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="#string/app_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textSize="20dp"
android:textColor="?attr/colorPrimary"
android:layout_centerInParent="true"
android:layout_marginEnd="20dp"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
You can use this directly inside your activity. However, you may need to change the active toolbar by making such a definition in the activity where you added this toolbar.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Dont set propterties like this
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);
Do like this
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Title and subtitle
toolbar.setTitle(R.string.about_toolbar_title);
toolbar.setSubtitleTextColor(Color.WHITE);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setBackgroundColor(getResources().getColor(
R.color.themeToolbarColor));
toolbar.setNavigationIcon(R.drawable.ic_action_back);
toolbar.setNavigationOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish(); // to go back finish() will do your work.
//mActionBar.setDisplayHomeAsUpEnabled(true);
//mActionBar.setDisplayShowHomeEnabled(true);
}
});
I am attempting to show both a title, using setTitle and a custom view in my toolbar.
I am not treating it as an actionbar, instead as nothing more than a view.
I am adding both the titles and custom view in Java
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
if (title != null) {
toolbar.setTitle(title);
toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
}
if (subtitle != null) {
toolbar.setSubtitle(subtitle);
toolbar.setSubtitleTextColor(getResources().getColor(android.R.color.white));
}
// Add switch view to toolbar
View switchView = LayoutInflater.from(getActivity())
.inflate(R.layout.device_list_switch, null);
toolbar.addView(switchView);
The xml for my switch view is
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/discoverable"
android:layout_alignParentBottom="true"
android:layout_alignTop="#+id/discoverable"
android:layout_toLeftOf="#+id/discoverable"
android:layout_toStartOf="#+id/discoverable"
android:gravity="center"
android:text="#string/discoverable_switch_label"
android:textColor="#android:color/white"
/>
<Switch
android:id="#+id/discoverable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="#dimen/discoverable_switch_margin_left"
android:layout_marginStart="#dimen/discoverable_switch_margin_left"/>
</RelativeLayout>
What happens is that the RelativeLayout fills the entire toolbar area and the title isn't visible.
Any ideas?
If you are not using the Toolbar as an ActionBar, then maybe you could just use a layout.
I used a LinearLayout instead of a Toolbar.
What advantages is Toolbar giving you?
This can be solved by adding your own title (and subtitle, if you need it) text views inside the toolbar alongside your custom view.
But perhaps a better way is to use a second toolbar nested inside the first. That way, all formatting is taken care of for you.
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/topToolbar"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/titleToolbar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:contentInsetStart="0dp"
app:contentInsetLeft="0dp">
</android.support.v7.widget.Toolbar>
<CustomView... />
</LinearLayout>
</android.support.v7.widget.Toolbar>
Does anybody know how to easily implement an action bar with two stretched buttons?
Here is an example of the Google calendar app:
Thank you!
If you rather have this in the ActionBar for whatever reason, one way to achieve this is by using a custom view on the Action bar. Within you Custom View's layout then worry about setting up the buttons width.
Telling the activity to use a custom view for the action bar:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar ab = getActionBar();
ab.setDisplayShowHomeEnabled(false);
ab.setDisplayShowTitleEnabled(false);
final LayoutInflater inflater = (LayoutInflater)getSystemService("layout_inflater");
View view = inflater.inflate(R.layout.action_bar_edit_mode,null);
ab.setCustomView(view);
ab.setDisplayShowCustomEnabled(true);
}
layout/action_bar_edit_mode.xml can then look something like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="fill_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/action_bar_button_cancel"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="#+id/action_bar_button_ok"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Ok" />
</LinearLayout>
</RelativeLayout>
Hope it helps someone!
Note: I realize the ugly nesting layouts here and normally I wouldn't recommend this but for some reason the actionbar's own layout refuses to let the LinearLayout take up the entire width on its own. Normally you should avoid nesting layouts unnecessarily like this!
Perhaps if someone sees this they can point us to a better solution?
What it looks like:
EDIT: There is an excellent post by Roman Nurik where he explains a way to do this very nicely.
EDIT 2: If anyone is curious, the correct way to lay out the buttons so that they expand the width of the actionbar without the need to nest your layouts like I did above, is to set the custom view with the proper layout parameters that allows its component to match the parent group.
Essentially:
actionBar.setCustomView(view,new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
I know 2 ways to do this, but one doesn't stay on top.
Here is the 1st:
You need to override the method onCreateOptionsMenu, but this is add on the ActionBar, you need API 11 to do this and when you rotate the screen this buttons appear on ActionBar, this depends of the screen size.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
MenuItem add = menu.add(Menu.NONE, ADD_TIME, 0, R.string.add_time);
add.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
MenuItem qkAdd = menu.add(Menu.NONE, QUICK_ADD_TIME, 1, R.string.quick_add_time);
qkAdd.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
and this is the result:
if you're using a Fragment you need to set setHasOptionsMenu to true, otherwise the menu wont show.
Here is the 2nd:
cancel_done.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_bar"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:dividerPadding="12dp"
android:orientation="vertical"
android:showDividers="end" >
<Button
android:id="#+id/button1"
style="#drawable/btn_cab_done_holo_light"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/btn_cab_done_holo_light"
android:text="CANCEL"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:dividerPadding="12dp"
android:orientation="vertical"
android:showDividers="beginning" >
<Button
android:id="#+id/button2"
style="#drawable/btn_cab_done_holo_light"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/btn_cab_done_holo_light"
android:text="DONE"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
the resource style btn_cab_done_holo_light.xml you can find on ..\sdk\platforms\android-%%\data\res\drawable and then on your layout you just add:
<include
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="fill"
layout="#layout/cancel_done" />
and this is the result:
I don't now if is the best way, but it's working.
Make a horizontal LinearLayout with two buttons. Then set each of their widths to match_parent and android:layout_weight="0.5"
(Each button will then take up 50% of the space).
EDIT:
To apply as the ActionBar background:
(ActionBarSherlock) getSupportActionBar().setCustomView(R.layout.my_view);
(ActionBar) getActionBar().setCustomView(R.layout.my_view);
You can use the actionMode of the ActionBar to implement the Done and Cancel actions.
See http://developer.android.com/guide/topics/ui/menus.html#CAB
Is called done bar in android. have a look at this it will be helpfull
https://github.com/googlesamples/android-DoneBar