I am using ActionbarSherlock library to implement action bar for Android 2.1 API 7 platform. The Sherlock library is imported to my own project successfully.
Then, in my project, I have res/menu/action_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/test1"
android:title="#string/test_1"
android:showAsAction="ifRoom"
/>
<item android:id="#+id/test2"
android:title="#string/test_2"
android:showAsAction="ifRoom"
/>
</menu>
My Activity:
import android.support.v4.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
public class MyActivity extends FragmentActivity {
#Override
protected void onStart() {
super.onStart();
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
}
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
}
With the above code, My application has an action bar. But the problem is that all the items defined in the above menu xml file are showing on the right hand side of the action bar, and the left side part of action bar is left blank.
You probably already noticed in my Activity code, I have:
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
The above two line codes are defined in my Activity onStart() callback which disable the action bar default text and icon.
But how can I have my custom items defined in menu xml file to be shown evenly on the action bar instead of only show on the right hand side of the action bar?
Related
I am trying to create an android application with custom actionbar.
my code is
package com.sample.actionbar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
//displaying custom ActionBar
View mActionBarView = getLayoutInflater().inflate(R.layout.action_custom, null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0C2640")));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
menu.findItem(R.id.action_settings).setVisible(false);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
But it shows margin on all sides.
I want it to fit with actionbar.
How can i remove this margin?
I tried many codes. but didnt gt what i want.
I had the same Problem. The solution for me was to add the following entries to my ActionBar style:
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
So the parts of my styles.xml related to it are resulting:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/Actionbar</item>
...
</style>
<style name="Actionbar" parent="Widget.AppCompat.Light.ActionBar">
...
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
</style>
</resources>
I have done this way:
ViewGroup actionBarLayout = (ViewGroup) contextAct.getLayoutInflater().inflate(R.layout.actionbar_customview,null);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(actionBarLayout, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
Hope this will help you.
The right way is to use Toolbar. Create it in your layout xml file like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<!-- Your custom action bar views -->
</android.support.v7.widget.Toolbar>
...
</LinearLayout>
Then in your activity just set the the Toolbar to be your action bar:
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
When i run the app overflow menu seen in emulator(i mean that three dots or lines on right top) , but while i was working, it doesn't seen on xml layout why ?? These are codes ;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class OverflowApp extends AppCompatActivity {
protected void onCreate(Bundle bambam){
super.onCreate(bambam);
setContentView(R.layout.overflow_xml);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main , menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
}
and xml codes ;
<?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">
Firstly, there is no such thing as a layout for action overflow menu. The content that is to be displayed is stored in the following form.
<menu ...>
<item android:id="..." android:title="..."/>
<item android:id="..." android:title="..."/>
<item android:id="..." android:title="..."/>
...
</menu>
This file can be accessed from the menu directory in the res folder.
You can add, delete or change the menu items but not how they look from here.
As far as the appearance is concerned, you can change the appearance only in terms of the colors using styles. You can start with the answer given here.
I am stuck on a part in my app. My app's minimum sdk version is v2.3. I am working on action bar. When I click on search icon on the action bar, it expands, but not fully. Two icons on the right side are still visible. And when I click on the home up button,it closes the SearchView, but doesn't call onOptionsItemSelected method.
Note : Home up button calls the onOptionsItemSelected method when clicked at first(when searchview is not expanded), but does not call onOptionsItemSelected when searchview is expanded.
I am using support library as I am working on very old version. Please help me.
My code is as follows:
menu_main.xml
<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=".MainActivity">
<item
android:id="#+id/search_action"
app:showAsAction="always|collapseActionView"
android:title="#string/search_action"
android:icon="#drawable/search_icon"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
<item
android:id="#+id/shopping_cart"
app:actionLayout="#layout/badge_layout"
android:title="#string/shopping_cart"
app:showAsAction="always"
>
</item>
<item
android:id="#+id/user_menu"
android:icon="#drawable/overflow_icon"
app:showAsAction="always"
android:title="user">
</item>
</menu>
Styles.xml
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<!--<item name="android:background" tools:ignore="NewApi">#color/action_bar_color</item>-->
<item name="background">#color/action_bar_color</item>
<item name="logo">#drawable/nt_logo</item>
<item name="displayOptions">useLogo|showHome</item>
</style>
MainActivity.java
package com.example.stickyheader.stickyheader;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.search_action);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint("Search here");
searchView.setIconifiedByDefault(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
I have sorted this issue in another way. I created a custom SearchView and attached two listeners i.e expandListener and collapseListener. On expand, I made all the menu items invisible and vice versa.
I am trying to add a button to my action bar in android
Below is my code in the menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_compose"
android:icon="#drawable/help"
android:title="help"/>
</menu>
and I added this to my Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
But when I run the app the Actionbar is still the same, I'm not sure if it matters but I have a Navigation drawer in my app to.
use actionbar sherlock library and use below code.
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class HomeActivity extends SherlockFragmentActivity implements TabListener
{
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.issue240);
actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(YOUR_BUTTON_LAYOUT);
}
}
At my new application that I created, I got auto generated code for creating menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
And I added item at menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/item1"></item>
</menu>
But there is no menu button, am I missing something?
EDIT:
In your menu definition you put:
android:showAsAction="never"
Change it to:
android:showAsAction="always"
Check this example, including an icon for the menu:
<item
android:id="#+id/menu_calendar"
android:title="#string/calendar"
android:icon="#drawable/ic_menu_calendar_holo_light"
android:showAsAction="always|withText" />
always means the button will always be displayed. You can replace it by ifRoom if it is a menu such as Settings that should appear in the menu as an option but not displayed all the time.
withtext means that the title of the menu will be displayed beside the icon if there is enough place for it.
Details about all these options are available here.
For the rest, you need to create and show the ActionBar in your onCreate() function:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//you might need this line if you are not using the Holo theme
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main_activity);
ActionBar actionBar = getActionBar();
actionBar.show();
(...)
Check for the import you are using. Check example below
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
You should use Menu and MenuInflater of actionbarsherlock..
Check out this link on implemention of ActionBarSherlock
Try
<item
android:id="#+id/action_settings"
android:showAsAction="always"
android:title="#string/action_settings"/>
<item android:id="#+id/item1"></item>