Android options menu button only works after second click - android

I have the following code in my app. There is a login button which works fine on my view. I've overridden the optionsmenu on my view and placed the login code inside a listener attached to the optionsmenu. When i press the optionsmenu login button nothing happens on the first click, but everything works fine on subsequent clicks. Why is this?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menuentryoptionsmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.login:
item.setOnMenuItemClickListener(new OnMenuItemClickListener(){
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
Log.e(TAG, "login clicked from opts menu");
compId = "100";
String theUsername = userName.getText().toString();
thePassword = passwordPin.getText().toString();
String loginType = "1";
String[] params = new String[]{compId, theUsername, thePassword, loginType};
//validate user Asynchonously on background thread
AsyncValidateCarer avc = new AsyncValidateCarer();
avc.execute(params);
return true;
}});
return true;
case R.id.changeuser:
if(isAllowChangeUser.equalsIgnoreCase("false")){
item.setVisible(false);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Remove the click listener.
Just put the whole login code after the case R.id.login:.
That's because onOptionsItemSelected is already the click, you don't have to create and set it again.

It would seem that the first time you click on the option menu item, you are only adding the OnMenuItemClickListener listener. The second time you click, you are triggering that listener.
Try removing the onMenuItemClickListener code and put the code in your onMenuItemClick function into the switch statement directly.

Related

Handling Click Event for menu Used for Floating Action Button Options

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);
}

Menu Item in action bar not working

Hi I added the menu file ,there are 3 Menu Items:
1) Menu Pin,
2) Send Reply and
3) Add Notes.
Send Reply , Add Notes are working fine but, when removed comment for pin, it is not working. I attached the code below.
Please suggest me any solution. When I paste the code of Send Reply to pin_menu case for testing purpose it is not working.
Log cat doesn't show anything.I added the toast on click of pin_menu it doesn't show.Please suggest me solution.
I also tried to add one extra menu in XML file and added the code same as the pin_menu but not worked. Doesn't show log cat ,toast. So that hard to debug.Same for Send Reply and Add Note but both are working fine.
Code is as below:
<item android:id="#+id/menu_pin"
android:icon="#drawable/pin"
android:title="#string/pin"
android:showAsAction="never"
/>
<item android:id="#+id/menu_send_reply"
android:icon="#drawable/send"
android:title="#string/send_reply"
android:showAsAction="never"
/>
<item android:id="#+id/menu_add_note"
android:icon="#drawable/add_note"
android:title="#string/add_note"
android:showAsAction="never"
/>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.ticket_properties_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("DATA ","Item ID "+item.getItemId());
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.menu_add_note:
Intent i3 = new Intent(Ticket_properties.this,Add_note.class);
i3.putExtra("ID", Ticket_id);
i3.putExtra("client_id", client_id);
startActivity(i3);
return true;
case R.id.menu_send_reply:
Intent reply= new Intent(Ticket_properties.this,Send_reply.class);
reply.putExtra("ticket_id", Ticket_id);
reply.putExtra("title", Ticket_title);
reply.putExtra("dept_id", tv_dept_id.getText());
reply.putExtra("Ticket_hash", Ticket_hash);
reply.putExtra("filter_id",filter_id);
startActivity(reply);
return true;
case R.id.menu_pin:
Intent reply1= new Intent(Ticket_properties.this,Send_reply.class);
reply1.putExtra("ticket_id", Ticket_id);
reply1.putExtra("title", Ticket_title);
reply1.putExtra("dept_id", tv_dept_id.getText());
reply1.putExtra("Ticket_hash", Ticket_hash);
reply1.putExtra("filter_id",filter_id);
startActivity(reply1);
return true;
/*
String PIN_URL=op.getUrl(Ticket_properties.this,"ticket", "add_pinup","&vis_ticket_id=124");
JSONArray pin_result = JSONfunctions.getJSONfromURL(PIN_URL+"&vis_encode=json",Ticket_properties.this);
String result =pin_result.toString();
if(result.equals("[\"success\"]"))
{
Operation.showToast(getApplicationContext(),R.string.pinned);
}
*/
default:
return super.onOptionsItemSelected(item);
}
}
Well i think you are using this commented code on the wrong place. You should use it before return, in the switch case structure. If you want to trigger it on pin clicked. Here is the updated code;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.ticket_properties_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("DATA ","Item ID "+item.getItemId());
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.menu_add_note:
Intent i3 = new Intent(Ticket_properties.this,Add_note.class);
i3.putExtra("ID", Ticket_id);
i3.putExtra("client_id", client_id);
startActivity(i3);
return true;
case R.id.menu_send_reply:
Intent reply= new Intent(Ticket_properties.this,Send_reply.class);
reply.putExtra("ticket_id", Ticket_id);
reply.putExtra("title", Ticket_title);
reply.putExtra("dept_id", tv_dept_id.getText());
reply.putExtra("Ticket_hash", Ticket_hash);
reply.putExtra("filter_id",filter_id);
startActivity(reply);
return true;
case R.id.menu_pin:
String PIN_URL=op.getUrl(Ticket_properties.this,"ticket", "add_pinup","&vis_ticket_id=124");
JSONArray pin_result = JSONfunctions.getJSONfromURL(PIN_URL+"&vis_encode=json",Ticket_properties.this);
String result =pin_result.toString();
if(result.equals("[\"success\"]"))
{
Operation.showToast(getApplicationContext(),R.string.pinned);
}
Intent reply1= new Intent(Ticket_properties.this,Send_reply.class);
reply1.putExtra("ticket_id", Ticket_id);
reply1.putExtra("title", Ticket_title);
reply1.putExtra("dept_id", tv_dept_id.getText());
reply1.putExtra("Ticket_hash", Ticket_hash);
reply1.putExtra("filter_id",filter_id);
startActivity(reply1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

How to close the menu when I click the button?

I use the GridView and set to MultiChoiceModeListener.
When I select the item from GridView , it will call the onCreateActionMode and the onActionItemClicked like the following code.
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
View v = LayoutInflater.from(getActivity()).inflate(R.layout.actionbar_layout, null);
mActionText = (TextView) v.findViewById(R.id.action_text);
mActionText.setText(formatString(fileListView.getCheckedItemCount()));
mode.setCustomView(v);
getActivity().getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}
And the menu will show how many item I have select like the following picture.
When I click the button , it will transmit the item which I have select to a new Fragment.
The following code is for button
download_button = (ImageButton) view.findViewById(R.id.download_button) ;
download_button.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = DownloadPage.newInstance(null, null, null, checkedItems) ;
MainActivity.addFragment(FileBrowserFragment.this, fragment);
menu.finish(); //can not call menu.finish();
}
But when it turn to the new fragment , the menu doesn't disappeared.
How to close the menu when I click the button and turn to the new fragment???
Think you are looking for finish(); on ActionMode see this example:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish();
return true;
}
}
If you want to finish by button click, register listener to your button and then put the finish() method inside that.
EDIT
Try this:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
download_button.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = DownloadPage.newInstance(null, null, null, checkedItems) ;
MainActivity.addFragment(FileBrowserFragment.this, fragment);
deleteSelectedItems();
mode.finish();
}
});
return true;
}

Hide or Show a Menu Item if Webview Class Activated?

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.

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