get drawable from Android Menu Item Icon - android

I have a bookmark icon in a menu item. I want to change the drawable in the icon depending on whether the bookmark has been pressed before or not.
I have two drawbles, staro (meaning star orange) or starw(meaning star white). I just want to toggle this on press.
How can I know which drawble is in the icon in public boolean onOptionsItemSelected(MenuItem item) method. Is it possible to know the drawable via the item. what I know is that item.getIcon() is not the drawble. I cannot compare item.getIcon() with R.drawable.starto

You could try
if (item.getIcon().getConstantState().equals(
getResources().getDrawable(R.drawable.starto).getConstantState()
)) {
...
}
As mentioned here

You can do the changes in onPrepareOptionsMenu() which is called every time before the menu is shown. Its suitable to show/hide options based on some dynamic data.
If you already now the condition, you can directly call
if (condition_for_orange) {
menu.findItem(resourceId).setIcon(R.drawable.staro);
} else {
menu.findItem(resourceId).setIcon(R.drawable.startw);
}
You can use Shared Preference or some other global variable which can store the state which may help you to decide which icon to show now.

you can compare these too.
you can find id of drawable by
int identifier = getResources().getIdentifier("pic1", "drawable","android.demo");
and then you can compare this with R.drawable.starto `.

Related

Remove 'tile' when clicked on

I'm working on a small project where you need to guess the picture behind the tiles. Currently everything is working, but I have no idea how I can check if someone clicked a tile.
I know I can do it with a button, but I want to be able to remove a tile when someone actually presses that tile. Is there a way to check if someone pressed somewhere on the screen or something?
You need to make your tile clickable, and then add a method on click event.
Add the following to your tiles in xml.
android:clickable="true"
android:onClick="TileClicked"
Then create a method in your activity
public void TileClicked(View v)
{
int clickedID = v.getId();
// Do something to the clicked tile .. e.g.
v.setVisiblity(View.INVISIBLE);
// or filter specific tiles
if(clickedID = R.id.myTile1)
{
// do something when tile 1 clicked
}
}
Alternatively, you can add the onclick listener in code and call the method there.

How to programmaticallly remove submenu from navigation drawer in Android?

I am developing an Android app. Firstly, let me tell you that I am not professional. What I am doing now is I am adding submenu to menu depending on a condition. But I need to do it very often in my app. But my problem is I added a submenu to the menu as first time.
But second time when I update menu depending on condition, existing submenu is not removed and new submenu is appended to navigation drawer. How can I remove submenu that is programmatically added to menu? Why my code is not removing it?
Here is my code
public void updateAuthUI()
{
isLoggedIn = tempStorage.getBoolean(getResources().getString(R.string.pref_is_logged_in),false);
Menu menu = leftDrawer.getMenu();
menu.removeItem(getResources().getInteger(R.integer.logout_item_id));
menu.removeItem(getResources().getInteger(R.integer.login_item_id));
menu.removeItem(getResources().getInteger(R.integer.register_item_id));
SubMenu authSubMenu = menu.addSubMenu("Auth");
if(isLoggedIn)
{
authSubMenu.add(1,getResources().getInteger(R.integer.logout_item_id),99,"Sign out");
}
else{
authSubMenu.add(1,getResources().getInteger(R.integer.register_item_id),97,"Register");
authSubMenu.add(1,getResources().getInteger(R.integer.login_item_id),98,"Sign in").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
openLoginActivity();
item.setChecked(true);
return true;
}
});
}
}
Here is the screenshot of my problem
As you can see Auth submenu is appended without removing existing one.
Try
authSubMenu.clear();
before your first
authSubMenu.add();
I just used SubMenu.clear() in an Android app where I was using a third-party library, and I needed to clear out an unwanted submenu from the action bar. (I actually wanted to remove the submenu completely, and this was the only way I could find to do it. It seemed to work.)
That's different from your situation, where authSubMenu is a menu you just added via menu.addSubMenu("Auth"), so you would expect it to be empty. But, as you've seen, it apparently isn't empty: rather, addSubMenu("Auth") returns the existing submenu of that title. (I can't find documentation to that effect; I'm just going by the results you've reported.)
If that really is the case, as it appears to be, then authSubMenu.clear() will remove all existing items from the submenu.
As #slymm said in a comment you can remove all menu and submenu items using
navigationView.getMenu().clear();
This can be used to remove submenu (and menu elements) and then recreate them with the new required items

Why does findViewById(R.android.id.home) always return null?

I'm using AppCompat and trying to recall the ImageView for the up/back button belonging to the toolbar.
I know R.android.id.home exists, because I can manage its click as a Menu item:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
//this works
}
return super.onOptionsItemSelected(item);
}
Apart from that, whenever I try to call findViewById(android.R.id.home) - be it onCreate, be it onClick of a custom button - I get null.
I even get null if, in the sample above, I call findViewById(item.getItemId()).
Why is it?
This question has been asked before here, most times regarding ActionBarSherlock (which I am not using). Another time it was suggested to use:
getWindow().getDecorView().findViewById(android.R.id.home)
But it isn't working. In that question the OP also says findViewById(android.R.id.home) works on API>3.0, but that's not true for me. Any ideas?
Whether or not the "home" icon is a widget, and what class of widget it is, and what its ID is (if any), is up to the implementation of the action bar. The native action bar may do this differently for different API levels, and all of that may be different than the way appcompat-v7 does it. Let alone ActionBarSherlock or other action bar implementations.
Specifically, android.R.id.home is a menu ID, which is why you can use it in places like onOptionsItemSelected(). It is not necessarily a widget ID, which is why it may or may not work with findViewById().
Ideally, you do not attempt to mess with the internal implementation of a UI that you did not construct yourself.
do one really has to make his own Up button to style it?
I do not know, as I have never tried to style it.
As CommonsWare said android.R.id.home is a menu ID, not a widget ID. But if you want to access this home button you could do it. For example I needed it to highlight home button in in-app tutorial:
fun AppCompatActivity.getToolbarHomeIcon(): View? =
this.findViewById<Toolbar?>(R.id.toolbar)?.let { toolbar ->
val contentDescription: CharSequence = toolbar.navigationContentDescription.let {
if (it.isNullOrEmpty()) {
this.getString(R.string.abc_action_bar_up_description)
} else {
it
}
}
// Here home button should be created even if it doesn't exist before
toolbar.navigationContentDescription = contentDescription
ArrayList<View>().let { potentialViews ->
toolbar.findViewsWithText(
potentialViews,
contentDescription,
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION
)
potentialViews.getOrNull(0)
}
}

android - change options menu dynamically , but by inflating from XML

i need to be able to change the options menu (the one that is shown upon pressing the menu button) on android , so that on one case (for example upon a button being pressed) , it will use a specific menu resource (XML file as in /res/menu/... ) for the menu , and on another case , use a different XML file.
so far i've seen only examples of doing it without xml (example here and here) , and they worked fine , but i want to be able to change the entire menu on some cases.
i've tried to modify the solutions i've found , but none of my trials worked.
if possible , i would prefer to re-create the menu only if the it needs to be updated with a menu resource that is different from the current one.
please help me.
If you want to change the Options Menu any time after it's first created, you must override the onPrepareOptionsMenu() method.
public boolean onPrepareOptionsMenu (Menu menu) {
menu.clear();
if (CASE_1 == 0) {
CASE_1 = 1;
getMenuInflater().inflate(R.menu.secondmenu, menu);
}
else {
CASE_1 = 0;
getMenuInflater().inflate(R.menu.firstmenu, menu);
}
return super.onPrepareOptionsMenu(menu);
}
where CASE_1 refer to the which menu you want to display depending on your requirement.

Setting Android CheckBox to a different image... and then back to the original images

I'm using the following (very common) code to change the checkbox image in my Android app.
mCheck = (CheckBox) findViewById(R.id.chkMine);
mCheck.setButtonDrawable(R.drawable.my_image);
I see many people asking for that. But I never see the second part:
How do I put BACK the original checkbox imagery, later in my code?
I hesitate to try to design all my own images (checked, unchecked, ghosted-checked, ghosted-unchecked, etc) because I need the original images that would normally appear on many different version of Android.
Maybe, initially save the default image with a (non-existing?) getButtonDrawable() call, and then reuse it later?
I would think it would be something as simple as calling setButtonDrawable() a 2nd time to "undo" my changes. Or is it?
Thanks.
As you already correctly mention yourself, unfortunately there is no getButtonDrawable() to get a reference to the drawable used before you replace it. Obviously you could copy Android's resources over to your local project and use those to reset the CheckBox's button, but that would mean you would have to take into account all the different styles from themes, let alone any changes device manufacturers might make to those resources. It's not impossible to go down this lane, but you'll find out it will be quite of a hassle for something that sounds to simple.
What you may want to consider is doing the following: query the resources for resource ids of the drawables that you need. That way you don't explicitly have to deal with different themes, as the lookups will do that. You can easily put this functionality in a dedicated method with just a few lines. Example:
private static int getDefaultCheckBoxButtonDrawableResourceId(Context context) {
// pre-Honeycomb has a different way of setting the CheckBox button drawable
if (Build.VERSION.SDK_INT <= 10) return Resources.getSystem().getIdentifier("btn_check", "drawable", "android");
// starting with Honeycomb, retrieve the theme-based indicator as CheckBox button drawable
TypedValue value = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.listChoiceIndicatorMultiple, value, true);
return value.resourceId;
}
Below a quick example of setting the button drawable to some custom image, and reset it afterwards. I'm simply toggling between the app icon and original button drawable whenever the check state changes.
CheckBox mCheckBox = (CheckBox) findViewById(R.id.checkbox);
mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override public void onCheckedChanged(CompoundButton button, boolean isChecked) {
button.setButtonDrawable(isChecked ? R.drawable.icon : getDefaultCheckBoxButtonDrawableResourceId(StackOverflowActivity.this));
}
});
I think to retrieve the original images a call to setButtonDrawable() should work again.
But instead of your resource you'll reference to android's original resource.
mCheck = (CheckBox) findViewById(R.id.chkMine);
mCheck.setButtonDrawable(android.R.drawable.*);
Probably you've to lookup the file name on your own:
platforms > android-* > data > res > drawable-*
(source: accepted answer)
edit
Ha! Me knew me've seen that site before: Android R Drawables ^^v
(credits to Mef)

Categories

Resources