First of all I saw one stack thread regarding the same question but its not completely working for me may be because I am a newbie if anybody knows please help me , I am in navigation drawer now I want make the menu items to align in center something just like this
this is what I got from stack overflow
int positionOfMenuItem = 0; //or any other postion
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString(settingsItemTitle);
s.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), 0, s.length(), 0);
item.setTitle(s);
I wrote this code in activity main and I am not sure that what is settingsItemTitlein this code so studio giving me error
Cannot resolve symbol 'settingsItemTitle'
if anybody knows please help me
Instead of using MenuItem (along with, I'm guessing, NavigationView), just use a vertical LinearLayout for your drawer content. You'll be able to style it any way you want.
You can try developing a custom drawer where you can place the menu items in the centre.
Custom navigation drawer
settingsItemTitle should be replaced by the title of your MenuItem.
In case the title was already set in the menu layout file, you could rewrite your code like:
int positionOfMenuItem = 0; //or any other postion
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString(item.getTitle());
s.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), 0, s.length(), 0);
item.setTitle(s);
This works, if you call it in the overwritten onCreateOptionsMenu method of your Activity, after having inflated the menu.
If you intend to do it for all items of your menu, you need to loop over them. Something like:
for(int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
SpannableString s = new SpannableString(item.getTitle());
s.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), 0, s.length(), 0);
item.setTitle(s);
}
I think setting this in an own layout, as Gavin pointed out in his answer, would be neater, but come at the price of not being able to use the Android off the shelf menu.
Related
I am using Navigation View in android studio to create a navigation drawer in my app. In the navigation drawer, I want one item to be colored in green, one in red and others in black (just like the one in the screenshot below). However, I can't seem to find the solution to this. I know I can change the color of all the items using 'itemTextColor' in XML but that's not what I want to do.
So it seems it not as easy to change specific items colours, its either all or nothing (via app:itemIconTint and app:itemTextColor on the com.google.android.material.navigation.NavigationView), but it seems you can do something like this (I did it in Java as you never mentioned if you were using Kotlin or Java):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// .. other code
NavigationView navigationView = findViewById(R.id.nav_view);
// .. other code
int menuItemPosition = 0; // the position of the menu item in NavigationView you want to change the color of
MenuItem menuItem = navigationView.getMenu().getItem(menuItemPosition);
this.changeMenuItemColor(menuItem, Color.RED);
}
private void changeMenuItemColor(MenuItem menuItem, #ColorInt int color) {
SpannableString coloredMenuItemTitle = new SpannableString(menuItem.getTitle());
coloredMenuItemTitle.setSpan(new ForegroundColorSpan(Color.RED), 0, coloredMenuItemTitle.length(), 0);
menuItem.setTitle(coloredMenuItemTitle);
}
I had already had some issues with the overflow button being black and wanting it white but after some trial and error I got it working. But I just can't get this text button to be white. It's just a string based item in the main_toolbar.xml
<item
android:id="#+id/more"
android:title="#string/more"
app:showAsAction="always"/>
Link to the image
After playing around for several hours in styles.xml and looking for solutions in here, I gave up and just decided to ask for my specific problem. Thanks in advance!
You can change the color of the MenuItem text easily by using SpannableString instead of String
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu, menu);
int positionOfMenuItem = 0; // or whatever...
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
item.setTitle(s);
}
I'm using NavigationView and menu on my app.
I want one of the menu items to to be in different color (Icon and text).
I can make the icon show other color using setItemIconTintList(null) and using icons in the required colors.
Is there a way to make a single menu item text color to be different?
You can use like this :
//Change to your NavigationView Id
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
//change R.id.menu_item to your id
navigationView.getMenu().findItem(R.id.menu_item).setTitle(Html.fromHtml("<font color='#ff3824'>Settings</font>"));
Inspired by DevTest's answer, I've eventually used SpannableString for setting a text with a specific color.
I've created a util method for it:
public static CharSequence getSpannableColorString (String text, int color) {
SpannableString spanString = new SpannableString(text);
spanString.setSpan(new ForegroundColorSpan(color),0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spanString;
}
Then used it for setting my MenuItem's title:
MenuItem menuItem = navigationView.getMenu().findItem(R.id.myItem);
menuItem.setTitle(getSpannableColorString(getString(R.string.itemText), getColor(R.color.itemColor)));
This is a short question:
I'm trying to force the action bar (used by a Toolbar) to use LTR alignment. I've succeeded making the layout itself use LTR, but not the "up" button (as I've done here, before Toolbar was introduced) .
It seems this view doesn't have an ID, and I think using getChildAt() is too risky.
Can anyone help?
The answer
Here's one way I've found to solve this, based on this answer .
I made it so that it is guarranteed to find only the "up" button, and whatever it does, it will revert back to the previous state it was before.
Here's the code:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
public boolean onCreateOptionsMenu(final Menu menu)
{
// <= do the normal stuff of action bar menu preparetions
if(VERSION.SDK_INT>=VERSION_CODES.JELLY_BEAN_MR1&&getResources().getConfiguration().getLayoutDirection()==View.LAYOUT_DIRECTION_RTL)
{
final ArrayList<View> outViews=new ArrayList<>();
final CharSequence previousDesc=_toolbar.getNavigationContentDescription();
for(int id=0;;++id)
{
final String uniqueContentDescription=Integer.toString(id);
_toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if(!outViews.isEmpty())
continue;
_toolbar.setNavigationContentDescription(uniqueContentDescription);
_toolbar.findViewsWithText(outViews,uniqueContentDescription,View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if (outViews.isEmpty())
if (BuildConfig.DEBUG)
throw new RuntimeException(
"You should call this function only when the toolbar already has views");
else
break;
outViews.get(0).setRotation(180f);
break;
}
_toolbar.setNavigationContentDescription(previousDesc);
}
//
return super.onCreateOptionsMenu(menu);
}
It seems this view doesn't have an ID
You're right, the navigation view is created programmatically and never sets an id. But you can still find it by using View.findViewsWithText.
View.findViewsWithText comes with two flags:
View.FIND_VIEWS_WITH_TEXT
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION
The navigation view's default content description is "Navigate up" or the resource id is abc_action_bar_up_description for AppCompat and action_bar_up_description for the framework's, but you can easily apply your own using Toolbar.setNavigationContentDescription.
Here's an example implementation:
final Toolbar toolbar = ...;
toolbar.setNavigationContentDescription("up");
setActionBar(toolbar);
final ArrayList<View> outViews = Lists.newArrayList();
toolbar.findViewsWithText(outViews, "up", View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
outViews.get(0).setRotation(180f);
Results
I would like to dynamically change the "home" icon in the ActionBar. This is easily done in v14 with ActionBar.setIcon(...), but I can't find anyway to accomplish this in previous versions.
If your actionbar works like Sherlock and is based on menu items, this is my solution:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem switchButton = menu.findItem(R.id.SwitchSearchOption);
if(searchScriptDisplayed){
switchButton.setIcon(R.drawable.menu_precedent);
}else{
switchButton.setIcon(R.drawable.icon_search);
}
return super.onPrepareOptionsMenu(menu);
}
If you are using the ActionbarCompat code provided by google, you can access the home icon via the ActionBarHelperBase.java class for API v4 onwards.
//code snippet from ActionBarHelperBase.java
...
private void setupActionBar() {
final ViewGroup actionBarCompat = getActionBarCompat();
if (actionBarCompat == null) {
return;
}
LinearLayout.LayoutParams springLayoutParams = new LinearLayout.LayoutParams(
0, ViewGroup.LayoutParams.MATCH_PARENT);
springLayoutParams.weight = 1;
// Add Home button
SimpleMenu tempMenu = new SimpleMenu(mActivity);
SimpleMenuItem homeItem = new SimpleMenuItem(tempMenu,
android.R.id.home, 0, mActivity.getString(R.string.app_name));
homeItem.setIcon(R.drawable.ic_home_ftn);
addActionItemCompatFromMenuItem(homeItem);
// Add title text
TextView titleText = new TextView(mActivity, null,
R.attr.actionbarCompatTitleStyle);
titleText.setLayoutParams(springLayoutParams);
titleText.setText(mActivity.getTitle());
actionBarCompat.addView(titleText);
}
...
You should be able to modify the code to the home button accessible to the activities that extend ActionBarActivity and change it that way.
Honeycomb seems a little harder and it doesn't seem to give such easy access. At a guess, its id should also be android.R.id.home so you may be able to pull that from the view in ActionBarHelperHoneycomb.java
I would say you do something like this :
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_drawer);
see the link How to change the icon actionBarCompat
The ActionBar will use the android:logo attribute of your manifest, if one is provided. That lets you use separate drawable resources for the icon (Launcher) and the logo (ActionBar, among other things).