Checking if a button has been clicked - android

How to load menu depend on button was clicked? Any idea, solution?
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
menu.clear();
if(button.**isclicked**) {
getMenuInflater().inflate(R.menu.menu_main, menu);
} else {
getMenuInflater().inflate(R.menu.test, menu);
}
return true;
}

Put a boolean field in your activity and change whenever you click button . Depends if its true or false show or not your menu
UPDATE
//declare boolean
boolean clicked=false;
//my button clic
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
//change boolean value
clicked=true;
}
});
//then on another method or where you want
if(clicked)
{
openmenu();
}
else
{
closemenu();
}

try this function
button.isPressed();

Try something like this:
get the button's id
Button button = (Button) findViewById(R.id.button_send);
Adding click event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do whatever you want
}
});

THe below also work fine.
if(button.isSelected())

Related

Focus ListView Items on Button Click

Is it possible to focus the List View Items through any button click?
Like i want that when user click on Floating Action Button then the listview gets focused. I dont want to show checkbox type layout on button clicked. I just want to show the same like in the screenshot on Button click.
What i did is I put the listview onItemLongClick code in the button click blocks but it doesnot work.
fabButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
listViewMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listViewMessages.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
tv.setText(listViewMessages.getCheckedItemCount()+ " Selected");
}
#Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu:
}
});
mode.finish();
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contextual, menu);
fabButton.setVisibility(View.INVISIBLE);
fabButtonn.setVisibility(View.VISIBLE);
fabButtonn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for ( int i=0; i< messageListAdapter.getCount(); i++ ) {
listViewMessages.setItemChecked(i, true);
}
}
});
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
fabButton.setVisibility(View.VISIBLE);
fabButtonn.setVisibility(View.GONE);
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
With this code if user clcik on button then he/she has too long press items again to focus the list items which is not what i want. I want to focus items right when button click. Any explanation or link provided will be helpful

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

How to cast Menuitem to Button or imageButton

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.

Categories

Resources