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>
Related
I am trying to get a 3 dot menu to show in my app but can't get it to work.
I have created a file called mymenu.xml under res/menu and the contents are:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/level"
android:title="#string/Level"
app:showAsAction="never">
</item>
</menu>
This is what is in my MainActivity.java file:
#Override
public boolean onCreateOptionsMenu(Menu my_menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, my_menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.level:
levelMax = 41;
levelTextView.setText("Level:\nEasy");
mCountDownTimer.cancel();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
What am I doing wrong? Any help would be appreciated. Thanks.
Change app:showAsAction="never" to app:showAsAction="always"
I am trying to display action overflow menu with icons in my one application. I am not getting icons in menu. My Target SDK = 23 and Minimum SDK = 16.
My Menu.xml is like below
<?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">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_donate"
android:icon="#drawable/new_facebook_page"
android:title="#string/facebook"
app:showAsAction="never" />
<item android:id="#+id/menu_settings"
android:icon="#drawable/new_facebook_page"
android:title="#string/facebook"
app:showAsAction="never" />
<item android:id="#+id/menu_logout"
android:icon="#drawable/new_facebook_page"
android:title="#string/facebook"
app:showAsAction="never" />
</menu>
and Java code is like below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
But it does not showing icons in menu. Let me know if anyone can help me to achieve this without changing menu structure.
Thanks
Add below line to your java class inside onCreateOptionsMenu(Menu menu):
menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile)));
Try with below code :
#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_patient_home_screen, menu);
menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile)));
menu.add(0, 2, 2, menuIconWithText(getResources().getDrawable(R.mipmap.add_user), getResources().getString(R.string.action_add_user)));
menu.add(0, 3, 3, menuIconWithText(getResources().getDrawable(R.mipmap.switch_profile), getResources().getString(R.string.action_switch_profile)));
menu.add(0, 4, 4, menuIconWithText(getResources().getDrawable(R.mipmap.logout), getResources().getString(R.string.action_sign_out)));
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
switch (item.getItemId()) {
case 1:
Toast.makeText(PatientHomeScreen.this, "Profile is Clicked", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(PatientHomeScreen.this, "Add New User is Clicked", Toast.LENGTH_SHORT).show();
return true;
case 3:
Toast.makeText(PatientHomeScreen.this, "Switch Profile is Clicked", Toast.LENGTH_SHORT).show();
return true;
case 4:
Toast.makeText(PatientHomeScreen.this, "Sign Out is Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
private CharSequence menuIconWithText(Drawable r, String title) {
r.setBounds(0, 0, r.getIntrinsicWidth(), r.getIntrinsicHeight());
SpannableString sb = new SpannableString(" " + title);
ImageSpan imageSpan = new ImageSpan(r, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
Or, you can show icon with menu item by :
xml code :
<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_alarm"
android:icon="#drawable/ic_accept"
android:orderInCategory="100"
android:title="#string/menu_create_alarm"
android:showAsAction="always|withText"
app:showAsAction="always|withText" />
Java Code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.menu_item, menu);
super.onCreateOptionsMenu(menu, inflater);
}
You can use the custom layout for menu item child
<?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">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_donate"
android:title="#string/facebook"
android:actionLayout="#layout/menu_donate_layout"
app:showAsAction="never" />
<item android:id="#+id/menu_settings"
android:title="#string/facebook"
android:actionLayout="#layout/menu_setting_layout"
app:showAsAction="never" />
<item android:id="#+id/menu_logout"
android:title="#string/facebook"
android:actionLayout="#layout/menu_logut_layout"
app:showAsAction="never" />
</menu>
Create menu_donate_layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/profile_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile" />
</LinearLayout>
Same create the separate layout of each menu item or change text and icon dynamically.
i want to just add searchview in my app.but i dont want to search any thing just i want the query entered by the user.
so far i tried this code but when i run my app it crashes.
Update:
I tried this one but eventhough my app crashes.
main_menu.xml code
<?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">
<item
android:id="#+id/search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
app:actionViewClass="android.support.v7.widget.SerachView"
app:showAsAction="always"
/>
MainActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
SearchView searchView=(SearchView)findViewById(R.id.search);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// Toast.makeText(this,query,Toast.LENGTH_LONG).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Can Anyone help me to solve this problem please.
ThankYou.
Lets not use SearchView directly, in your menu.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=".HomeActivity">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
and add this simple code to your java :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
// Not implemented here
return false;
default:
break;
}
searchView.setOnQueryTextListener(queryTextListener);
return super.onOptionsItemSelected(item);
}
Main_Menu.xml
<?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">
<item
android:id="#+id/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"
android:icon="#android:drawable/ic_menu_search"
android:title="Search" />
</menu>
onCreateOptionMenu method
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_view_menu_item, menu);
MenuItem searchViewItem = menu.findItem(R.id.action_search);
final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchViewItem);
searchViewAndroidActionBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchViewAndroidActionBar.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(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 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.