Android custom action bar with action buttons - android

Here is the action bar that I want to create.
Here is items that I put in menu.
<item
android:id="#+id/search"
android:title="Search"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/search_layout"
/>
<item
android:id="#+id/search1"
android:title="PHOTO"
android:showAsAction="ifRoom"
android:actionLayout="#layout/search_layout"
/>
collapseActionView - collapses search menu on startup.
After click on search item it shows edit text like on picture 1. But I need always see it, not only after click.
If I set it to Always it will pop the menu items from the screen because of fill_parent.
<EditText
android:id="#+id/txt_search"
android:inputType="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Is it possible to get width of title bar layout without menu items? In this case I can set fixed width to edittext item.
Is it any other approaches to make it work?
In result it must be custom title layout + menu items. This custom layout must fill empty space between menu items and icon.

Using Custom ActionBar, you can solve your Problem. For Custom ActionBar you have to create on Custom Layout as per following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:paddingEnd="8dip"
android:paddingRight="8dip"
android:paddingLeft="8dip">
<EditText
android:layout_width="200dip"
android:layout_height="wrap_content"
android:id="#+id/action_bar_edt_search"/>
<ImageButton
android:layout_width="50dip"
android:layout_height="50dip"
android:id="#+id/action_bar_img_search"
android:background="#drawable/search_ico"/>
<Button
android:id="#+id/action_bar_search"
android:layout_alignParentRight="true"
android:layout_gravity="right"
style="#style/ActionBarButtonWhite" />
<Button
android:id="#+id/action_bar_photo"
android:layout_alignParentRight="true"
android:layout_gravity="right"
style="#style/ActionBarButtonOffWhite" />
<Button
android:id="#+id/action_bar_video"
android:layout_alignParentRight="true"
android:layout_gravity="right"
style="#style/ActionBarButtonOffWhite" />
<Button
android:id="#+id/action_bar_mobile"
android:layout_alignParentRight="true"
android:layout_gravity="right"
style="#style/ActionBarButtonOffWhite" />
color.xml
<resources>
<color name="action_bar">#ff0d0d0d</color>
<color name="white">#ffffffff</color>
<color name="off_white">#99ffffff</color>
</resources>
styles.xml
<style name="MyActionButtonStyle" parent="android:Widget.ActionButton">
<item name="android:minWidth">28dip</item>
</style>
<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>
now you need to create ViewGroup and ActionBar in your java file as per following:
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.actions,
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 EditText actionBarEditSearch = (EditText) findViewById(R.id.action_bar_edt_search);
actionBarEditSearch.setVisibility(View.GONE);
final ImageButton actionBarImgSearch = (ImageButton) findViewById(R.id.action_bar_img_search);
actionBarImgSearch.setVisibility(View.GONE);
final Button actionBarSearch = (Button) findViewById(R.id.action_bar_search);
actionBarSearch.setText("SEARCH");
final Button actionBarPhoto = (Button) findViewById(R.id.action_bar_photo);
actionBarPhoto.setText("PHOTO");
final Button actionBarVideo = (Button) findViewById(R.id.action_bar_video);
actionBarVideo.setText("VIDEO");
final Button actionBarMobile = (Button) findViewById(R.id.action_bar_mobile);
actionBarMobile.setText("MOBILE");
actionBarSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
actionBarEditSearch.setVisibility(View.VISIBLE);
actionBarImgSearch.setVisibility(View.VISIBLE);
actionBarSearch.setVisibility(View.GONE);
}
});
actionBarImgSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
actionBarEditSearch.setVisibility(View.GONE);
actionBarImgSearch.setVisibility(View.GONE);
actionBarSearch.setVisibility(View.VISIBLE);
}
});
<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>

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.

Related

How to show overflow menu only if text can be displayed on the ActionBar

How can I know (programmatically) if the menu is displayed with text on the ActionBar?
<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">
<item
android:id="#+id/action_cancel"
android:orderInCategory="100"
android:title="#string/action_cancel"
android:icon="#drawable/ic_cancel_24dp"
app:showAsAction="always|withText"/>
<item
android:id="#+id/action_clear"
android:orderInCategory="200"
android:title="#string/action_clear"
android:icon="#drawable/ic_clear_24dp"
app:showAsAction="always|withText"/>
<item
android:id="#+id/action_done"
android:orderInCategory="300"
android:title="#string/action_done"
android:icon="#drawable/ic_done_24dp"
app:showAsAction="always|withText"/>
</menu>
If the text is not displayed I want the menu to remain as menu
I tried also to replace always|withText with ifRoom|withText but in both cases, the Device I am using for debug shows only the icons, not the text.
I finally resourced to using a custom toolbar layout, where I put the items with icon + text.
<TextView
android:id="#+id/action_cancel"
style="#style/ActionBarItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_cancel"
android:text="#string/action_cancel"/>
<TextView
android:id="#+id/action_clear"
style="#style/ActionBarItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_clear"
android:text="#string/action_clear"/>
<TextView
android:id="#+id/action_done"
style="#style/ActionBarItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_done"
android:text="#string/action_done"/>
and
private void setupActionBar() {
MyLog.pe(DEBUG, TAG, "+ setupActionBar()");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
try {
assert actionBar != null;
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.actionbar_cancel_clear_done);
final View customActionBarView = actionBar.getCustomView();
customActionBarView.setLayoutParams(
new Toolbar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
customActionBarView.findViewById(R.id.action_cancel).setOnClickListener(this);
customActionBarView.findViewById(R.id.action_clear).setOnClickListener(this);
customActionBarView.findViewById(R.id.action_done).setOnClickListener(this);
} catch (Exception e) {
MyLog.e(DEBUG, TAG, "SupportActionBar is null!");
}
MyLog.px(DEBUG, TAG, "- setupActionBar()");
}

Android How to remove the second title in my actionbar?

i created an a custom toolbar to replace the default action bar of appcompat in android. but when i run the application i get two title, like in the picture
http://i.stack.imgur.com/wOPVE.png
i want only the title in the middle
here the code:
Main.java
public class MainMenu extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
Toolbar toolbar = (Toolbar)findViewById(R.id.mytoolbar);
setSupportActionBar(toolbar);
Typeface mistral = Typeface.createFromAsset(getAssets(), "MISTRAL.TTF");
TextView title = (TextView)findViewById(R.id.toolbar_title);
title.setText("Fyllo");
title.setTypeface(mistral);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id =item.getItemId();
if(id == R.id.action_settings){
Toast.makeText(getApplicationContext(),"this is a menu option",Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
MainMenu.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.adams.fyllo.MainMenu">
<android.support.v7.widget.Toolbar
android:id="#+id/mytoolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#00000000"
android:elevation="4dp"
android:title="Services">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_gravity="center"
android:text="Toolbar Title" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
And for style, i am using AppTheme
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#000000</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:titleTextStyle">#style/NoTitleText</item>
</style>
<style name="NoTitleText">
<item name="android:textSize">0sp</item>
<item name="android:textColor">#00000000</item>
</style>
</resources>
What can i do to remove this error
You have given titles two times either you want to remove upper one remove TextView which is inside Toolbar and setTitle of toolbar.
otherwise remove title from the Toolbar
You can remove title with help of
getSupportActionBar().setDisplayShowTitleEnabled(false);

Dynamic Spinner with icons inheriting AppCompat theme

I am trying to achieve two things:
Make my Spinners inherit from the AppCompat themes.
Add icons to the spinner elements, as is possible in the Toolbar popup menus.
Since I am not able to achieve the first point I am focusing on that, but I also want to add icons later on. As it is now, my Toolbar popup menus inherit the AppCompat theme, but the Spinners do not, as shown in the pictures below. The first image shows the (proper) popup menu from the Toolbar, while the second shows the popup menu from a Spinner. This is an example of a Spinner that is not inheriting the style. Or should this popup menu style be expected?
I have tried loads of things, so there are likely multiple duplicates of this question, but I am not able to make it work. What can be wrong in the code below? After the theme is inherited correctly, how can I add icons later on? Minimum SDK version is 16, target is 23.
themes.xml:
<resources>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/my_brown</item>
<item name="colorPrimaryDark">#color/my_dark_gray</item>
<item name="colorAccent">#color/my_green</item>
<!-- This is just a test, it makes no difference. -->
<item name="android:spinnerStyle">#style/MySpinnerStyle</item>
</style>
<style name="MyTheme" parent="MyTheme.Base"></style>
<!--
ActionBar style, applied directly to XML elements
-->
<style name="MyActionBarStyle" parent="#style/Widget.AppCompat.ActionBar">
<item name="theme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#color/my_brown</item>
<item name="android:minHeight">?attr/actionBarSize</item>
</style>
<!--
Spinner style, for testing. Also tried applied directly to xml Spinners.
-->
<style name="MySpinnerStyle" parent="#style/Widget.AppCompat.Spinner">
<item name="popupTheme">#style/ThemeOverlay.AppCompat.Light</item>
<item name="android:popupMenuStyle">#style/Widget.AppCompat.Light.PopupMenu</item>
</style>
</resources>
The Spinner is set up as simply as:
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, mCategories);
mSpinner.setAdapter(adapter);
where mSpinner is the inflated Spinner and mCategories is a String array. In XML the Spinner is defined as
<Spinner
android:id="#+id/my_spinner"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
I have tried adding various styles directly to the Spinner, but it does not work.
In my AndroidManifest.xml I have added the following to the Application tag:
android:theme="#style/MyTheme"
I managed to do it (or to be very close to it) by changing themes and styles like that:
Just add to your theme:
<item name="android:spinnerDropDownItemStyle">#style/MySpinnerItem</item>
Then create a style for MySpinnerItem that inherits from Widget.AppCompat.DropDownItem.Spinner:
<style name="MySpinnerItem" parent="#style/Widget.AppCompat.DropDownItem.Spinner">
<item name="android:textColor">#color/your_text_color</item>
<item name="android:textSize">16sp</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingStart" tools:targetApi="jelly_bean_mr1">16dp</item>
<item name="android:paddingRight">16dp</item>
<item name="android:paddingEnd" tools:targetApi="jelly_bean_mr1">16dp</item>
</style>
That's it for the AppCompat theme.
Finally, if you want to add icons to list items, you have to create a custom layout and set it programmatically. You can follow this tutorial http://android-er.blogspot.sg/2010/12/custom-arrayadapter-for-spinner-with.html that explains it.
Basically you have to:
create a custom layout
create a custom adapter that inherits from ArrayAdapter
implements getCutomView() methods to set your different images for each item
finally set your adapter to the spinner with MyCustomAdapter.createFromResource(this, R.array.my_data, R.layout.my_cutom_item_layout);
The cause of my problem was in the line
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, mCategories);
Here I am using android.R.layout.simple_spinner_item which is probably meant for the Spinner itself. Using android.R.layout.simple_spinner_dropdown_item instead gives the desired look for the dropdown items.
The difference between those are described in some detail in this question, though the images are for older Android versions.
For reference, here are the two layouts taken directly from the Android source code.
simple_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
simple_spinner_dropdown_item
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
Adding icons to the dropdown items
Now, for the icon part, I followed the steps from #euitam and ended up with the following:
MyAdapter:
public class MyAdapter extends ArrayAdapter<String>
{
private String[] mCategories;
private int[] mIcons;
public CategoryDropDownAdapter(Context context, int layoutResourceId, String[] categories)
{
super(context, layoutResourceId, categories);
mCategories = categories;
// Add the same icon to all items, just for testing.
mIcons = new int[mCategories.length];
for (int i = 0; i < mIcons.length; i++)
{
mIcons[i] = R.drawable.my_icon;
}
}
/**
* View for a dropdown item.
* */
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
rowView = inflater.inflate(R.layout.my_spinner_categories_dropdown_item, parent, false);
}
TextView categoryText = (TextView) rowView.findViewById(R.id.my_spinner_dropdown_item_text);
categoryText.setText(mCategories[position]);
ImageView icon = (ImageView) rowView.findViewById(R.id.my_spinner_dropdown_item_icon);
icon.setImageResource(mIcons[position]);
return rowView;
}
/**
* The Spinner View that is selected and shown in the *Spinner*, i.e. not the dropdown item.
* */
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View spinnerView = convertView;
if (spinnerView == null)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
spinnerView = inflater.inflate(R.layout.my_spinner_categories_spinner_item, parent, false);
}
TextView categoryText = (TextView) spinnerView.findViewById(R.id.my_spinner_item_text);
categoryText.setText(mCategories[position]);
return spinnerView;
}
}
my_spinner_categories_dropdown_item.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="horizontal" >
<ImageView
android:id="#+id/my_spinner_dropdown_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- Stolen from android.R.layout.simple_spinner_dropdown_item -->
<TextView
android:id="#+id/my_spinner_dropdown_item_text"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="marquee" />
</LinearLayout>
my_spinner_categories_spinner_item.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="horizontal" >
<!-- Stolen from android.R.layout.simple_spinner_dropdown_item -->
<TextView
android:id="#+id/my_spinner_item_text"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:ellipsize="marquee" />
</LinearLayout>
And finally, setting the adapter:
MyAdapter adapter = new MyAdapter(getContext(), android.R.layout.simple_spinner_dropdown_item, mCategories);
mSpinner.setAdapter(adapter);

how to set custom Action Bar properly in Android

I have created a custom action bar in my android app. Unfortunately the height and width is not set properly in my custom layout. After I run the app there remains a blank space left and right at the custom action bar as well as a heights problem. How can I solve this ?
Custom action bar
<?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:background="#3A86CF"
android:orientation="horizontal"
android:layout_gravity="fill_horizontal">
<ImageView
android:id="#+id/imgLeftMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical|center_horizontal"
android:layout_weight="1"
android:src="#drawable/menu_left" />
<TextView
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_weight="1"
android:id="#+id/txtTitle"
android:gravity="center_vertical|center_horizontal"
android:text="All Post"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/imgSearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#android:drawable/ic_menu_search" />
<ImageView
android:id="#+id/imgAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#android:drawable/ic_menu_add" />
</LinearLayout>
Activity code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_post);
CustomActionBar();
}
public void CustomActionBar()
{
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions( android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
// Do any other config to the action bar
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// set custom view
View actionBarView = getLayoutInflater().inflate(
R.layout.custom_action_bar, null);
View btnMenuLeft= actionBarView.findViewById(R.id.imgLeftMenu);
btnMenuLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
View textView_Title= actionBarView.findViewById(R.id.txtTitle);
View btnMenuShare= actionBarView.findViewById(R.id.imgSearch);
View btnMenuAdd= actionBarView.findViewById(R.id.imgAdd);
android.support.v7.app.ActionBar.LayoutParams params = new android.support.v7.app.ActionBar.LayoutParams(
android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT,android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT);
actionBar.setCustomView(actionBarView, params);
// Hide the home icon
actionBar.setIcon(android.R.color.transparent);
actionBar.setLogo(android.R.color.transparent);
}
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:height">5dp</item>
</style>
</resources>
put below code in your style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Customize your theme here. -->
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/actionbar_bg</item>
<item name="background">#color/actionbar_bg</item>
<item name="android:homeAsUpIndicator">#drawable/ic_backarrow</item>
<item name="homeAsUpIndicator">#drawable/ic_backarrow</item>
</style>
Use "#style/AppTheme" in manifest
Custom layout
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/cal_tvActionbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/white"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
Write below code in your java class for actionbar
/**
* Method to change ActionBar Title
*/
public void setMainScreenActionBar(String p_title)
{
LayoutInflater m_inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ActionBar m_actionBar = getSupportActionBar();
View m_viewActionBar = m_inflater.inflate(R.layout.custom_actionbar_title_layout, null);
ActionBar.LayoutParams m_params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
TextView m_tvTitle = (TextView) m_viewActionBar.findViewById(R.id.cal_tvActionbarTitle);
m_tvTitle.setText(p_title);
m_actionBar.setCustomView(m_viewActionBar, m_params);
m_actionBar.setDisplayShowCustomEnabled(true);
m_actionBar.setDisplayShowTitleEnabled(false);
m_actionBar.setDisplayHomeAsUpEnabled(true);
m_actionBar.setHomeButtonEnabled(true);
}
put below code in your style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">Light">
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Customize your theme here. -->
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:layout_height">5dp<background">#color/actionbar_bg</item>
<item name="background">#color/actionbar_bg</item>
<item name="android:homeAsUpIndicator">#drawable/ic_backarrow</item>
<item name="homeAsUpIndicator">#drawable/ic_backarrow</item>
</style>
If you donĀ“t fix Use "#style/AppTheme" in manifest
Custom layout
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/cal_tvActionbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/white"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
Write belove code in your problem with this post a screenshot please. java class for actionbar
/**
* Method to change ActionBar Title
*/
public void setMainScreenActionBar(String p_title)
{
LayoutInflater m_inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ActionBar m_actionBar = getSupportActionBar();
View m_viewActionBar = m_inflater.inflate(R.layout.custom_actionbar_title_layout, null);
ActionBar.LayoutParams m_params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
TextView m_tvTitle = (TextView) m_viewActionBar.findViewById(R.id.cal_tvActionbarTitle);
m_tvTitle.setText(p_title);
m_actionBar.setCustomView(m_viewActionBar, m_params);
m_actionBar.setDisplayShowCustomEnabled(true);
m_actionBar.setDisplayShowTitleEnabled(false);
m_actionBar.setDisplayHomeAsUpEnabled(true);
m_actionBar.setHomeButtonEnabled(true);
}

How to set two custom actionbar button in left and right in android?

I am Using Sherlock library and i also implement with myself. My problem is that i added two items in menu, now i want 1 item in left of actionbar and second in right of actionbar. How to to it?
You can create such action bar, but it's little more complicated than inflating a menu. Menu created in onCreateOptionsMenu() method will be always aligned to right, placed in split action bar or hidden under the menu key.
If you want your action bar to contain just two menu items - one on the left edge and the other on the right edge - you have to create custom view in the action bar.
The custom view layout will be something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:src="#drawable/ic_menu_item1"
android:background="?attr/actionBarItemBackground"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:id="#+id/item1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<ImageButton android:src="#drawable/ic_menu_item2"
android:background="?attr/actionBarItemBackground"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:id="#+id/item2"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
Define in the theme that you want to use custom view. The theme should contain:
<style name="MyTheme" parent="#style/Theme.Sherlock">
<item name="actionBarStyle">#style/MyActionBarStyle</item>
<item name="android:actionBarStyle">#style/MyActionBarStyle</item>
</style>
<style name="MyActionBarStyle" parent="#style/Widget.Sherlock.ActionBar">
<item name="displayOptions">showCustom</item>
<item name="android:displayOptions">showCustom</item>
</style>
Set the custom view in the activity (or fragment):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar ab = getSherlock().getActionBar();
LayoutInflater li = LayoutInflater.from(this);
View customView = li.inflate(R.layout.my_custom_view, null);
ab.setCustomView(customView);
ImageButton ibItem1 = (ImageButton) customView.findViewById(R.id.item1);
ibItem1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// ...
}
});
ImageButton ibItem2 = (ImageButton) customView.findViewById(R.id.item2);
ibItem2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// ...
}
});
}
You have to add the click listeners for the menu items. You cannot use onOptionsItemSelected() in this case.
Additionally to Tomik's answer. You need to add
ab.setDisplayShowCustomEnabled(true);
in onCreate method, otherwise custom view does not appear (at least for me).

Categories

Resources