How to set menu background in android 2.3 - java

I'm trying to set a color background to my welcome menu. I looked for a solution but I didn't find one good for me. I'm working with android 2.3 API
my welcome_menu code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_preferiti"
android:orderInCategory="100"
android:title="#string/menu_preferiti"
android:icon="#drawable/menu_preferiti_pressed"/>
<item
android:id="#+id/menu_sicuro_bici"
android:orderInCategory="100"
android:title="#string/menu_navigazionesicura"
android:icon="#drawable/menu_sicuro_bici_pressed"/>
<item
android:id="#+id/menu_credits"
android:orderInCategory="100"
android:title="#string/menu_credits"
android:icon="#drawable/menu_credits_pressed"/>
WelcomeActivity code to create menu
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
Intent intent = null;
switch (item.getItemId())
{
case R.id.menu_preferiti:
// CAMBIARE LA QUERY!!! METTERE QUELLA DEL RECUPERO DEI PREFERITI CON JOIN!!!
daos = new DAOService(this);
ArrayList<Itinerario> listafav = daos.doRetriveAllItinerari();
intent = new Intent(WelcomeActivity.this, FavoriteActivity.class);
intent.putExtra("lista_favoriti", listafav);
startActivity(intent);
return true;
case R.id.menu_sicuro_bici:
intent = new Intent(WelcomeActivity.this, NavigazioneSicuraActivity.class);
startActivity(intent);
return true;
case R.id.menu_credits:
intent = new Intent(WelcomeActivity.this, CreditsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Maybe a solution is to create a style for menu, but I don't know how to do it.

That's not possible. However you can display your own custom menu and handle it with onKeyDown()

Related

How to use Radio button in menu to change text and background colour of a textview

basically I am trying to create a night mode of white text on black background on radio button click. I would like the radio button to be gray when not clicked and green when clicked/active. I have been able to make the item toast on click but lost from here. I have attached a screenshot below. No idea what to do next. Thanks for your help.
Night mode gray image
Code in Activity
enter code here #Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_text, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.item2) {
Toast.makeText(this, "Night mode", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.item3) {
Toast.makeText(this, "Fonts", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.font1) {
Toast.makeText(this, "Font 1", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.font2) {
Toast.makeText(this, "Font 2", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.font3) {
Toast.makeText(this, "Font 3", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu 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">
<item android:title="#string/settings"
app:showAsAction="always">
<menu>
<item android:id="#+id/item2"
android:title="#string/night_mode"
android:icon="#drawable/ic_night"
app:showAsAction="withText" />
<item android:id="#+id/item3"
android:title="#string/font"
app:showAsAction="never" >
<menu>
<item android:id="#+id/font1"
android:title="Times Roman"/>
<item android:id="#+id/font2"
android:title="Vollkorn"/>
<item android:id="#+id/font3"
android:title="Default"/>
</menu>
</item>
</menu>
</item>
</menu>
You can use the official AppCompatDelegate.setDefaultNightMode(MODE) where MODE stands for one of these scenarios:
AppCompatDelegate.MODE_NIGHT_YES
AppCompatDelegate.MODE_NIGHT_NO
AppCompatDelegate.MODE_NIGHT_AUTO
For your project, you will need to switch between MODE_NIGHT_NO and MODE_NIGHT_YES because MODE_NIGHT_AUTO will change the App Theme depending on the time.
When you hit the radio button you have to switch the mode and recreate the activity, calling activity.recreate()
For more information, there is a useful article here: DayNight Theme Android Tutorial with Example

Implement onClick on submenu to start new activity

I'm building an app which has menu item in toolbar, i have created submenu for one menu i.e whose id is 'cloud'. I would like to implement onclick on each submenu when click will open different activity.
Here is the menu.xml file
<?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/action_refresh"
app:showAsAction="ifRoom"
android:title="Refresh"
android:icon="#drawable/ic_loop_black_24dp"></item>
<item
android:id="#+id/notes"
app:showAsAction="ifRoom"
android:title="Refresh"
android:icon="#drawable/ic_event_note_black_24dp"></item>
<item
android:id="#+id/cloud"
app:showAsAction="ifRoom"
android:title="Refresh"
android:icon="#drawable/ic_cloud_upload_blackt_24dp">
<menu>
<group
android:checkableBehavior="single">
<item android:id="#+id/imageee"
android:title="Image Upload"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
<item android:id="#+id/pdfff"
android:title="Pdf Upload"
android:orderInCategory="100"
app:showAsAction="never"/>
</group>
</menu>
</item>
</menu>
Here is the Mainactivity file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
mymenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_refresh:
Intent intent = new Intent(MainActivity.this, UpdateService.class);
startService(intent);
}
return true;
case R.id.notes:{
Intent activity_weather = new Intent(this,Physics.class);
startActivity(activity_weather);
}
return true;
case R.id.cloud:{
}
return true;
}
return super.onOptionsItemSelected(item);
}
I looked for answer and tried on my own but I have no idea how to achieve this.
Any help is appreciated.
Thank you in advance.
I have a solution for you:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
// TODO:
toast("refresh");
return true;
case R.id.notes:
// TODO:
toast("notes");
return true;
case R.id.imageee:
// TODO: Start your image upload
toast("imageee");
return true;
case R.id.pdfff:
// TODO: Start your PDF upload
toast("pdfff");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void toast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
I write toast method to show a toast when users click on an item on menu or sub menu. Please remove //TODO: line and add your code for each case there.
I don't think you want to have
android:checkableBehavior="single"
set on your submenu. This behaviour is only useful if you just want to capture the choice (or choices) in the menu item, but not if you actually want to react (ie: do something) when the user selects a submenu item.
Based on your question, it sounds like you want to launch another Activity when the user selects one of these submenu items. To do that just add those menu items to the switch statement in onOptionsItemSelected():
case R.id.imageee:
Intent imageIntent = new Intent(this, Upload.class);
startActivity(imageIntent);
return true;
case R.id.pdfff:
Intent pdfIntent = new Intent(this, Pdf.class);
startActivity(pdfIntent);
return true;
Another interesting solution could be:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
switch (item.getItemId()) {
case R.id.action_refresh:
intent.setClass(this, UpdateService.class);
break;
case R.id.notes:
intent.setClass(this, Physics.class);
break;
case R.id.cloud:
intent = null;
break
case R.id.imageee:
// TODO set intent class
break;
case R.id.pdfff:
// TODO set intent class
break;
}
// If the selected item is the one that opens the submenu does not try to start the
// activity avoiding throwing null pointer exception and consequently opens the submenu
if (!(intent == null))
startActivity(intent);
return super.onOptionsItemSelected(item);
}
That way you avoid having to have multiple times, in all cases the same call to the method.
startActivity()
Saving lines of code. It is even more advantageous if you decide to add more items to the menu.

Action bar option action

Before Clicking:
After Clicking:
I am having an issue with my code where clicking on an actionbar action will bring up the title of the action. If you then click on the title that is how you get to the onclick listener. Very strange... I know it is hard to tell without code but I am away and will post it soon. Just wanted to ask around to see if anyone has experienced similar.
ActionBar:
<?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/action_settings"
android:title="#string/hello1"
app:showAsAction="never"/>
</menu>
onClickListener:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_settings:
onCreateDialog();
return true;
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onCreateDialog() {
String[] string= new String[]{"Add to Calendar", "Share"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Options")
.setItems(string, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:...................}
This is the expected behavior for the way you have the menu setup currently.
<item
android:id="#+id/action_settings"
android:title="#string/hello1"
app:showAsAction="never"/> <!-- Don't show this action until the overflow menu is shown -->
With the showAsAction set to never, the action is not shown until the overflow menu is shown. The available options for showAsAction are:
["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
In your case, because you only have one menu item, you can use always.
Refer to the menu resource documentation for more info.
You should be able to preview this in Android Studio (see screenshot below)
I fixed it by extending the activity to appcompatactivity instead of activity. Weird bug. Then it didn't do it.

Adding second button in Action Bar

I'm trying to add a second button to my action bar. I have one drawable that opens a new activity. I want to add a second on in onOptionsItemSelected. Here is my code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.new_account:
Intent intent = new Intent(getActivity(), AddAccountActivity.class);
this.startActivity(intent);
break;
switch (item.getItemId()) {
case R.id.action_settings:
Intent myIntent = new Intent(getActivity(), SettingsActivity.class);
this.startActivity(myIntent);
break;
}
return super.onOptionsItemSelected(item);
}
}
I get an error asking in the last closing bracket saying, "Missing return statement." I am new to android development so I'm guessing it is something simple. Thanks.
Edit: Here is my main.xml in my menu folder
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.myapplication2.app.MainActivity" >
<item android:id="#+id/new_account"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="always" />
I haven't changed any lines of the settingsActivity.class. I just created a new activity with the "settings" mode.
If you want add menuItem, you must add item in xml.
Project-res-menu has a xml files.
There is already has a 1 item.
Try add new Item and set attribute, I think you show a two menu.
You are getting "Missing return statement" because you don't have a return statement in switch-case.
For ex:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.settings:
setSettings();
return true;
case R.id.second:
setColor();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

How do I make menu item clickable and display a toast after clicked android?

Hello I having a problem trying to get eclipse to recognized my menu icon. I want to have the icon appear in the action bar and when clicked, I want to display a toast.The problem is that my save icon is not being recognized in eclipse. Here is my code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.profilemenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case android.R.id.home:
finish();
case android.R.id.save:
save cannot be resolved or is not a field
}
return false;
}
The menu :
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/save" android:showAsAction="ifRoom"
android:title="#string/save_str" android:icon="#drawable/content_save" />
</menu>
You're using the incorrect identifier within the switch statement, you should use just R.id.Save, like so:
switch(item.getItemId()) {
case R.id.home:
finish();
case R.id.save:
Toast.makeText(getContext(), "Toast message", Toast.LENGTH_SHORT).show();
}
Using the android identifier means you are trying to find a resource built into the android sdk which doesn't exist.

Categories