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.
Related
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 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>
I have this menu that pops up fine when I create it.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/day"
android:title="Day" />
<item
android:id="#+id/week"
android:title="Week" />
<item
android:id="#+id/month"
android:title="Month" />
<item
android:id="#+id/year"
android:title="Year" />
</menu>
For some reason, the onMenuItemClick is only giving me "Year" no matter which item I click on. This is a really strange problem and I have no idea why it's happening.
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(getContext(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.date_range_menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.day:
((MainActivity) requireActivity()).toast("Day");
changeDateRange(DateRange.DAY);
case R.id.week:
((MainActivity) requireActivity()).toast("Week");
changeDateRange(DateRange.WEEK);
case R.id.month:
((MainActivity) requireActivity()).toast("Month");
changeDateRange(DateRange.MONTH);
case R.id.year:
((MainActivity) requireActivity()).toast("Year");
changeDateRange(DateRange.YEAR);
default:
return false;
}
}
});
}
You need to add the break; keyword to each case.
i am trying to add listner to my navigation drawer menu item but it's not working please check my script and also check this image [https://www.dropbox.com/s/imzufywwj5qyjlj/Nav_Menu.png?dl=0] Thank You.
ProfileActivity.java
NavigationView navigationView =(NavigationView)findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
TextView prousername = (TextView) headerView.findViewById(R.id.pro_username);
TextView promail = (TextView) headerView.findViewById(R.id.pro_mail);
TextView prophone = (TextView) headerView.findViewById(R.id.pro_phone);
ProDrawareLayout = (DrawerLayout)findViewById(R.id.activity_profile);
ProToggle = new ActionBarDrawerToggle(this,ProDrawareLayout, R.string.open, R.string.close);
ProDrawareLayout.addDrawerListener(ProToggle);
ProToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
prousername.setText(SharedPrefManager.getInstance(this).getUsername());
promail.setText(SharedPrefManager.getInstance(this).getUserEmail());
prophone.setText(SharedPrefManager.getInstance(this).getUserPhone());
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Switch(item.getItemId()){
case R.id.nav_logout:
SharedPrefManager.getInstance(this).logOut();
finish();
startActivity(new Intent(this, MainActivity.class));
}
Navigation_menu.xml
<group android:checkableBehavior="single">
<item android:id="#+id/nav_account"
android:title="My account"/>
<item android:id="#+id/nav_settings"
android:title="Settings"/>
<item android:id="#+id/nav_logout"
android:title="Log Out"/>
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/Rate_id"
android:title="#string/rate_us"
android:showAsAction="never"></item>
<item
android:id="#+id/Contact_id"
android:title="#string/contact_us"
android:showAsAction="never"></item>
<item
android:id="#+id/About_id"
android:title="#string/about_us"
android:showAsAction="never"></item>
</menu>
</item>
activity_profile.xml
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
android:background="#color/colorBG"
app:headerLayout="#layout/navigation_header"
android:id="#+id/nav_view" >
</android.support.design.widget.NavigationView>
Remove the upper case on your switch case function.
switch instead of Switch
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Switch(item.getItemId()){ // the issue is here
case R.id.nav_logout:
SharedPrefManager.getInstance(this).logOut();
finish();
startActivity(new Intent(this, MainActivity.class));
}
EDIT:
You can't use this like a reference to your activity because your are in this case in a OnNavigationItemSelectedListener.
Try that instead :
Create a class field private Context mContext.
Instanciate mContext on onCreate function : mContext = this;
Use mContext.
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Switch(item.getItemId()){ // the issue is here
case R.id.nav_logout:
SharedPrefManager.getInstance(mContext).logOut();
finish();
startActivity(new Intent(mContext, MainActivity.class));
}
I've used a pretty standard implementation of the ShareActionProvider in my actionbar and it works fine. However, when I start another activity from an actionbutton in the actionbar and return from that activity the dropdownmenu of the SharedActionProvider is automatically opened.
This behavior doesn't occur when I execute the exact same code to open another activity from a normal button.
Here is my menu xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_btn_my_scedule"
android:icon="#drawable/ic_menu_my_calendar"
android:title="To Scedule"
android:showAsAction="ifRoom" />
<item android:id="#+id/menu_btn_share"
android:title="Share.."
android:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_set_scedule"
android:icon="#drawable/ic_menu_today"
android:title="Set Scedule"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_settings"
android:icon="#drawable/ic_menu_manage"
android:title="Settings"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_feedback"
android:icon="#drawable/ic_menu_manage"
android:title="Feedback/Question"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_about"
android:icon="#drawable/ic_menu_info_details"
android:title="About"
android:showAsAction="collapseActionView"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
The other menu methods:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.optionsmenu, menu);
MenuItem actionItem = menu.findItem(R.id.menu_btn_share);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
actionProvider.setShareIntent(createShareIntent());
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return false;
case R.id.menu_btn_my_scedule:
Intent i = new Intent(ONTTOptions.this, ONTTShowScedule.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
case R.id.menu_btn_set_scedule:
i = new Intent(ONTTOptions.this, ONTTScedule.class);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
case R.id.menu_btn_settings:
i = new Intent(ONTTOptions.this, ONTTPreferences.class);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
case R.id.menu_btn_feedback:
showFeedbackDialog();
return true;
case R.id.menu_btn_about:
i = new Intent(ONTTOptions.this, ONTTAboutActivity.class);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
startActivity(i);
return true;
default: return super.onOptionsItemSelected(item);
}
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + ONTTConfig.PACKAGE_NAME);
return shareIntent;
}
I must have missed something somewhere, thanks for any help.
I found the problem. I thought android:actionProviderClass="android.widget.ShareActionProvider"
was also needed on every actionbar item I wanted in the overflow menu. I simply removed it on every item except the share button ofcourse.
My menu xml now looks like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_btn_my_scedule"
android:icon="#drawable/ic_menu_my_calendar"
android:title="To Scedule"
android:showAsAction="ifRoom" />
<item android:id="#+id/menu_btn_share"
android:title="Share.."
android:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item android:id="#+id/menu_btn_set_scedule"
android:icon="#drawable/ic_menu_today"
android:title="Set Scedule"
android:showAsAction="collapseActionView" />
<item android:id="#+id/menu_btn_settings"
android:icon="#drawable/ic_menu_manage"
android:title="Settings"
android:showAsAction="collapseActionView" />
<item android:id="#+id/menu_btn_feedback"
android:icon="#drawable/ic_menu_manage"
android:title="Feedback/Question"
android:showAsAction="collapseActionView" />
<item android:id="#+id/menu_btn_about"
android:icon="#drawable/ic_menu_info_details"
android:title="About"
android:showAsAction="collapseActionView" />
</menu>