My requirement is to open a menu when edittext is clicked with options "bold", "italic", "Underline", "Fonts" and "Colors".
Please can someone help me on this..
Here's how I ended up solving this:
EditText menuEdit = (EditText) activity.findViewById(R.id.menuImageView);
menuEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
activity.openOptionsMenu(); //This is the key method!
}
});
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.demographics:
return true;
case R.id.settings:
Log.v("v", "settings clicked");
return true;
default:
return false;
}
}
Related
I used a library recently that Provides a Floating Action Button and changing to a Something like Navigation bar by clicking on it.
but i cant set click Event for my navigation bar Items.
after Pressing on items i don't see any reaction!
Anyone can help me?
fabOptions = (FabOptions) findViewById(R.id.fabcontainer);
fabOptions.setButtonsMenu(R.menu.menu1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
newBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void newBack() {
Toast.makeText(this, "back", Toast.LENGTH_SHORT).show();
}
}
First of all you should have to use break whenever you use switch statement otherwise the program will go to default status. so use break; the code below.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getId(); // Retrieve the id of the selected menu item
switch(id) {
case R.id.back:
newBack();
return true;
break;
case ...
}
return super.onOptionsItemSelected(item);
}
Faced with impossibility to change toolbar button icon from AlertDialog. ("Cannot resolve symbol").
Tried to declare toolbar button in other places in a code and got no result. Please help to solve it. How should I declare this button to avoid this error?
//Menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem mChangeCur = menu.findItem(R.id.change_cur);
mChangeCur.setIcon(R.drawable.usd);
MenuItem mSearch = menu.findItem(R.id.action_search);
SearchView mSearchView = (SearchView) mSearch.getActionView();
mSearchView.setQueryHint("Search");
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.change_cur:
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose currency");
// add a list
String[] currencies = {"USD", "RUB", "AUD", "BRL", "CAD"};
builder.setItems(currencies, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
cur = "USD";
new Refresh().execute();
mChangeCur.setIcon(R.drawable.rub); //Here is mistake
break;
case 1:
cur = "RUB";
new Refresh().execute();
mChangeCur.setIcon(R.drawable.rub); //Here is mistake
break;
case 2:
case 3:
case 4:
default:
break;
}
}
});
Problem was in declaring and initialization mChangeCur. I had to declare mChangeCur on class level and initialize in public boolean onCreateOptionsMenu(Menu menu)
I am trying to disable programmatically a menu item if WebView class is not activated.
I tried this code:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.itemRefresh);
if (WebViewFragment.viewContentWebView.isActivated()){
item.setVisible(true);
item.setEnabled(true);
} else
{
item.setVisible(true);
item.setEnabled(false);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.itemRefresh:
WebViewFragment.viewContentWebView.reload();
return true;
case R.id.itemAdd:
//oke im gonna write here
return true;
case R.id.itemHelp:
// ok ok i will
return true;
default:
return true;
}
}
In Eclipse I don't get any error msg but on real device App blocks itself.
I think I'm doing something wrong in IF-ELSE statement part.
Thank you.
In my app i have an optionsmenu. It has 2 buttons. Depending on a boolean value i would like to show/hide one of the buttons. I've got the following code but it doesn't hide the button. How can i do this?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menushowmoredetails, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(displayRotaDetails.equalsIgnoreCase("false")){
if(item.getItemId() == R.id.moredetails)
item.setVisible(false);
}
switch (item.getItemId()) {
case R.id.back:
onBackPressed();
return true;
case R.id.moredetails:
You have to use the onPrepareOptionMenu method like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// add your conditions here and change 0 with the R.id.moredetails item postion.
if(displayRotaDetails.equalsIgnoreCase("false")){
menu.getItem(1).setVisible(false);
}
}
I use phonegap,
When linking to another page with simple href my menu stop working.
In my Java class I override onKeyDown() and return false for menu button
#Override
public boolean onKeyDown(int i,KeyEvent e){
if (i==KeyEvent.KEYCODE_MENU){
return false;
}else{
return true;
}
}
When I loadUrl from the java class I can open the menu (in about.html)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.exit:
this.finish();
return true;
case R.id.about:
super.loadUrl("file:///android_asset/www/about.html");
return true;
default:
return super.onOptionsItemSelected(item);
}
}