Command of an item from the title bar into onCreate | Android - android

Does anybody knows how to put the code of an item from the titlebar into the onCreate?
The code is still so:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
// Here the code of the item should be
}
// But it's here:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main3, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item1) {
// Command
return true;
}
return super.onOptionsItemSelected(item);
}
Thanks for help!

Related

Textview not resolved inside onOptionsItemSelected(MenuItem item) method

Here is MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myTxt = findViewById(R.id.view);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
myTxt.setText("what a yummy toast!");
return true;
case R.id.nutella:
return true;
case R.id.milk:
default:
return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
}
}
}
Here is the menu XML file
<?xml version="1.0" encoding="utf-8"?>
<item android:title="#string/toast"
android:id="#+id/toast"/>
<item android:title="#string/nutella"
android:id="#+id/nutella"/>
<item android:title="#string/milk"
android:id="#+id/milk"/>
I want to handle myTxt which is a textview whenever I choose an item from the menu option the problem is that myTxt is not recognized. How Should I handle Menus correctly and what are
Here is an image for more explanation
You need to declare your TextView myTxt as global outside onCreate() method
SAMPLE CODE
public class MainActivity extends AppCompatActivity {
TextView myTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTxt = findViewById(R.id.view);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
myTxt.setText("what a yummy toast!");
return true;
case R.id.nutella:
return true;
case R.id.milk:
default:
return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
}
}
}
myTxt is declared inside onCreate() so you can not access it from outside this function. Try declare it outside onCreate()
public class MainActivity extends AppCompatActivity {
private TextView myTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTxt = findViewById(R.id.view);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
myTxt.setText("what a yummy toast!");
return true;
case R.id.nutella:
return true;
case R.id.milk:
default:
return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
}
}
}

hide/show options menu when clicking buttons

I'm using some buttons in my fragments. When i checks those buttons then options menu should display. And when i uncheck it it should hide options menu. How should i do this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mLocation = getArguments().getString(Beco.EXTRA_LOCATION);
listMalls = temporaryModelCache.getDealData().getFacets().getArea();
listCategories = temporaryModelCache.getDealData().getFacets().getCategories();
listGender = temporaryModelCache.getDealData().getFacets().getAgeGroup();
try {
MainActivity activity = (MainActivity) getActivity();
if (activity != null) activity.hideBottomBar();
} catch (Exception ignored) {
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.forgot_password, menu);
mResetButton = menu.findItem(R.id.action_reset);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.action_reset:
Log.d(TAG, "onClick");
resetFilter();
return true;
}
return super.onOptionsItemSelected(item);
}
And this is the event that i need to hide/display options menu
private void checkSelected() {
if (!mapMall.isEmpty() || !mapGender.isEmpty() || !mapCategory.isEmpty()) {
footerTab.setVisibility(View.VISIBLE);
} else {
footerTab.setVisibility(View.GONE);
}
}
When if (!mapMall.isEmpty() || !mapGender.isEmpty() || !mapCategory.isEmpty()) i need to display actions menu and in the else part i need to hide options menu ! How can i achieve this?
call invalidateOptionsMenu() for hide and show option menu
Boolean Isreset= false;
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.forgot_password, menu);
mResetButton = menu.findItem(R.id.action_reset);
if(!Isreset)
{
mResetButton.setVisibility(true);
}else{
mResetButton.setVisibility(false);
}
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.action_reset:
Log.d(TAG, "onClick");
resetFilter();
Isreset= true;
invalidateOptionsMenu();
return true;
}
return super.onOptionsItemSelected(item);
}
You can keep the instance of Menu object and later on use it to invalidate the options menu.
private Menu menu;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mLocation = getArguments().getString(Beco.EXTRA_LOCATION);
listMalls = temporaryModelCache.getDealData().getFacets().getArea();
listCategories = temporaryModelCache.getDealData().getFacets().getCategories();
listGender = temporaryModelCache.getDealData().getFacets().getAgeGroup();
try {
MainActivity activity = (MainActivity) getActivity();
if (activity != null) activity.hideBottomBar();
} catch (Exception ignored) {
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.forgot_password, menu);
mResetButton = menu.findItem(R.id.action_reset);
this.menu = menu;
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.action_reset:
Log.d(TAG, "onClick");
resetFilter();
return true;
}
return super.onOptionsItemSelected(item);
}
Using the menu object then toggle the options menu.
private void checkSelected() {
if (!mapMall.isEmpty() || !mapGender.isEmpty() || !mapCategory.isEmpty()) {
footerTab.setVisibility(View.VISIBLE);
menu.findItem(R.id.action_reset).setVisibility(View.VISIBLE);
} else {
footerTab.setVisibility(View.GONE);
menu.findItem(R.id.action_reset).setVisibility(View.GONE);
}
}

unfortunately, app has stopped on calling actionModeStarted

I am creating a menu to show on top of the screen while longpress in webview.
But I am getting a message "unfortunately, app has stopped", then app closed.
I created mymenu in xml(fn.xml) and placed it in \res\menu\fn.xml
fn.xml file:
<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.fn.MainActivity" >
<item
android:id="#+id/mark"
android:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="Mark">
</item>
</menu>
My MainActivity class code is below
public class MainActivity extends ActionBarActivity {
WebView webview;
private ActionMode mActionMode = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl("http://www.myurl.com");
}
#Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
Toast.makeText(getApplicationContext(), "stateVal",
Toast.LENGTH_SHORT).show();
mActionMode = mode;
Menu menu = mode.getMenu();
mode.getMenuInflater().inflate(R.menu.fn, menu);
}
super.onActionModeStarted(mode);
}
public void onContextualMenuItemClicked(MenuItem item) {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
my Log screen shot
May I know what is missing here?

Menu item call twice in fragment android

In fragment when i click on menu item , it calls function twice one is from previous fragment and other one is from existing fragment.
Fragment 1:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.refresh){
RetryCallMap();
return true;
}else
return false;
}
Fragment 2:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.refresh){
RetryCall();
return true;
}else
return false;
}
MainActivity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.map) {
pushFragments("mapFragment", new MapFragment(), true, true);
return true;
}
if (id == android.R.id.home) {
this.drawerLayout.openDrawer(drawableListView);
return true;
}
return super.onOptionsItemSelected(item);
}
These are the fragments and my MainActivity i am refreshing fragment by on menu item click so that if internet connection not found or any other problem occurs while fetching for i can refresh the page.
Returning true in onOptionsItemSelected() function worked for me.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.refresh){
RetryCall();
return true;
}else
return true;
}

saving the edittext value to another activity on click of save actionbutton in android

how to save the text given to edit text, after clicking the save action button in action bar that should save to another activity.
my first activity Add.java
public class Add extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate=getMenuInflater();
getMenuInflater().inflate(R.menu.activity_add_action, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:save();
return true;
case R.id.cancel:cancelnavi();
return true;
}
return super.onOptionsItemSelected(item);
}
private void save() {
EditText titlename;
titlename=(EditText)findViewById(R.id.edittitle);
String title=titlename.getText().toString();
if (title.equalsIgnoreCase("")){
Toast.makeText(Add.this, "Script title should not be empty", Toast.LENGTH_LONG).show();
} else {
Intent i;
i=new Intent(getApplicationContext(), Scripts.class);
i.putExtra("n", titlename.getText().toString());
startActivityForResult(i, 0);
titlename.setText("");
}
}
}
It should be saved to scripts.java
public class Scripts extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scripts);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate=getMenuInflater();
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:editscript();
break;
}
return true;
}
private void editscript() {
Intent editinIntent;
editinIntent=new Intent(Scripts.this, Edit.class);
startActivity(editinIntent);
}
}
Maybe this can help you.
public class Scripts extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scripts);
getActionBar().setDisplayShowHomeEnabled(false);
//Extract the data
String yourTitle = getIntent().getStringExtra("n");
}
}

Categories

Resources