I want to add some icon in the drop-down menu of a popupMenu i created. please help me. I am so much bothered on how to create popupMenu to show dropdown menu items with icon when clicked. I have tried a lot of things which did not work. I tried to set showAsAction to always|withtext, but it didn't work.
Please help me. Any help will be much appreciated!
Below is the Java
showMenuButton = findViewById(R.id.btn_long_press);
//Init popup menu
final PopupMenu popupMenu = new PopupMenu(
this, //the context
showMenuButton //UI view where to click to show the popup menu
);
//add menu xml to our popup menu
popupMenu.getMenuInflater().inflate(R.menu.pop_menu, popupMenu.getMenu());
//handle popup menu item clicks
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
//get id of menu item clicked
int id = menuItem.getItemId();
//handle clicks
if (id==R.id.settings_menu){
//settings selected
Toast.makeText(MainActivity.this, "Settings Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main2Activity.class));
getTitleColor();
return true;
}
else if (id==R.id.manual_menu){
//Manual selected
Toast.makeText(MainActivity.this, "User Manual Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main3Activity.class));
return true;
}
else if (id==R.id.about_menu){
//about selected
Toast.makeText(MainActivity.this, "About Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main4Activity.class));
return true;
}
return false;
}
});
//handle button click to show menu
showMenuButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popupMenu.show();
}
});
this is the menu XML
<item
android:id="#+id/settings_menu"
android:title="Settings"
android:icon="#drawable/ic_settings"
app:showAsAction="always|withText" />
<item
android:id="#+id/manual_menu"
android:title="User Manual"
android:icon="#drawable/ic_developer" />
<item
android:id="#+id/about_menu"
android:title="About"
android:icon="#drawable/ic_about"/>
I discovered from my research that to show drop-down popup menu with menu item icon takes deep process. And the process is not clean.
However, i have found a solution. Creating sub menu seems to be the only lee-way to get what we want. And I encourage anyone seeking to create menu icon to follow this way I founded for now while we wait further improvement from google.
<?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:id="#+id/action_more"
android:icon="#android:drawable/ic_menu_more"
android:orderInCategory="1"
android:title="More Options ยป"
app:showAsAction="always">
<menu>
<item
android:id="#+id/settings_menu"
android:title="Settings"
android:icon="#drawable/ic_settings"/>
<item
android:id="#+id/manual_menu"
android:title="User Manual"
android:icon="#drawable/ic_developer" />
<item
android:id="#+id/about_menu"
android:title="About"
android:icon="#drawable/ic_about"/>
</menu>
</item>
</menu>
Related
I cant click on item inside my menu. My menu opens and everything is displayed normally, but nothing happens when I click. I don't know why and searched for information but it doesn't help me.
This my XML code profile__detail_menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always"
android:icon="#drawable/ic_baseline_more_vert_24"
android:title="">
<menu>
<item
android:id="#+id/change_name"
android:title="Change name"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
</item>
</menu>
This my Java code.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.profile__detail_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
Toast.makeText(Profile.this,"Something", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.change_name:
Toast.makeText(this, "change_name selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.new_photo:
Toast.makeText(this, "new_photo selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.log_out:
Toast.makeText(this, "log_out selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The thing which I feel is wrong is your menu XML file. For some reason you have a menu tag inside a menu tag. So there is sort of a sub menu if you get my point? But you're not inflating that actually.
Modify the XML as follows
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".YourActivityName"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/change_name"
android:title="Change name"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_edit_24"
/>
<item
android:id="#+id/new_photo"
android:title="New photo"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_add_a_photo_24"
/>
<item
android:id="#+id/log_out"
android:title="Log out"
app:showAsAction="always|withText"
android:icon="#drawable/ic_baseline_login_purp_24"
/>
</menu>
I have updated your menu as per you code. If you need, add more items to it.
Can anyone help me, my bottom navigation bar looks like this:
I want to make it to look like this:
Offcourse, with blue color.
My code is below: Thanks everyone!!
layout.xml
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/main_manu"
app:itemBackground="#color/blue"
app:itemIconTint="#android:color/white"
app:itemTextColor="#android:color/white"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
/>
Application.java
final BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(2);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_pocetna:
new ListaVoznji.Home().execute();
break;
case R.id.action_unos:
new ListaVoznji.Login().execute();
break;
case R.id.action_pregled:
Intent intent2 = new Intent(ListaVoznji.this,ListaSvihVoznji.class);
intent2.putExtra("voznja",voznja);
startActivity(intent2);
break;
case R.id.action_shutdown:
Intent homescreen=new Intent(ListaVoznji.this,LoginActivity.class);
log = 1;
homescreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homescreen);
finish();
break;
}
return true;
}
});
Add app:labelVisibilityMode="labeled" in your layout in botton navigation
Just use the background attribute to change the whole background , and if you want to customize the item's text and icon color , you can do this by :
First create a drawable file name item_background , add to it the following lines :
<selector>
<item android:state_checked="true" android:color="#color/colorPrimary" />
<item android:state_checked="false" android:color="#color/colorAccent"/>
</selector>
Then in your bottom navigation view add attributes itemTextColor & itemIconTint with the value #drawable/item_background
I added Share App and Rate us links to Action bar.It shows nicely.
When click on Rate us ,it will open Play store link.It's OK.
But when click on Share App,it will open sharing dialog and App store link also.
I want to disable opening App store link when click Share App.
Another problem,
When click back button in app store page,it will go to Play store.But I want to go back to my app.
How to solve my 2 issues...?please help me
Here is my Java code.
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, String.format(getString(R.string.txt_share_me), "http://play.google.com/store/apps/details?id=" + this.getPackageName()));
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
case R.id.id_rateus:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=PackageName")));
}
return true;
}
Here is my Menu code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_share"
android:orderInCategory="100"
android:title="#string/action_share"
android:textAllCaps="false"
app:showAsAction="always" />
<item
android:id="#+id/id_rateus"
android:orderInCategory="100"
android:title="#string/action_rateus"
android:textAllCaps="false"
app:showAsAction="never" />
</menu>
Add break; after your share case and
Use
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + this.getPackageName())));
To access your app via the play store
I am using the new Android Design Navigation Drawer. I want to add a switch in the drawer. Is there a away to implement this?
this is the menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/nav_view_menu_group_1"
android:checkableBehavior="single">
<item
android:id="#+id/nav_view_menu_item_myschedule"
android:icon="#drawable/ic_navview_my_schedule"
android:title="#string/navview_menu_item_myschedule"
android:titleCondensed="#string/navview_menu_item_myschedule" />
<item
android:id="#+id/nav_view_menu_item_iolive"
android:icon="#drawable/ic_navview_play_circle_fill"
android:title="#string/navview_menu_item_iolive"
android:titleCondensed="#string/navview_menu_item_iolive"
android:visible="false"/>
<item
android:id="#+id/nav_view_menu_item_explore"
android:icon="#drawable/ic_navview_explore"
android:title="#string/navview_menu_item_explore"
android:titleCondensed="#string/navview_menu_item_explore" />
<item
android:id="#+id/nav_view_menu_item_map"
android:icon="#drawable/ic_navview_map"
android:title="#string/navview_menu_item_map"
android:titleCondensed="#string/navview_menu_item_map"
android:visible="false"/>
</group>
</menu>
How can I change one <item> to be switch:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Switch"
android:id="#+id/switch1"
android:layout_gravity="center_horizontal" />
I am currently using the default layout:
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/activity_main_drawer" />
Just Like this Image under Android Notification http://i.stack.imgur.com/M9nD7.png
I really Appreciate any feedback. Thank you.
One way I have found of doing this would be to use setActionView on the menuItem you want:
mNavigationView.getMenu().findItem(R.id.nav_connect)
.setActionView(new Switch(this));
// To set whether switch is on/off use:
((Switch) mNavigationView.getMenu().findItem(R.id.nav_connect).getActionView()).setChecked(true);
Probably want a click listener as well to change the state of the Switch:
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_connect:
((Switch) menuItem.getActionView()).toggle();
return true;
}
}
}
I haven't tried, but you could probably use android:actionLayout="#layout/switch_layout" in xml and point to a custom layout you created.
Also could try using an ActionProvider which might offer a little more robustness. I haven't tried this method either though.
We can do it by the following way
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
//Get a reference to your item by id
MenuItem item = menu.findItem(R.id.menu_pick_color);
//Here, you get access to the view of your item, in this case, the layout of the item has a FrameLayout as root view but you can change it to whatever you use
FrameLayout rootView = (FrameLayout)item.getActionView();
//Then you access to your control by finding it in the rootView
YourControlClass control = (YourControlClass) rootView.findViewById(R.id.control_id);
//And from here you can do whatever you want with your control
return true;
}
If you only want to check for the changes in the switch, you can set onCheckedChangeListener only for the switch like so
((Switch) navigationView.getMenu().findItem(R.id.darkModeSwitch).getActionView())
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Unchecked", Toast.LENGTH_SHORT).show();
}
}
});
If you want to be cool use lambda
((Switch) navigationView.getMenu().findItem(R.id.darkModeSwitch).getActionView())
.setOnCheckedChangeListener((buttonView, isChecked) -> {
if(isChecked) {
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Unchecked", Toast.LENGTH_SHORT).show();
}
});
I have a popup menu that works. The user would press an image and a popup menu appears with 5 items. The problem is that I don't seem to be able to change the size of the text of the popup.
The Java that calls the popup is as follows:
public void TheCompanyMenu(View v) {
PopupMenu mypopupmenu = new PopupMenu(this, v);
mypopupmenu.setOnMenuItemClickListener(this);
MenuInflater inflater = mypopupmenu.getMenuInflater();
inflater.inflate(R.menu.popup, mypopupmenu.getMenu());
mypopupmenu.show();
}
#Override
public boolean onMenuItemClick(MenuItem arg0) {
switch (arg0.getItemId()) {
case R.id.option1:
Intent intent1 = new Intent(this, MainActivity.class);
startActivity(intent1);
return true;
case R.id.option2:
Intent intent2 = new Intent(this, Item2.class);
startActivity(intent2);
return true;
case R.id.option3:
Intent intent3 = new Intent(this, Item3.class);
startActivity(intent3);
return true;
case R.id.option4:
Intent intent4 = new Intent(this, Item4.class);
startActivity(intent4);
return true;
case R.id.option5:
Intent intent5 = new Intent(this, Item5.class);
startActivity(intent5);
return true;
default:
return super.onContextItemSelected(arg0);
}
}
The Menu xml called popup is as follows:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/option1"
style="#style/PopupItemStyle"
android:textSize="12sp"
android:text="#string/HomeMenu"
android:title="#string/HomeMenu" />
<item android:id="#+id/option2"
style="#style/PopupItemStyle"
android:textSize="12sp"
android:text="#string/option2"
android:title="#string/option2menu" />
<item android:id="#+id/option3"
style="#style/PopupItemStyle"
android:textSize="12sp"
android:text="#string/option3"
android:title="#string/option3menu" />
<item android:id="#+id/option4"
style="#style/PopupItemStyle"
android:textSize="12sp"
android:text="#string/option4"
android:title="#string/option4menu" />
<item android:id="#+id/option5"
style="#style/PopupItemStyle"
android:textSize="12sp"
android:text="#string/option5"
android:title="#string/option5Menu" />
</menu>
I have tried changing the android:textSize to be dp but it has no effect.
The code in the style xml is:
<style name="PopupItemStyle">
<item name="android:background">#FFA0A0A0</item>
<item name="android:gravity">center</item>
<item name="android:padding">10dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">12sp</item>
</style>
As I am fairly new to Java, I am keen to keep the java bit as it is (i.e. using popupmenu) as it is working (sort of), but need to change the text size.
Thanks a lot!
actually,I also want do like this.but,I do it in another way.
private void showPopUp(View v) {
PopupMenu popup = new PopupMenu(DetailActivity.this, v);
LayoutInflater inflater = (LayoutInflater) DetailActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.GRAY);
//link layout and popwindow
View myView = inflater.inflate(R.layout.menu_popwindow, null);
PopupWindow myPopupWindow = new PopupWindow(myView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
myPopupWindow.setFocusable(true);//getfocus for editview
//the follow help dismiss popup myPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.linkme_menu));
myPopupWindow.setOutsideTouchable(true);
myPopupWindow.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss()
{ } }});myPopupWindow.showAsDropDown(v);
i use it in actionbar,you can creat a new xml(menu_popwindow)by yourself.