How to add icon to menu icon selection in android? - android

I want to add icon like the above images in android
How can I do it
This is my menu.xml at the moment, I try to use android:icon, it is not showing
<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.kahheng.smartchat.MainActivity">
<item
android:id="#+id/action_poll"
android:orderInCategory="100"
android:layout_width="35dp"
android:layout_height="50dp"
android:title="#string/action_poll"
android:icon="#mipmap/pollbtn"
app:showAsAction="always" />
<item
android:id="#+id/action_draw"
android:orderInCategory="100"
android:layout_width="35dp"
android:layout_height="50dp"
android:title="#string/action_draw"
android:icon ="#mipmap/drawbtn"
app:showAsAction="always" />
<item
android:id="#+id/action_bookmark"
android:orderInCategory="100"
android:layout_width="35dp"
android:layout_height="50dp"
android:icon="#drawable/sendbtn"
android:title="#string/action_bookmark"
app:showAsAction="always" />
</menu>
what can I do to make the icon appear?
This is my mainactivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_draw:
Intent newActivity = new Intent(MainActivity.this, DrawingActivity.class);
startActivity(newActivity);
break;
//Me
case R.id.action_poll:
Intent pollActivity = new Intent(MainActivity.this, pollActivity.class);
startActivity(pollActivity);
break;
case R.id.action_bookmark:
Intent bookmarkActivity = new Intent(MainActivity.this, bookmarkActivity.class);
startActivity(bookmarkActivity);
break;
}
return true;
}

Related

how to set menu item title in java File?

curently done: menu working fine.
i want: i want to set menu title in MainActivity.java
attched: 1. activitymain.xml 2. MAinActivity.java 3. menu_nevigation.xml
can i set menu item title in MainActivity.java because i want to change title in some conditions how to settitle in java file...
activityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textSize="50sp"
android:textStyle="bold"
android:layout_centerInParent="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_navigation"
app:itemBackground="#color/colorcustom"
app:itemTextColor="#drawable/selector"
app:itemIconTint="#drawable/selector"
app:menu="#menu/menu_navigation"
app:labelVisibilityMode="labeled"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize And Assign Variable
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
//Set Home Selected
bottomNavigationView.setSelectedItemId(R.id.home);
//Perform ItemSelected
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.about:
startActivity(new Intent(getApplicationContext()
,about.class));
overridePendingTransition(0,0);
return true;
case R.id.home:
return true;
case R.id.term:
startActivity(new Intent(getApplicationContext()
,term.class));
overridePendingTransition(0,0);
return true;
case R.id.test:
startActivity(new Intent(getApplicationContext()
,test.class));
overridePendingTransition(0,0);
return true;
case R.id.setting:
startActivity(new Intent(getApplicationContext()
,setting.class));
overridePendingTransition(0,0);
return true;
}
return false;
}
});
}
}
menu_navigation.xml
<?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"
android:icon="#drawable/ic_home"/>
<item
android:id="#+id/about"
android:title="About"
android:icon="#drawable/ic_about"/>
<item
android:id="#+id/test"
android:title="Test"
android:icon="#drawable/ic_test"/>
<item
android:id="#+id/term"
android:title="Term"
android:icon="#drawable/ic_term"/>
<item
android:id="#+id/setting"
android:title="Setting"
android:icon="#drawable/ic_setting"/>
</menu>
Put this anywhere in your MainActivity
invalidateOptionsMenu();
Override
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.home);
if (item.getTitle().equals("Home")) {
item.setTitle("Your new title"); }
return super.onPrepareOptionsMenu(menu);
}

How to use Setting's in overflow Icon

I tried my level of best but I couldnt find it.I am working in navigation drawer activty where I can see a overflow Icon in top right when I click it, a Settings button like thing pops out when I click it(Settings) nothing happens
I dont no how to assing an XML to this so that when it is clicked a new activty should open
I know to create an xml and also to assign a onClickListner to the button but i am unable to proceed further since I dont no where to call the setting activty when that button(Settings button in overflow Icon) is pressed
Try this -
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
Intent intent = new Intent(this, YourSettingfActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
menu_main.xml -
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
</menu>
You can use find these below methods in activity and there you can inflate the menu and do want you want
Menu xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
<item android:id="#+id/action_search"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
<item android:id="#+id/action_logout"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
</menu>
and you have to write the code like below in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_search:
Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

how to add new item to setting menu at Android?

i need to add facebook like at setting menu as below image .so how to add new item to setting menu I tried to solve this issue only i had found the setting menu at res > values >string.xml > settings menu .
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<menu>
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Settings"/>
<item
android:id="#+id/action_about"
android:orderInCategory="2"
android:showAsAction="never"
android:title="About"/>
<item
android:id="#+id/action_exit"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Exit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/web_engine"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
Open '/res/menu/menu.xml'
Add this code in it:
<item
android:id="#+id/action_about"
android:orderInCategory="2"
android:showAsAction="never"
android:title="About"/>
<item
android:id="#+id/action_exit"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Exit"/>
Open '/src/(packagename)/(acitivityname).java'
Add this code there
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_about:
// About option clicked.
return true;
case R.id.action_exit:
// Exit option clicked.
return true;
case R.id.action_settings:
// Settings option clicked.
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Android option menu: change size of icon

I am using option menu, i want to increase the size of my icon...
means icon are too small... how can increase their width or height ....
or how remove space around icon.. i.e padding, margin etc...
i want a menu with icon at bottom...
what i should do???
<activity
android:name="com.example.parentengagementtracking.BaseActivity"
android:label="#string/title_activity_base"
android:theme="#style/MenuTheme" >
<!-- android:theme="#style/Theme.FixedSize"> -->
</activity>
<style name="MenuTheme" parent="android:Theme">
<item name="android:panelFullBackground">#drawable/back11</item>
<item name="android:padding">1dip</item>
<item name="android:drawablePadding">1dip</item>
</style>
public class BaseActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_parent_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
Bundle objBundle;
Intent objIntent;
switch (item.getItemId())
{
case R.id.menu_about_us:
objIntent=null;
objIntent = new Intent(BaseActivity.this,InfoActivity.class);
objBundle=new Bundle();
objBundle.putString("source","BaseActivity");
objIntent.putExtras(objBundle);
finish();
startActivity(objIntent);
return true;
case R.id.menu_profile:
objIntent=null;
objIntent = new Intent(BaseActivity.this,UserProfile.class);
finish();
startActivity(objIntent);
return true;
case R.id.menu_activity:
objIntent=null;
objIntent = new Intent(BaseActivity.this,ViewActivity.class);
finish();
startActivity(objIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
<!-- android:icon="#drawable/homen" -->
<item
android:id="#+id/menu_profile"
android:orderInCategory="2"
android:showAsAction="always"
android:icon="#drawable/profile1"
android:title="Profile"/>
<item
android:id="#+id/menu_activity"
android:orderInCategory="3"
android:showAsAction="always"
android:icon="#drawable/reports"
android:title="Reports"/>
<item
android:id="#+id/menu_about_us"
android:icon="#drawable/aboutusn"
android:orderInCategory="4"
android:showAsAction="always"
android:title="Aboutus"/>
how can i set padding, margin, width and height etc of icon..??
Your image should be 24*24 or 36*36.
You can solve this problem by using "app:actionLayout".
<item
android:id="#+id/menu1"
app:actionLayout="#layout/custom_action_bar" //this line will change icon
android:visible="true"
android:title="Menu 1"
app:showAsAction="always" />
Refer this docs Custom menu icons to add listeners to custom menu.

Duplicate Menu Items Showing Up and One Not Showing at All; Crashing as well

Okay, so I've been doing pretty well with developing my Android App but then I run into this issue. I tried to make another Menu Item to an Option Menu which I've never had problems with before but now, as described in the question, one of the items is showing up twice and another isn't showing up at all and then when I enter the Credits activity it crashes. Below is the codes for the res/menu/menu.xml And MainActivity.java (first one)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu); return true; }
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.quit:
return true;
case R.id.new_game:
Intent intent = new Intent(this, New_Game.class);
startActivity(intent);
return true;
case R.id.visit_site:
Intent inten = new Intent(this, Site.class);
startActivity(inten);
return true;
case R.id.stay:
Intent inte = new Intent(this, MainActivity.class);
startActivity(inte);
return true;
case R.id.credits:
Intent i = new Intent(this, Credits.class);
startActivity(i);
return true;
case R.id.exit:
Intent in = new Intent(Intent.ACTION_MAIN);
in.addCategory(Intent.CATEGORY_HOME);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);
finish();
System.exit(0);
return true;
}return false;
}
Here is the XML Menu
<menu
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/visit_site"
android:title="Visit the Medieval Site!" />
<item
android:id="#+id/quit"
android:title="Exit">
<item
android:id="#+id/credits"
android:title="Credits"
/>
<menu
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/stay"
android:title="Cancel"
/>
<item
android:id="#+id/exit"
android:title="Exit" />
</menu>
</item>
<item
android:id="#+id/new_game"
android:title="Start the Game"
/>
</menu>
I set up the credits as just a normal "Hello World" Activity in the Java file for it Here is the Android Manifest declaration for the activity. I'm including this just in the case that the error was in there by chance.
<activity android:name=".Credits"
android:label="Application Credits"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape" >
</activity>
If anybody could please tell me where I messed up and what I need to do tof ix it.
Here is the Credits.java asked by Sam below
package com.apw.games.rpg.medieval;
import android.app.*;
import android.os.*;
import android.view.*;
public class Credits extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.credits);
}
}
Your menu layout is incorrect. The problem is "Credits", you cannot have nested items: <item><item></item></item>. Either move "Credits" into the submenu or move it into the regular menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/visit_site"
android:title="Visit the Medieval Site!"/>
<item
android:id="#+id/quit"
android:title="Exit">
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/stay"
android:title="Cancel"/>
<!-- I moved credits here -->
<item
android:id="#+id/credits"
android:title="Credits"/>
<item
android:id="#+id/exit"
android:title="Exit"/>
</menu>
</item>
<item
android:id="#+id/new_game"
android:title="Start the Game"/>
<!-- Or you can move credits here -->
</menu>
Notice the layers are <menu> <item> <menu> <item> </item> </menu> </item> </menu>.

Categories

Resources