I'm trying to make an Options "Menu" Screen. When the menu button is pressed the menu comes up with a "button" that says Options when that is clicked, the function clicks a button called optionsButton. When that button is pressed I want one TableLayout to become Invisible and one to become visible.
Here is the code I have to hide the layouts.
public void optionButton(View view)
{
TableLayout mainTable = (TableLayout)findViewById(R.id.tableMain);
TableLayout optionTable = (TableLayout)findViewById(R.id.tableOptions);
mainTable.setVisibility(TableLayout.INVISIBLE);
optionTable.setVisibility(TableLayout.VISIBLE);
}
And here is the XML that handles my optionButton <Button android:onClick="optionButton" android:id="#+id/optionsButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:visibility="invisible"></Button>
And here is the code that handles my "Menu Button"
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.options:
Button optbtn = (Button)findViewById(R.id.optionsButton);
optbtn.performClick();
break;
default:
break;
}
return true;
}
The problem is that when the menu button is clicked, nothing happens. Any help on this issue would be greatly appreciated.
I fixed the problem by deleting the optionsTable and all of it's contents. Remaking it and putting it below the mainTable in the Graphical Editor of main.xml
Related
My App contains a RecyclerView with TextView items. On each TextView item I have defined a few functionalities, like sharing the text to another App.
How can I combine both onLongPress or onCreateContextMenu with (enabling) select-and-copy text? It is OK to enable the select-and-copy text from the context menu.
Of course I could use the for selecting text. But that conflicts with the long press (context) menu on the textview item.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/pwTextView"
android:enabled="true"
android:textIsSelectable="true" <== mandatory
android:focusable="true" <== optionally
android:longClickable="true" /> <== optionally
Of course I would like to have the context menu back after select-copying the text.
Is this functionality realistic? Yes. For the app this is essential. I have seen it in other apps as well ;-)
The below solution helps me for most situations.
I want text to be either selectable (for copy and paste) or want other gestures to work.
Initially setting the gestures on the text field:
Set textIsSelectable to false, either in the layout file or programmatically.
Set an onTouchListener on the textview with your Gestures.
Allow one of the gestures to switch to textSelection mode. See below.
How to set textSelection programmatically?
Set the textIsSelectable, focusable, longPressable to true
Set the onTouchListener to null.
Install a clickListener on the textview to allow you switch back to the original TouchListener.
1) Install your GestureHandler:
// Create your Touch Listener
onTouchListener = new OnSwipeTouchListener(mCtx, this);
view.setOnTouchListener( onTouchListener);
2) Switch to textselction modus:
// Create your popup with an menu option to switch to textselection modus:
PopupMenu popup = new PopupMenu(mCtx, view);
popup.inflate(R.menu.text_options_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case ...
case R.id.text_textisselectable:
view.setOnTouchListener(null);
((TextView)view).setTextIsSelectable( true);
((TextView)view).setFocusable( true);
((TextView)view).setLongClickable( true);
// Install a click listener to switch back to the previous Touch Listener
((TextView)view).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupx = new PopupMenu(mCtx, view);
popupx.inflate(R.menu.selecttext_back_menu);
popupx.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
((TextView)view).setTextIsSelectable( false);
((TextView)view).setFocusable( false);
((TextView)view).setLongClickable( false);
view.setOnTouchListener(onTouchListener);
return true;
}});
popupx.show();
}
});
break;
Right now I have a couple of buttons that do different things when clicked, but now I want one of them to display a menu when clicked, but I am not sure how to do this.
My code is something like this for the main buttons:
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.button1:
//do stuff
break;
case R.id.button2:
//display menu
break;
}
If button2 is pressed, I want to display a list of options and see what menu item the user selects, but how can I do this?
Displaying icon in menu
XML
<item
android:id="#+id/item_1"
android:icon="#drawable/settings"
android:showAsAction="always"
android:title="Add item1" />
You could use a PopupMenu In your onOptionsItemSelected() which will then show a different menu when one of your menu buttons is clicked. Modify this piece of code according to your needs:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.button1:
// DO SOMETHING HERE
break;
case R.id.button2:
// THE R.id.button2 has to be the same as the item that will trigger the popup menu.
View v = findViewById(R.id.button2);
PopupMenu pm = new PopupMenu(LoginActivity.this, v);
pm.getMenuInflater().inflate(R.menu.pm_accounts_item, pm.getMenu());
pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(), String.valueOf(item.getTitle()), Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.menuEdit:
break;
case R.id.menuDetails:
break;
case R.id.menuDelete:
break;
default:
break;
}
return true;
}
}); pm.show();
break;
default:
break;
}
return false;
}
You will notice that a new menu XML has been inflated at this line:
pm.getMenuInflater().inflate(R.menu.pm_accounts_item, pm.getMenu());
You will have to create a second menu XML with the list of options that you need to display when one of the buttons is clicked. This is similar to your current menu XML with the difference being, a different set of options.
IMPORTANT!
Do not forget to include this View v = findViewById(R.id.button2); before the PopupMenu pm..... The PopupMenu requires a View to anchor itself to. But the onOptionsItemSelected() method does not provide this at all. Hence the extra statement.
The above example illustrates the example in an Activity. To use this in a Fragment, change the View v = findViewById(R.id.button2); to View v = getActivity().findViewById(R.id.button2);
This is the final result:
If you are talking about Opening option menu, Then there is a method openOptionMenu() in Activity class.
case R.id.button2:
openOptionsMenu();
break;
If you are talking about opening contextMenu
You have to call openContextMenu() method, But don't forget to add registerForContextMenu(view) and other methods for taking action
I added an option menu in my app.
I want activity's whole background appear dark when menu key is touched so the user can see option menu well.(like when a dialog is displayed)
Should I use animation to do this or is there any other way?
As you're using OptionMenu, you could do:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.your_option_menu:
mRootLayout.setAlpha(0.5f);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
mRootLayout would be the root layout from the xml you set the view in your activity. Just use findViewById to set it.
After you select a final option in your menu, make sure to return the alpha property of the root layout to 1.
Can i make my Action Bar app icon clickable without displaying the back icon?
This is my code, it works, I have only layout problem:
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is my activity layout, what I want is remove back icon, is it possible?
Try to use setHomeButtonEnabled(boolean enabled) instead of setDisplayHomeAsUpEnabled(boolean enabled). The latter do exactly the same thing as former which is enabling home button but additionally put up affordance sign which you want to get rid off.
I want to create on/off button with slide .
I am using this Technic:
I put the buttons in the same position and one is visible and the other is hidden.
and when I click on one the other button is appeared and the clicked is disappeared.
Now How can I make that button slide-able.
Here is the buttons:
How can i do this ?
try using Switch but, if you are ok with minSdkVersion>=14.
hope following code helps you out.
In Layout Xml create a switch like:
<Switch
android:id="#+id/on_off_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"/>
Then in your activity get a switch from layout and set listener as follows,
Switch onOffSwitch = (Switch) findViewById(R.id.on_off_switch);
onOffSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"isChecked"+isChecked, Toast.LENGTH_LONG).show();
}
});