Adding action bar and options menu to activity - android

I am using this sample for my app. This sample doesn't have an action bar.
This is the layout of activity in which I want to add an action bar with options menu:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_gravity="bottom"
android:background="#000"
tools:context="com.example.android.camera2basic.CameraActivity" />
</LinearLayout>
So, LinearLayout and a FrameLayout inside of it as a container for fragment. In Main Activity I have implemented onCreateOptionsMenu and added setSupportActionBar((Toolbar)findViewById(R.id.my_toolbar)); but actionbar with the menu isn't appearing.
How can I add an ActionBar with menus to this activity?

Create a main_menu.xml in your menu folder
<item
android:id="#+id/menu1"
android:title="Option 1" />
<item
android:id="#+id/menu2"
android:title="Optiion 2" />
Add this in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu1:
Toast.makeText(this, "Clicked Menu 1", Toast.LENGTH_SHORT).show();
break;
case R.id.menu2:
Toast.makeText(this, "Clicked Menu 2", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}

Set your Activity Theme to NoActionBar and add this 2 lines of Code in your Activity onCreate method.
Add ToolBar in your Layout.
ToolBar toolbar = findViewById ();
setSupportActionBar(toolbar);

Related

Android, adding a menu to an activity

I want to add a menu to my main activity. When I start my application, all shows up correctly but the menu. What I'm doing wrong?
This is my activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
This is part of the MainActivity.java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu1:
Toast.makeText(this, "Clicked Menu 1", Toast.LENGTH_SHORT).show();
break;
case R.id.menu2:
Toast.makeText(this, "Clicked Menu 2", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
This is the styles.xml
<resources>
<style name="AppTheme" parent="android:Theme.DeviceDefault.Light">
<item name="android:statusBarColor">#android:color/black</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
And for last, this is the menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu1"
android:title="Option 1" />
<item
android:id="#+id/menu2"
android:title="Optiion 2" />
</menu>
Is your activity extending from AppCompatActivity?
Extending from AppCompatActivity allows you to set the toolbar using the method setSupportActionBar check this first.
If not, the 'easy' way would be to use a theme that by default provides you an AppBar (ending with .DarkActionBar for example)
You need to create a toolbar in your activity_main.xml and in your MainActivity.java in the onCreate add this:
setSupportActionBar(yourToolbarId);

How to hide action Overflow menu (Not menu item) from activity and keep in Fragment? [Not solved]

I have a requirement that action Overflow menu icon should show in fragment, but not activity.
Code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.filter:
openFilterDialog();
return true;
default:
break;
}
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_mail_list, menu);
return true;
}
menu 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="com.dwisehealthcare.pmstablet.consult.activity.ConsultActivity">
<item
android:id="#+id/filter"
android:title="Filter"
android:icon="#drawable/ic_baseline_filter_list_24"
app:showAsAction="always" />
</menu>
I searched, but everyone is talking about menu item, not action icon.
Try this link may be it will help you, with the help of this you can add menu anywhere you want.Just add an ImageView to that fragment and when you click that imageView you create a PopUpMenu

Overflow menu icon not visible...

I am adding the toolbar to linear layout I have created a toolbar without using xml.Everything is working fine but i am not able to add overflow menu .Overflow icon is not showing
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_lnrLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.smartify.customizetoolbardemo.MainActivity"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lnrlayout_toolbar"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Add a menu to your toolbar
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Settings"
android:id="#+id/action_settings"
/>
</menu>
inflate you menu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}
You will get icon of overflow menu then.
Add one button or any view in yourlnrlayout_toolbar
Button btn=(Button)findViewById(R.id.btn_menu);
showPopupMenu(btn);
and call below method for showing overlay menu
public void showPopupMenu(View v){
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.sub_menu, popup.getMenu());
popup.show();
}
is sub_menu is your menu file that you want to show..
Try this
XML code for menu items
create menu resource in res>>menu>>menu_optipn.xml
<item android:id="#+id/new_game"
android:icon="#mipmap/ic_launcher"
android:title="item1"
/>
<item android:id="#+id/help"
android:icon="#mipmap/ic_launcher"
android:title="item2"
android:orderInCategory="0"
/>
MainActivity.Java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_option, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
Toast.makeText(this, "Item 1 is selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.help:
Toast.makeText(this, "Item 2 is selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Android Toolbar Items not showing?

I'm trying to create a Toolbar with the android.support.v7.widget.toolbar but when i try t add an item, it will not show on the toolbar:
Toolbar in activity_main.xml:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
main_activity:
on the onCreate:
Toolbar my_tbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(my_tbar);
out of onCreate:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
res/menu/main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="RefreshButton"
android:id="#+id/refresh"
android:icon="#drawable/refresh_icon"
app:showAsAction="always"
/>
</menu>
the refresh_icon is created by me because in the #drawable/i didn't found the ic_menu_refresh
Why the button is not shown?
Thank you
It is not showing because your not infalting the menu layout. Before calling onOptionsItemSelected() you need to inflate the layout like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
just add this method in your activity, and it will work fine.

How to make click event for custom action bar in android?

I need to enable click event for custom action bar menu.In my app action bar(am used AppCompat Library) i created custom menus.So i need to click this menu for call another activity.
If i used normal menu resource in action bar like below i know to how enable the click event.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
</menu>
For above we can use onOptionsItemSelected() to acheive the solution like below
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But my scenario am using Custom Menu.For this code i need to make click event.
My Codes:
// menu.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:someNamespace="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/badge"
someNamespace:showAsAction="always"
android:title="#string/msg_noty"
android:icon="#drawable/msg"
/>
</menu>
// feed_update_count.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_gravity="right" >
<ImageView
android:id="#+id/flagImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageView1"
android:layout_marginRight="30dp"
android:paddingRight="0dp"
android:src="#drawable/flag"
android:contentDescription="#string/flag_noty"
android:clickable="true"
/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_marginTop="18dp"
android:layout_toLeftOf="#+id/flagImageView"
android:src="#drawable/msg"
android:contentDescription="#string/msg_noty"
/>
</RelativeLayout>
//Java Code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.gk_page, menu);
MenuItem item = menu.findItem(R.id.badge);
View count = menu.findItem(R.id.badge).getActionView();
MenuItemCompat.setActionView(item, R.layout.feed_update_count);
View view = MenuItemCompat.getActionView(item);
return super.onCreateOptionsMenu(menu);
}
Please help me to solve solution.Thanks
Note : Am using Appcompat library (android-support-v7-appcompat)

Categories

Resources