I followed the instructions and examples on Android website to create a context menu but mine shows up completely black and i cannot change any options in it ; anyone out there had the same experience and can help me solve this issue.
FWIW, here are my class .java and the menu .xml files
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="#+id/red"
android:title="#string/red"
android:checked="true" />
<item android:id="#+id/blue"
android:title="#string/blue" />
<item android:id="#+id/green"
android:title="#string/green" />
<item android:id="#+id/yellow"
android:title="#string/yellow" />
<item android:id="#+id/black"
android:title="#string/black" />
<item android:id="#+id/white"
android:title="#string/white" />
<item android:id="#+id/orange"
android:title="#string/orange" />
</group>
</menu>
package com.MyProject;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
public class ColorsActivity extends Activity {
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
registerForContextMenu(v);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.red:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.blue:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.green:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.yellow:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.black:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.white:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.orange:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
default:
return super.onContextItemSelected(item);
}
}
}
Activity.registerForContextMenu (View) registers the context menu with the OS so that when the menu button is pressed and the given view is in the foreground, the callback to onCreateContextMenu is made. What you have done is register the view within the callback, making it fundamentally unreachable in your code because the view would have to be already registered in order to reach the registration you have here. registerForContextMenu should be called in one of your lifecycle startup methods, probably onResume.
Related
I'm new to coding, now creating an android app which has some videos under video tab/list, and my question is how can I add a Delete(with a notification), Share, Cancel button when I long press a specific video which I want to delete (from internal storage)? Guide me with examples. In totally new into this :)
minSdkVersion="11"
targetSdkVersion="25"
Searched in but didn't find any appropriate answer with examples, though ..
Eager to study :(
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.deletevideo:
// add stuff here
return true;
case R.id.sharevideo:
// edit stuff here
return true;
case R.id.cancelvideo:
// remove stuff here
return true;K
default:
return super.onContextItemSelected(item);
}
}
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item
android:id="#+id/deletevideo"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/video_delete"/>
<item
android:id="#+id/sharevideo"
android:icon="#android:drawable/ic_menu_edit"
android:title="#string/video_share"/>
<item
android:id="#+id/cancelvideo"
android:title="#string/video_Cancel"/>
</menu>
All you have to do is add contextListener:
Java File
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
return true;
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.add:
// add stuff here
return true;
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
// remove stuff here
return true;
default:
return super.onContextItemSelected(item);
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_delete" />
<item android:id="#+id/edit"
android:icon="#android:drawable/ic_menu_edit"
android:title="#string/menu_share" />
<item android:id="#+id/delete"
android:icon="#android:drawable/my_icon_delete"
android:title="#string/menu_cancle" />
</menu>
I have this in my menu_check.xml But when I click on the check nothing happens so the Toast never show... what's the problem? Thanks a lot
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_selecciontodo"
android:title="#string/check"
android:checkable="true"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="always" />
</menu>
and this in my java class
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_check, menu);
checkBox = (CheckBox) menu.findItem(R.id.action_selecciontodo).getActionView();
checkBox.setText("Select all");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_selecciontodo:
CheckBox checkBox= (CheckBox) item.getActionView();
if (checkBox.isChecked())
Toast.makeText(getApplicationContext(), "You selected all", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
No need to app:actionViewClass="android.widget.CheckBox" just do like this
in oncreate options menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return true;
}
and in onOptionsItemSelected write this code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_bookmark:
if (item.isChecked()) {
item.setChecked(false);
Toast.makeText(MainActivity.this, "Un Checked", Toast.LENGTH_SHORT).show();
} else {
item.setChecked(true);
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
}
break;
}
return super.onOptionsItemSelected(item);
}
and in your menu.xml in menu folder declare menu like this
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/menu_bookmark"
android:checkable="true"
android:title="Bookmark"/>
so when you check uncheck the menu item it show toast and show checkbox checked or unchecked
Try this,
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.stackoverflow.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_check"
android:title="YOUR_TITLE"
android:orderInCategory="200"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
</menu>
MainActivity.java
#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_main, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);
MenuItem item = menu.findItem(R.id.action_check);
item.setChecked(isChecked);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_check) {
item.setChecked(!item.isChecked());
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("checkbox", item.isChecked());
editor.commit();
Log.i("cheeck status" , "" + item.isChecked());
return true;
}
return super.onOptionsItemSelected(item);
}
this should work.
Happy coding.!!!
I have a search feature for recipes and I want to include search filters for different allergies or diets. I have included this as an option on the action bar on the page. However, whenever I click on a checkbox, the menu disappears and I have to go click on the orange icon again to popup the menu and then click on another option. Is there a way where the menu can stay open so I can check multiple filters without the menu closing each time?
This is an picture of my filter section.
checkable_test.xml
<item android:id="#+id/action"
android:title="#string/action_example"
android:icon="#drawable/filter"
app:showAsAction="withText|ifRoom">
<menu>
<item android:title="Allergies">
<menu>
<group android:id="#+id/allergies"
android:checkableBehavior="all">
<item android:id="#+id/no_peanut"
android:icon="#drawable/no_peanut"
android:title="No Peanuts"
android:checkable="true"/>
<item android:id="#+id/no_sesame"
android:icon="#drawable/no_sesame"
android:title="No Sesame"
android:checkable="true"/>
<item android:id="#+id/no_dairy"
android:title="No Dairy"
android:icon="#drawable/no_milk"
android:checkable="true"/>
<item android:id="#+id/gluten_free"
android:icon="#drawable/gluten_free"
android:title="No Gluten"
android:checkable="true"/>
<item android:id="#+id/no_egg"
android:icon="#drawable/no_eggs"
android:title="No Egg"
android:checkable="true"/>
<item android:id="#+id/no_soy"
android:icon="#drawable/no_soy"
android:title="No Soy"
android:checkable="true"/>
<item android:id="#+id/no_seafood"
android:icon="#drawable/no_crust"
android:title="No Seafood"
android:checkable="true"/>
</group>
</menu>
</item>
Search.java
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.checkable_test, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.no_peanut:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.no_dairy:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.no_egg:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.no_seafood:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.no_sesame:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.no_soy:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.gluten_free:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Any solutions please?
You can try changing all of the
return true;
into
return false;
I have a menu_item and here is the code:
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.TextView"
android:title="In Stocks"
app:showAsAction="always" />
<item
android:id="#+id/action_checkbox"
app:actionViewClass="android.widget.CheckBox"
android:title="#string/action_check"
app:showAsAction="always"
/>
I am trying to catch Menu_item_clicked_event So, I write following code in Activity file
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_checkbox:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.action_search:
System.out.println("xas");
default:
return super.onOptionsItemSelected(item);
}
}
When I am pressing the action_search it's working but not for action_checkbox. Why this is happening? I am getting No exception in ADB logs. Is this is the right way to do check for Checkboxes?
app:actionViewClass should be android:actionViewClass="android.widget.CheckBox"
<item
android:id="#+id/action_checkbox"
android:actionViewClass="android.widget.CheckBox"
android:icon="#drawable/unchecked"
android:checkable="true"
android:checked="false"
android:title="#string/action_check"
app:showAsAction="always"
/>
However checkbox will not be visble. Referthis
you could have your own icon and set it android:icon=
And in class
case R.id.action_checkbox:
if(item.isChecked()){
item.setIcon(R.drawable.unchecked);
}
else {
item.setIcon(R.drawable.checked);
}
item.setChecked(!item.isChecked());
break;
I cant seem to get my code to work. I keep getting a error.
I made a selector.xml with this code
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/loginbuttondn" />
<item android:state_selected="false"
android:drawable="#drawable/loginbutton" />
</selector>
heres my actual code
package monaiz.net.periscope.periscope;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
public class MainActivity extends AppCompatActivity implements OnTouchListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
v.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
v.setSelected(arg1.getAction()==MotionEvent.ACTION_DOWN);
return true;
}
});
}
return true;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Im trying to get it so that when i click the button it has a click down effect showing the other image
Im getting a error here:
Im still getting a error on this line v.setSelected(arg1.getAction()==MotionEvent.ACTION_DOWN);
on the "v."
code for my button
<ImageView
android:layout_width="280dp"
android:layout_height="90dp"
android:layout_marginTop="830px"
android:layout_marginLeft="55dp"
android:src="#drawable/loginbutton"/>
Your selector needs to look like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/loginbuttondn" />
<item android:drawable="#drawable/loginbutton" />
</selector>
Call this login_button_selector.xml and put it in your /res/drawable folder
Now use it like this:
<ImageView
android:layout_width="280dp"
android:layout_height="90dp"
android:layout_marginTop="830px"
android:layout_marginLeft="55dp"
android:src="#drawable/login_button_selector"/>
When the view is in "pressed" state, it will match the first item in the selector. When it is not in "pressed" state, it will just match the second item in the selector.
Use state_pressed:
drawable/selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/loginbuttondn" />
<item
android:drawable="#drawable/loginbutton" />
</selector>
<Button
android:layout_width="280dp"
android:layout_height="90dp"
android:background="#drawable/selector"/>