Toast not showing - android

I want to show Toast on actionItem() function inside. But it doesn't now showing Toast.
If I call
Context context = getApplicationContext();
Before actionItem() then the app crashes.
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class ShowWebViewActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_web_view);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_show_web_view, 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.action_settings) {
// return true;
// }
switch (item.getItemId()) {
case R.id.action_settings:
actionItem();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// Context context = getApplicationContext() makes app to crash why??
public void actionItem(){
// Toast function should call here. But not working
Context context = getApplicationContext();
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG);
}
}

It should be,
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();

Show() is missing from your code. Kindly refer to my code:
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();

Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG);
should be:
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();
You would have got an IDE warning also for this

You missed show()
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG);
it should be:
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();

show() is missing use the following code
public void actionItem(){
// Toast function should call here. But not working
Context context = getApplicationContext();
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();
}

You missed the show() function.
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();

Related

onMenuItemClick isn't being called - Android

I have google map's activity, and a button that I want to inflate a popup menu.
This is the class:
public class NewMissionMapMainUser extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener, OnMapReadyCallback, GoogleMap.OnMarkerClickListener, GoogleMap.OnMyLocationButtonClickListener {
And this is the onClick method that is called when my button is pushed:
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
}
And that is the Code that is not being called.
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_comedy:
Toast.makeText(this, "Comedy Clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item_movies:
Toast.makeText(this, "Movies Clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item_music:
Toast.makeText(this, "Music Clicked", Toast.LENGTH_SHORT).show();
return true;
default:
Toast.makeText(this, "Yo", Toast.LENGTH_SHORT).show();
return false;
}
}
I've tried this link:
(Deprecated) Fragment onOptionsItemSelected not being called
and setHasOptionsMenu(true);
But i've changed the fragment to extent AppCompatActivity in order the action bar to be visible, so the line "setHasOptionsMenu(true);" can't be resolved.
Does someone knows how to fix this ? Thanks
Since you're instantiating a PopupMenu dynamically, you need to provide it with the listener that will receive the click events. You should call popup.setOnMenuItemClickListener(this), assuming this is your Activity or Fragment.

How can i change background on item click in overflow?

So in my MainActivity, I can change the background of the layout by clicking on the overflow in my actionbar and selecting a background.
But how can I change the layout across all other activities?
This is the code i have now
private int selectedBackgroundId = R.id.defaultTheme;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.defaultTheme:
mainLayout.setBackgroundResource(R.drawable.defaultbackground);
if (selectedBackgroundId == R.id.defaultTheme){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.defaultTheme;
return true;
case R.id.background1:
mainLayout.setBackgroundResource(R.drawable.redpinkgradientbackground);
if (selectedBackgroundId == R.id.background1){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.background1;
return true;
Second question i have
How can I save the layout that has been set when app is destroyed?
So when I reopen the app, it's still the layout I clicked in the overflow menu.

How can I add a menu dynamically to bottom navigation view?

Android has new ui element - BottomNavigationView
I don't want to contain my menus in the xml files. I will receive the information about menu items and order from backend side. I want to create them dynamically and set into the BottomNavigationView in the onCreate() method. Can I do this?
By default, BottomNavigationView starts with an empty menu. You can use the getMenu() method to get the Menu instance, then add menu items as in the response above. For example,
BottomNavigationView bottomNavigation = findViewById(R.id.bottom_navigation);
Menu menu = bottomNavigation.getMenu();
menu.add(Menu.NONE, MENU_ITEM_ID_ONE, Menu.NONE, getString(R.string.str_menu_one))
.setIcon(R.drawable.ic_action_one);
The easy way to use dynamic options in bottom navigation view is use different menu items like this:
switch (userType){
case UserTypes.A:
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_a);
break;
case UserTypes.B:
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_b);
break;
case UserTypes.C:
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_c);
break;
}
Heres an example of dynamic build of menu items, in the main activity u have 2 layouts.
you can get your own layout from your backend and create menu items dynamically using menu.add
src: http://www.mobiledevguide.com/2014/01/dynamically-create-menu-items-in-android.html
public class MainActivity extends Activity {
private Button mButtonOne,mButtonTwo;
private static final int MENU_ITEM_ID_ONE =1;
private static final int MENU_ITEM_ID_TWO =2;
private static final int MENU_ITEM_ID_THREE =3;
private static final int MENU_ITEM_ID_FOUR =4;
private static final int MENU_ITEM_ID_FIVE =5;
private static final int MENU_ITEM_ID_SIX =6;
private int mMenuSet = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonOne=(Button) findViewById(R.id.buttonSetOne);
mButtonTwo=(Button) findViewById(R.id.buttonSetTwo);
mButtonOne.setOnClickListener(clickListener);
mButtonTwo.setOnClickListener(clickListener);
}
OnClickListener clickListener=new OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId()==R.id.buttonSetOne) {
mMenuSet=1;
} else if (v.getId()==R.id.buttonSetTwo){
mMenuSet=2;
}
invalidateOptionsMenu();
/*
* if you are using ActionBarSherlock use this.supportInvalidateOptionsMenu();
* if you are using ActionBarCompat use invalidateOptionsMenu (Activity activity) method
* */
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
if(mMenuSet==1){
menu.add(Menu.NONE, MENU_ITEM_ID_ONE, Menu.NONE,getString(R.string.str_menu_one)).setIcon(R.drawable.ic_action_one).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, MENU_ITEM_ID_TWO, Menu.NONE,getString(R.string.str_menu_two)).setIcon(R.drawable.ic_action_two).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, MENU_ITEM_ID_THREE, Menu.NONE,getString(R.string.str_menu_three)).setIcon(R.drawable.ic_action_three);
}else if(mMenuSet==2){
menu.add(Menu.NONE, MENU_ITEM_ID_FOUR, Menu.NONE,getString(R.string.str_menu_four)).setIcon(R.drawable.ic_action_four).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, MENU_ITEM_ID_FIVE, Menu.NONE,getString(R.string.str_menu_five)).setIcon(R.drawable.ic_action_five).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, MENU_ITEM_ID_SIX, Menu.NONE,getString(R.string.str_menu_six)).setIcon(R.drawable.ic_action_six);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_ID_ONE:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_one), Toast.LENGTH_SHORT).show();
break;
case MENU_ITEM_ID_TWO:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_two), Toast.LENGTH_SHORT).show();
break;
case MENU_ITEM_ID_THREE:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_three), Toast.LENGTH_SHORT).show();
break;
case MENU_ITEM_ID_FOUR:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_four), Toast.LENGTH_SHORT).show();
break;
case MENU_ITEM_ID_FIVE:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_five), Toast.LENGTH_SHORT).show();
break;
case MENU_ITEM_ID_SIX:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_six), Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings:
Toast.makeText(this, "Click on "+ getString(R.string.action_settings), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}}

How can I change some text in an ActionBar item after an onOptionItemSelected event?

I'm using ActionBar in my application, and I want when the user clicks a button, the item in the ActionBar should change the text.
This is my code for onOptionsItemSelected():
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
.show();
finish();
break;
case R.id.lg:
Toast.makeText(getBaseContext(), "ma", Toast.LENGTH_SHORT).show();
break;
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
break;
case R.id.nerlandais:
Toast.makeText(getBaseContext(), "brahim", Toast.LENGTH_SHORT)
.show();
}
return true;
}
What must I do to change the item title from another item?
Example: When I click in French item I want to change nerlandais item title.
If you want to change an MenuItem's title clicking to another item you can do something like this :
private String mMenuItemTitle;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.nerlandais);
item.setText(mMenuItemTitle);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
mMenuItemTitle = "My New Title";
supportInvalidateOptionsMenu();
break;
}
return true;
}
Try item.setTitle("new title")
Or, if from another item then the Menu has findItem

Handling a Menu Item Click Event - Android

I want to create an intent that starts a new activity once a Menu Item is clicked, but I'm not sure how to do this. I've been reading through the android documentation, but my implementation isn't correct..and some guidance in the right direction would help. I've listed my code below and commented out my problem areas, I think I'm invoking the wrong method.
package com.jbsoft.SimpleFlashlight;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SimpleFlashLightActivity extends Activity {
Button GreenButton; // Declare instances of buttons to use later
Button BlueButton;
private static final int OK_MENU_ITEM = Menu.FIRST;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BlueButton = (Button) findViewById(R.id.bluebutton);
BlueButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Display msg when user clicks Blue Button
showColorChangeMsg();
// Switch Activities on click
Intent blueintent = new Intent(SimpleFlashLightActivity.this,
BlueFlashLightActivity.class);
startActivity(blueintent);
}
});
//Install listener for second button
GreenButton = (Button) findViewById(R.id.greenbutton);
GreenButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Display msg when user clicks Green Button
showColorChangeMsg();
Intent greenintent = new Intent(SimpleFlashLightActivity.this,
GreenFlashLightActivty.class);
startActivity(greenintent);
}
});
;
/**************************************************************************************/
// Method Declarations // THIS IS WHERE I'M HAVING A PROBLEM
MenuItem AddColorButton = (MenuItem)findViewById(R.id.menu_insert);
boolean onOptionsItemSelected(AddColorButton) {
Intent intent = new Intent(SimpleFlashLightActivity.this,
BlueFlashLightActivity.class);
startActivity(intent);
return true;
;
};
/****************************************************************************************/
}
private void showColorChangeMsg()
{
Toast msgtoast = Toast.makeText(this.getBaseContext(), "SWITCH COLOR!",
Toast.LENGTH_LONG);
msgtoast.show();
}
private void showMsg(String msg) {
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.show();
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case OK_MENU_ITEM:
showMsg("OK");
break;
}
return super.onOptionsItemSelected(item);
}
}
Add Following Code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_item:
Intent i = new Intent(this,SecondActivity.class);
this.startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Menu items file looks like
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=".MainActivity">
<item
android:id="#+id/settings"
android:title="Setting"
app:showAsAction="never" />
<item
android:id="#+id/my_activity"
android:title="My Activity"
app:showAsAction="always"
android:icon="#android:drawable/btn_radio"/>
</menu>
Java code looks like
src/MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.my_activity) {
Intent intent1 = new Intent(this,MyActivity.class);
this.startActivity(intent1);
return true;
}
if (id == R.id.settings) {
Toast.makeText(this, "Setting", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
And add following code to your AndroidManifest.xml file
<activity
android:name=".MyActivity"
android:label="#string/app_name" >
</activity>
I hope it will help you.
in addition to the options shown in your question, there is the possibility of implementing the action directly in your xml file from the menu, for example:
<item
android:id="#+id/OK_MENU_ITEM"
android:onClick="showMsgDirectMenuXml" />
And for your Java (Activity) file, you need to implement a public method with a single parameter of type MenuItem, for example:
private void showMsgDirectMenuXml(MenuItem item) {
Toast toast = Toast.makeText(this, "OK", Toast.LENGTH_LONG);
toast.show();
}
NOTE: This method will have behavior similar to the onOptionsItemSelected (MenuItem item)
This code is work for me
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// add your action here that you want
return true;
}
else if (id==R.id.login)
{
// add your action here that you want
}
return super.onOptionsItemSelected(item);
}
This is how it looks like in Kotlin
main.xml
<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:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_logout"
android:orderInCategory="101"
android:title="#string/sign_out"
app:showAsAction="never" />
Then in MainActivity
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
This is onOptionsItemSelected function
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when(item.itemId){
R.id.action_settings -> {
true
}
R.id.action_logout -> {
signOut()
true
}
else -> return super.onOptionsItemSelected(item)
}
}
For starting new activity
private fun signOut(){
MySharedPreferences.clearToken()
startSplashScreenActivity()
}
private fun startSplashScreenActivity(){
val intent = Intent(GrepToDo.applicationContext(), SplashScreenActivity::class.java)
startActivity(intent)
finish()
}
Replace Your onOptionsItemSelected as:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case OK_MENU_ITEM:
startActivity(new Intent(DashboardActivity.this, SettingActivity.class));
break;
// You can handle other cases Here.
default:
super.onOptionsItemSelected(item);
}
}
Here, I want to navigate from DashboardActivity to SettingActivity.

Categories

Resources