how to add new item to setting menu at Android? - 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);
}
}

Related

how to add checkboxes in navigation drawer as a menu items in android with custom styles

I have created widgets_chkboxes file in layout
In menu create widgets.xml file
enter code here
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Filtered"/>
<item android:title="SortedBy">
<menu>
<item
app:actionLayout="#layout/widgets_checkboxes"
android:title="Low To High"/>
<item
app:actionLayout="#layout/widgets_checkboxes"
android:title="High To Low"/>
</menu>
</item>
</menu>
try this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_check"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item
android:id="#+id/action_check2"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item
android:id="#+id/action_check3"
android:title="#string/action_check"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
</menu>
Now you can set it in the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.action_check);
item.setChecked(isChecked);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_check) {
if(item.isChecked());{
// your code or action
}
}
return super.onOptionsItemSelected(item);
}

Issue with adding a Rate me button in the action bar

At first I coded the rate me button as part of the option menu and it worked nicely.
Now I'd like to put more attention to the rate me function. So I added an icon to RateMe menu main.xml. But when I run the app the rate me button is still in the menu and doesn't appear as a button on its own.
What am I missing?
This is my menu main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/settings"
android:icon="#mipmap/action_search"
android:title="#string/settings"
android:showAsAction="ifRoom|withText"
/>
<item android:id="#+id/RateMe"
android:icon="#mipmap/ic_action_important"
android:title="#string/RateMe"
android:showAsAction="always"
/>
<item android:id="#+id/about"
android:icon="#mipmap/action_search"
android:title="#string/about"
android:showAsAction="ifRoom|withText" />
</menu>
It shows correctly in studio:
Here is my mainactivity code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(id.settings).setVisible(!drawerOpen);
menu.findItem(id.about).setVisible(!drawerOpen);
menu.findItem(id.RateMe).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent i;
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch(item.getItemId()) {
case id.settings:
i = new Intent(this, Settings.class);
startActivity(i);
return true;
case id.about:
i = new Intent(this, About.class);
startActivity(i);
return true;
case id.RateMe:
i = new Intent(this, RateMe.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here is my main layout file:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
and the rate me file (empty):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_rate_me"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.bernard_zelmans.checksecurity.Activities.RateMe">
</RelativeLayout>
Add xmlns:app="http://schemas.android.com/apk/res-auto" belowxmlns:android="http://schemas.android.com/apk/res/android"
and replace all android:showAsAction= with app:showAsAction=
like :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/settings"
android:icon="#mipmap/action_search"
android:title="#string/settings"
app:showAsAction="ifRoom|withText"
/>
<item android:id="#+id/RateMe"
android:icon="#mipmap/ic_action_important"
android:title="#string/RateMe"
app:showAsAction="always"
/>
<item android:id="#+id/about"
android:icon="#mipmap/action_search"
android:title="#string/about"
app:showAsAction="ifRoom|withText" />
</menu>
Hope this helps.
I am not sure if this will help you. Try to add app:showAsAction="always|collapseActionView" to your item.
<item android:id="#+id/RateMe"
android:icon="#mipmap/ic_action_important"
android:title="#string/RateMe"
app:showAsAction="always|collapseActionView" />

Android Menu does not inflate

I am pulling my hairs out in trying to get menu to infate on my main activity but to no avail.
I am using Android Studio and the design view showed that the XML menu items are rightfully defined.
However, when I run the codes either in the phone(running Android 4.4.2) or emulator, the menu items are not showing up. Nothing happens when I press on the menu.
Any enlightenment will be most welcome.
Menu XML - my.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1" android:title="Option1"></item>
<item android:id="#+id/item2" android:title="Option2"></item>
<item android:id="#+id/item3" android:title="Option3"></item>
<item android:id="#+id/item4" android:title="Option4"></item>
<item android:id="#+id/item5" android:title="Option5"></item>
<item android:id="#+id/item6" android:title="Option6"></item>
</menu>
Activity Class - myActivity.java
package com.example.mymenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.widget.Toast;
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, " " +
"Option1", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Option2", Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(this, "Option3", Toast.LENGTH_SHORT).show();
return true;
case R.id.item4:
Toast.makeText(this, "Option4", Toast.LENGTH_SHORT).show();
return true;
case R.id.item5:
Toast.makeText(this, "Option5", Toast.LENGTH_SHORT).show();
return true;
case R.id.item6:
Toast.makeText(this, "Option6", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Layout File
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I have solved the problem.
My phone has a menu button, and pressing that button will display the menu list.
I tried with a newer phone that does not have the menu button, and the menu shows up within the action bar.
Regards
I think the problem is you must have created my.xml in layout folder .Create my.xml in menu folder.
Cross check if the menu.xml is in menu folder and not any other res folder like layout.
Trying to inflate it in action bar?
If yes, then try using the following code in the menu.xml
android:showAsAction="always"
Refer : http://developer.android.com/guide/topics/ui/menus.html
Hi try this if you want to display it in Actionbar.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:icon="#drawable/action_overflow"
app:showAsAction="always"/>
<menu>
<item android:id="#+id/item1" android:title="Option1"></item>
<item android:id="#+id/item2" android:title="Option2"></item>
<item android:id="#+id/item3" android:title="Option3"></item>
<item android:id="#+id/item4" android:title="Option4"></item>
<item android:id="#+id/item5" android:title="Option5"></item>
<item android:id="#+id/item6" android:title="Option6"></item>
</menu>
</menu>

Action Overflow is not working

I have tried everything that i could have. gone through 6-7 articles except http://developer.android.com/ and things totally messed up i don't know what i am missing or taking the reference of wrong articles.
Here is my code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/userIcon"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/icon_user"
android:title="#string/app_name"/>
<item
android:id="#+id/listIcon"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/icon_list"
android:title="#string/app_name"/>
<item
android:id="#+id/menu_red"
android:orderInCategory="1"
android:showAsAction="ifRoom|withText"
android:title="#string/get_back"/>
<item
android:id="#+id/menu_green"
android:orderInCategory="2"
android:showAsAction="ifRoom|withText"
android:title="#string/get_login"/>
</menu>
Activity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_product_list, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.listIcon:
Toast.makeText(this,
"Menu list",
Toast.LENGTH_LONG).show();
break;
case R.id.userIcon:
Toast.makeText(this,
"User Icon",
Toast.LENGTH_LONG).show();
break;
}
//return super.onOptionsItemSelected(item);
return true;
}
Thanks a lot
Order in category determines the order appearance, from low to high check this link -> Set Item postition in ActionBar try the following code, but i'm not sure about in which order you want the menus to appear
<item
android:id="#+id/userIcon"
android:orderInCategory="1"
android:showAsAction="always"
android:menuCategory="system"
android:icon="#drawable/icon_user"
android:title="#string/app_name"/>
<item
android:id="#+id/listIcon"
android:orderInCategory="2"
android:showAsAction="always"
android:menuCategory="system"
android:icon="#drawable/icon_list"
android:title="#string/app_name"/>
<item
android:id="#+id/menu_red"
android:orderInCategory="99"
android:showAsAction="ifRoom|withText"
android:title="#string/get_back"/>
<item
android:id="#+id/menu_green"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:title="#string/get_login"/>

Menu option in android

After referring a lot of tutorials I came to know that instead of Menu they have ActionBar for > API 10. But i am using API 7 sdk for my testing, I have used Menus to show text with drawable images. But only the text is coming and the drawable icon image is not showing in the menu option. Please help me to solve this.
My XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!--
Single menu item
Set id, icon and Title for each menu item
-->
<item
android:id="#+id/savedstory"
android:background="#000000"
android:minHeight="20dp"
android:title="Saved Stories"/>
<item
android:id="#+id/setting"
android:background="#000000"
android:minHeight="20dp"
android:title="Settings"/>
<item
android:id="#+id/Bookmark"
android:background="#000000"
android:minHeight="20dp"
android:title="Bookmark This"/>
<item
android:id="#+id/share"
android:background="#000000"
android:minHeight="20dp"
android:title="Share This"/>
<item
android:id="#+id/save"
android:background="#000000"
android:minHeight="20dp"
android:title="Save This"/>
<item
android:id="#+id/small"
android:icon="#drawable/font3"
android:minHeight="20dp">
This icon is not showing.
/>
<item
android:id="#+id/medium"
android:background="#ffffff"
android:minHeight="20dp"
android:title="Medium font"/>
<item
android:id="#+id/big"
android:background="#000000"
android:minHeight="20dp"
android:title="Big font"/>
</item>
</menu>
My inflating code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.newsdescriptionmenu, menu);
return true;
}
If you refer to Menu documentation"
Options menus: The icon menus do not support item check marks and only
show the item's condensed title. The expanded menus (only available
if six or more menu items are visible, reached via the 'More' item in
the icon menu) do not show item icons, and item check marks are
discouraged.
Since I cannot see how you inflate (what options, etc) your menus I can only assume that you don't see this item's icon as it is a sixth item and hits the expanded menu after 'More'.
Please check the following code snippet.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/Menu1"
android:orderInCategory="1"
android:title="Menu 1"/>
<item
android:id="#+id/Menu2"
android:orderInCategory="2"
android:title="Menu 2"/>
<item
android:id="#+id/Menu3"
android:orderInCategory="3"
android:title="Menu 3"/>
<item
android:id="#+id/submenu"
android:orderInCategory="4"
android:title="Sub menu">
<menu>
<item
android:id="#+id/submenu1"
android:title="Sub menu 1"/>
<item
android:id="#+id/submenu2"
android:title="Sub menu 2"/>
</menu>
</item>
</menu>
Add these lines in your Activity Class
public class MenuActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplication()).inflate(R.menu.menu, menu);
return(super.onPrepareOptionsMenu(menu));
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Menu1:
Toast.makeText(this, "Menu 1", Toast.LENGTH_SHORT).show();
break;
case R.id.Menu2:
Toast.makeText(this, "Menu 2", Toast.LENGTH_SHORT).show();
break;
case R.id.Menu3:
Toast.makeText(this, "Menu 3", Toast.LENGTH_SHORT).show();
break;
case R.id.submenu:
Toast.makeText(this, "Sub menu", Toast.LENGTH_SHORT).show();
break;
}
return(super.onOptionsItemSelected(item));
}
}

Categories

Resources