Menu dose not show on Action bar android - android-actionbar

I am using Holo.theme
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"
My menu file is
<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/action_settings" android:title="#string/action_settings"
android:icon="#drawable/ic_action_search"
android:orderInCategory="200" app:showAsAction="always" />
<item android:id="#+id/action_compose" android:title="#string/action_settings1"
android:icon="#drawable/ic_action_add_person"
android:orderInCategory="100" app:showAsAction="always" />
My main_activity_java file is
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.show();
}
#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_activity_actions, menu);
//return true;
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#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.
return super.onOptionsItemSelected(item);
}
}
Menu is showing on clicking hardware button of phone instead of on Action Bar. Please help me. I want to show these on action bar

If you want to keep Holo theme then use following answer.
First of all remove Appcompat lib (If it exists) from your project depending on the IDE you are using.
Change TargetSDK and compile SDK to 20.
In your menu.xml do following changes.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/ic_action_search"
android:orderInCategory="200"
android:showAsAction="always" />
<item android:id="#+id/action_compose"
android:title="#string/action_settings1"
android:icon="#drawable/ic_action_add_person"
android:orderInCategory="100"
android:showAsAction="always" />
I hope this can help.

Related

App crash with actionbar

i'm new on Android. I'm trying to implent an actionbar im my activity. Everytime i run that, the app crash.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_percorsodisabili);
//Option Bar
getSupportActionBar().setTitle("Percorso Bici");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Resources res = getResources();
myVist3 = (ListView) findViewById(R.id.myVist3);
items = res.getStringArray(R.array.nomi_percorsi3);
descriptions = res.getStringArray(R.array.desc_percorsi3);
content = res.getStringArray(R.array.desc_percorsi3);
ItemAdapter itemAdapter = new ItemAdapter(percorsodisabili.this, items,foto,descriptions,content);
myVist3.setAdapter(itemAdapter);
}
}
Add the toolbar to your activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="56dp" />
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
<!--YOUR LAYOUT CODE-->
</RelativeLayout>
Then initialize java code for activity:
Use android.support.v7.widget.Toolbar in case you are using AppCompactActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

How to set action bar icons in action bar

I am new to android, I want to display actionbar icons in actionbar. Simple task, but I am not able to get this. I have uploaded my Mainactivity.java file.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_icons, menu);
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is my Custom Menu.XML file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_favorite_border_white_48dp"
android:title="action_search"
android:showAsAction="ifRoom"/>
<!-- Location Found -->
<item android:id="#+id/action_location_found"
android:icon="#drawable/ic_star_rate_white_18dp"
android:title="action_location_found"
android:showAsAction="ifRoom" />
</menu>
I have also added my Mainfest file for reference.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.actionbaricons">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And I am running the application in my Android Mobile (Nexus 5 Android 6.0.0)
Thanks in adavance.
In your main activity you can set actionbar icon
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.ic_button); /* setting icon */
}

How to add action bar to a DialogFragment?

How to add action bar to a DialogFragment? I found a way to style an activity to the way we require and then display it as a dialog as in the following link ActionBar in a DialogFragment I need to set an action bar on a proper DialogFragment. Is it possible?
As Up ActionBar action on DialogFragment
indicates: There is no way to attach an ActionBar to the DialogFragment even though you can set the theme of the DialogFragment it will not register as a ActionBar to it, Dialog.getActionBar() will always return null.
But there's always the cases that I really want to use DialogFragment(which contain ActionBar like style) instead of Activity. Just add a Layout that will look like an ActionBar into the DialogFragment's Layout
the following is the steps:
1) DialogFragment layout: about_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white" >
<include android:id="#+id/fake_action_bar"
layout="#layout/fake_action_bar_with_backbotton" />
2) Implement a ActionBar like layout: fake_action_bar_with_backbotton.xml
<?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/fake_action_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="#drawable/ic_menu_back"
android:background="#color/background_material_dark"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
Note: the #drawable/ic_menu_back is copied from sdk\platforms\android-21\data\res\drawable-hdpi
3) Update the DialogFragment code
public class AboutDialogFragment extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// use Theme_Holo_Light for full screen
// use Theme_Holo_Dialog for not full screen
// first parameter: DialogFragment.STYLE_NO_TITLE no title
setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Holo_Light_DarkActionBar);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.about_dialog, container, false);
// set the listener for Navigation
Toolbar actionBar = (Toolbar) v.findViewById(R.id.fake_action_bar);
if (actionBar!=null) {
final AboutDialogFragment window = this;
actionBar.setTitle(R.string.about_title);
actionBar.setNavigationOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
window.dismiss();
}
});
}
return v;
}
}
Wish it can help!

Remove menu in FragmentActivity when Add Fragment with new Menu

I have FragmentActivity with option menu A in ActionBar, in this Activity, I added one Fragment and ActionBar change to new option menu B. But after add this fragment, both menu A and B show in ActionBar. How to remove menu A when Fragment is visible?
I have the answer right now.
Ex in FragmentActivity I have
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_page_menu, menu);
menu.setGroupVisible(R.id.menu_group_main, true); // this line to show menu of Activity
return super.onCreateOptionsMenu(menu);
}
With
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="#+id/menu_group_main" >
<item
android:id="#+id/item_grid"
android:icon="#drawable/icon_grid"
android:showAsAction="always"
android:title="#string/action_grid"/>
</group>
And Fragment I will set
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_grid_thumb, menu);
menu.setGroupVisible(R.id.menu_grid_thumb, true);//this line to show menu of Fragment
menu.setGroupVisible(R.id.menu_group_main, false);// this line to hide menu of Activity
super.onCreateOptionsMenu(menu, inflater);
}
With
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="#+id/menu_grid_thumb" >
<item
android:id="#+id/item_frag_grid"
android:icon="#drawable/icon_grid"
android:showAsAction="always"
android:title="#string/action_grid"/>
</group>
It runs very well

Adding spinner to ActionBar in Fragment(Not Navigation Listener)

I want to display spinner in action bar(not in activity,in Fragment).for that i did below things
Step-1 (spinnermenu.xml)
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menuSort"
android:showAsAction="ifRoom"
android:actionLayout="#layout/spinner"/>
</menu>`
Step-2 (spinner.xml)
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="70dp"
android:layout_height="wrap_content" />
step-3 (Code)
public class All extends Fragment{
ArrayList<String> spinnerlist;
ArrayAdapter<String> spinneradapter;
#Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.spinnermenu, menu);
spinnerlist = new ArrayList<String>();
spinnerlist.add("Items1");
spinnerlist.add("Items2");
spinneradapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerlist);
Spinner s = (Spinner) menu.findItem(R.id.menuSort).getActionView();
s.setAdapter(spinneradapter);
super.onCreateOptionsMenu(menu, inflater);
}
}
I am getting error in this line
setHasOptionsMenu(true);
and Null exception in this line
s.setAdapter(spinneradapter);
Could any one tell me how to rectify this error?
*PS : Is there any way to do the same?*