Can't display an icon in the actionbar - java

I'm trying to show a map icon downloaded from the official android developers source.
I did everything as it should, but the icon won't show.
Here is my xml file named main_activity_bar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/mapIcon"
android:icon="#drawable/ic_action_map"
android:title="#string/mapIconTitle"
android:showAsAction="always"
/>
</menu>
Here is the main activity xml:
<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="match_parent"
tools:context="com.gs.testApp.MainActivity"
tools:ignore="MergeRootFrame" />
and this is what I have in the java class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater actionMenue = getMenuInflater();
actionMenue.inflate(R.menu.main_activity_bar, menu);
return super.onCreateOptionsMenu(menu);
}
Everything seems to be fine, but the icon wont show on the emulator. Here is a screenshot:
The minimum version is android 3.0
Why the icon is not showing? What Am I missing? I know that it is something really small, but I can't spot it.

Here is how I fixed it - in case that someone is facing the same issue.
I changed
android:showAsAction="always" to app:showAsAction="always" and I also placed the icon order android:orderInCategory="0" and the auto res xmlns:app="http://schemas.android.com/apk/res-auto" so now my xml looks like:
<?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:orderInCategory="0"
android:id="#+id/mapIcon"
android:icon="#drawable/ic_action_map"
android:title="#string/mapIconTitle"
app:showAsAction="always"
/>
</menu>

I dont think an icon is shown in the overflow menu. Try changing your title to "t" or rotate the screen. See if the icon appears this way.

Related

How to implement this bottom navigation bar

Hei I have the new android studio 4.1
I have imported a bottom navigation menu from git hub here this one
Now I have set up dependencies correctly
I did all the things necessary but still, there are no actions in the navigation bar, the colour is not changing. even the menus are not displaying
here is the main activity code
<me.ibrahimsn.lib.SmoothBottomBar
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="70dp"
app:backgroundColor="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
this is the menu code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/home"
android:title="Home"/>
<item android:id="#+id/rank"
android:title="Rank"/>
</menu>
now still the background is white see this: image still not changing
Have you added this line in your layout parent tag:
xmlns:app = "http://schemas.android.com/apk/res-auto"

Android - How to add a default checked CheckBox to Navigation Drawer

I want to add a check box to Navigation Drawer, and I want one of them to be default checked
I'm using this: app:actionViewClass="android.widget.Switch" as suggested in this answer.
But I couldn't figure out how to make one of them default checked. If I use this property android:checked="true", then the option is checked instead of checkbox (See image).
Does anyone know how to make it default checked (if possible, I want to do it in XML only)?
Here is my activity_drawer.xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group
android:checkableBehavior="single">
<item
android:id="#+id/checkboxX-axis"
android:title="Show x-axis"
android:icon="#drawable/ic_x_axis_black_24dp"
app:actionViewClass="android.widget.CheckBox"
android:checked="true"
/>
<item
android:id="#+id/checkboxY-axis"
android:title="Show y-axis"
android:icon="#drawable/ic_y_axis_black_24dp"
app:actionViewClass="android.widget.CheckBox"
/>
<item
android:id="#+id/checkboxZ-axis"
android:title="Show z-axis"
android:icon="#drawable/ic_z_axis_black_24dp"
app:actionViewClass="android.widget.CheckBox"
/>
</group>
</menu>
You can do this:
MenuItem item = navigation.getMenu().findItem(R.id.checkboxX-axis);
CompoundButton compoundButton = (CompoundButton) item.getActionView();
compoundButton.setChecked(true);
replace navigation with the NavigationView's name.

open a menu from Image View

I have problem to set a menu.
I want to open a menu from an ImageView, because I want to use a specific image on the tool bar.
I can't found any tutorials
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater =getMenuInflater();
menuInflater.inflate(R.menu.days, menu);
return true;
}
Thanks in advance!
Do you want to create menu something like this?
Menu xml code: menu/days.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/item_love"
android:title="Love"
android:icon="#drawable/baseline_favorite_border_white_48"
app:showAsAction="always">
</item>
<item
android:id="#+id/item_more"
android:title="More"
android:icon="#drawable/baseline_add_white_48"
app:showAsAction="always">
<menu>
<item android:id="#+id/sub_item_1"
android:title="Sub Item 1"
app:showAsAction="withText"
/>
<item android:id="#+id/sub_item_2"
android:title="Sub Item 2"
app:showAsAction="withText"
/>
<item android:id="#+id/sub_item_3"
android:title="Sub Item 3"
app:showAsAction="withText"
/>
</menu>
</item>
</menu>
Full source code: https://github.com/hiepxuan2008/basic-menu-android
Hope it will help you. Thanks!

Change MenuItem drawable on user click

So I have the following configuration:
A MenuItem with a selector drawable attached which I want to change based on user click.
However for some reason I cannot make it work because I cannot find a way to compare the icons(drawables).
My configuration is attached below:
<?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/menu_grid_id"
android:icon="#drawable/grid_option"
android:title="#string/layout_type"
app:showAsAction="ifRoom" />
</menu>
Selector is attached bellow:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_grid_on_32"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_grid_off_32" /> <!-- default -->
</selector>
Also the onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_grid_id: {
//I tried to use getContentState but does not seem to be reliable.
}
default:
return true;
}
}
I end up answering my own question. Maybe it will help others that have the same problem.
The idea is relatively simple(if you know it), and it is to use a switch or a toggle button.
Options menu looks like below:
<?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/menu_grid_id"
android:title="#string/layout_type"
// that was the difference
app:actionLayout="#layout/grid_option_layout"
app:showAsAction="ifRoom" />
</menu>
Now, one can use ToggleButton or Switch. I ended up using ToggleButton. Below one can se the configuration:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ToggleButton
android:id="#+id/switch_grid"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerVertical="true"
android:background="#drawable/grid_option"
android:focusable="false"
android:focusableInTouchMode="false"
android:textOff=""
android:textOn="" />
</RelativeLayout>
Now, the drawable selector looks like below(grid_option):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_grid_on_32" android:state_checked="true" />
<item android:drawable="#drawable/ic_grid_off_32" /> <!-- default -->
</selector>
Of course, ic_grid_on and off are some images.

Android error: cannot find symbol variable menu

Im creating menu:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.points, menu);
return true;
}
I have xml file in res/menu/points.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Item" />
</menu>
I get error - Error:(27, 36) error: cannot find symbol variable menu
why ?
Clean Rebuild helps - Thanks
I had this same problem and my issue was with the parameter Menu menu. When i first invoked Menu i didn't hit enter to set it to android.view menu, which would explain why yours is saying it doesn't understand the variable. If your not casting it as a menu type for what you're importing it as then it wont understand it as a standalone variable. I hope this helps, we could possible be working on the same tutorial. Good luck either way!!
Create a menu_main.xml file in menu folder in res folder.res>menu>menu_main.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.example.myapplication.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="action_settings"
app:showAsAction="never" />

Categories