How to cast Menuitem to Button or imageButton - android

i want to cast Menuitem to button so i can set listener and perform action
i have tried with custom Actionbar where i set imagebutton and perform a task
can anyone have please share it here

#Override
public boolean onCreateOptionsMenu(Menu menu) {
//getSupportMenuInflater() if you are using sherlock action bar
getMenuInflater().inflate(R.menu.urmenuxml, menu);
MenuItem mi = menu.getItem(0);
View anyView = new View();
anyView.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
mi.setActionView(anyView);
}
I hope this helps.

Related

android remove item from actionbar buttonclick

am having success with this code below but its not what i want.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
present.
getMenuInflater().inflate(R.menu.main, menu);
**MenuItem item = menu.findItem(R.id.helpp);
item.setVisible(false);**
return true;
}
this code above works but its not how i want it , i want to add this code on a button click event.
Menu menu;
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MenuItem item = menu.findItem(R.id.helpp);
item.setVisible(false);
}
});
this crashes my app kindly tell me how to do this as i have wasted 9 hours thanks.
First of all, you are probably getting null pointer exception, because you're accessing uninitialized field menu. Except that, Android's Activity class provides method invalidateOptionsMenu() which requests menu to be recreated.
Your activity class might look like:
boolean buttonClicked = true;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (buttonClicked) {
menu.findItem(R.id.helpp).setVisible(false);
}
return true;
}
Button listener:
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
buttonClicked = true;
invalidateOptionsMenu();
}
});

How to click a button to open a customized menu (it's not context menu of the button) in android?

I hope to display a customized menu deleterecord.xml by clicking btnDelete button. It's not a context menu of btnDelete, opening a context menu need long click, so you can't use registerForContextMenu do it.
I have used openOptionsMenu() to open Options Menu while I click btnMore button, I hope to click btnDelete button to open another customized menu (The menu file is deleterecord.xml), how to do? Thanks!
private void SetButtons() {
findViewById(R.id.btnMore).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openOptionsMenu();
}
});
findViewById(R.id.btnDelete).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//How to open deleterecord.xml menu
}
});
}
#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_more, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.MoreShare:
return true;
case R.id.MoreSettings:
return true;
case R.id.MoreAbout:
return true;
}
return super.onOptionsItemSelected(item);
}
This is option menu menu_more.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="info.dodata.unlock.UnlockMain" >
<item android:id="#+id/MoreShare"
android:title="#string/MoreShare" />
<item android:id="#+id/MoreUninstall"
android:title="#string/MoreUninstall" />
<item android:id="#+id/MoreAbout"
android:title="#string/MoreAbout" />
</menu>
This is my customized menu deleterecord.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/ContextDeleteLess"
android:title="Delete 10 records" />
<item android:id="#+id/ContextDeleteMore"
android:title="Delete 20 records"/>
</menu>
You can set a buttons onClick by android:onClick="methodName" in your xml layout under the button. This method should be in your views class and should have a single View argument. You can use this to determine if the view opens one menu or another. If it isn't a button view, make sure to set android:clickable="true".
You can create a menu layout and try opening it as popup.
findViewById(R.id.btnAnoter).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent popup=new Intent(getBaseContext(),Islemler.class);
popup.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(popup);
}
});
This is how I managed to create custom menu window.
What is this other menu?Is it a Dialog with menu options?
However if you want to show Dialog with your options, you can do it like below:
findViewById(R.id.btnAnoter).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.menus);//This is your menu view.
dialog.show();
}
});
You need to store Menu reference in global variable and then inflate the menu again on button click. Below is the code for the same:
public class MainActivity extends Activity {
private Menu menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openMenu1();
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openMenu2();
}
});
}
private void openMenu1() {
menu.clear();
getMenuInflater().inflate(R.menu.main, menu);
openOptionsMenu();
}
private void openMenu2() {
menu.clear();
getMenuInflater().inflate(R.menu.main1, menu);
openOptionsMenu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
menu.clear();
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

How can I click an Android Action Bar item under program control?

PeterH posted the following code:
//initiate the button
button.performClick();
button.setPressed(true);
button.invalidate();
// delay completion till animation completes
button.postDelayed(new Runnable() { //delay button
public void run() {
button.setPressed(false);
button.invalidate();
//any other associated action
}
}, 800); // .8secs delay time
Can the same type of operation be performed for Action Bar items?
Well, you can save MenuItem as a field and then call onOptionsItemSelected(savedMenuItem). But as ActionBar items are MenuItems and not Buttons (of course, if your action bar isn't customized with view like http://www.vogella.com/articles/AndroidActionBar/article.html#actionbar_dynamic). But if your ActionBar is customized with view and that view has a Button, that Button's behaviour can be customized as considered in your code snippet.
Example:
public class MainActivity extends Activity {
MenuItem item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("mytest");
actionBar.setTitle("TESTESTEST");
TextView tView = (TextView) findViewById(R.id.textView1);
tView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(item);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
this.item = item;
Toast.makeText(this, "settings", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
}

Add a clickeable icon in title bar

I want to add a clickable icon on the default title bar looks like
edited with
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add("title is needed");
menuItem.setIcon(R.drawable.call);
return true;
}
Looks like
Override method onCreateOptionsMenu in your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add("title if needed");
menuItem.setIcon(R.drawable.YOUR_ICON_HERE);
// the rest of the code here...
}
In fact if you are planning to use this action bar on old platforms as well (2.x-3.x), you better consider using ActionBarSherlock
Check this code,
public boolean onCreateOptionsMenu(Menu menu) {
// Used to put dark icons on light action bar
menu.add("Search")
.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
Intent search = new Intent(MainActivity.this,
SearchActivity.class);
startActivityForResult(search, 0);
overridePendingTransition( R.anim.righttoleft, R.anim.stable );
return false;
}
}).setIcon(R.drawable.ic_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}

Android menu options doesn't higlight on click/touch

I am displaying menu action bar at the bottom of the screen. when user click/touch any of the menu item, i want to highlight it (i.e. the way button click highlighting happens). i tried onClickListener and ontouchListener but it doesn't highlight.
can someone tell me which porperty/method i have set.
Here is code i am using.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.landing_page_layout);
ActionBar actionBar = getActionBar();
actionBar.show();
// business logic }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items, menu);
item1 = menu.findItem(R.id.menu_option1);
item1.getActionView().setOnTouchListener(new OnTouchListener() {
// logic when user touch menu option1 touch
}});
Thanks
Chintan
Check this section in the documentation: http://developer.android.com/guide/topics/ui/menus.html#options-menu
To set the menu up you do this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
Where R.menu.menu points to your res/menu/menu.xml file. This will load the elements from that file
The options menu is listened to in the same way as regular View's with OnClickListeners and such. Instead you onOptionsItemSelected override like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.item1:
// Do something
return true;
case R.id.item2:
// Do something else
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Categories

Resources