Prerequisite
If you not understand this lecture Contextual Action mode in android studio.You should also read my previous lectures Option Menu in android studio and Creating a time picker in android . After reading these lecture you will also able to create Contextual Action mode of your app.
Overview
Contextual action mode displays a bar at the top of the screen with a items such Delete, share, etc.When a user enables this mode by selecting an item a contextual action bar appears at the top of the screen to present actions the user can perform on the currently selected item.
Introduction
In this lecture we will describe how can create contextual action mode in android studio .And also describe what is contextual Action mode.why we can use it.After Reading this lecture you will understand Contextual action mode in android And i hope you will create Contextual action Mode of your application.
Contextual menu and Contextual action mode
Contextual menus used to select one option menu.Context Menu displays hierarchical data as a multi level menu in a popup.It provides option of items and user can select one option at a time.But the contextual action mode display action bar at the top and provide facility to user.User can select multipal items in contextual action mode.
Description of Contextual action mode
The contextual action mode is available on Android 3.0.Contextual option menu is used when you select multipal items.Using contextual action mode you will select multipal items at a same time.You can used contextual action mode for individual or selecting multipal option.Like a list of items in a list view or some image in a grid view.In this lecture will describe how can create contextual action mode with individual mode.So we create a new project in android studio.Every project contain two file first is java class file and second is layout XML file.So our first step is add image in drawable folder how can add image into drawable folder please read step 1.For more detail about contextual action mode please click on Contextual action mode.
Step 1=Image add<-copy image->and right click on drawable<-paste image>into<-drawable folder->
After this step you will create a new menu .how can create menu layout detail is provides step by step n a below pic.
Step 2=Add menu<-Right click the menu folder-><- select new option->and click on-<Menu resource folder->
Image may be NSFW.
Clik here to view.
Step3=Step2<-Writename->ok
Image may be NSFW.
Clik here to view.
First of all we will add two items first item is for deleting the image and second item that are add is for sharing the image So add these two items using< item> attributes. and also assign id and title to each item.For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity code you should define a menu and all its items in an XML.Code that are write in menu.xml file
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/id_delete" android:title="delete" android:icon="@drawable/delete" ></item> <item android:id="@+id/id_share" android:title="delete" android:icon="@drawable/share"></item> </menu>
After this step you will open a Layout.XML file.And add image and set height and width.And also need to assign id to image view using android id attribute.And use android src property to assign image.Code that are write in Layout. XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="#000000"> <ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="250dp" android:src="@drawable/fish" /> </RelativeLayout>
Code that are write MainActivity. java class file.
public class MainActivity extends ActionBarActivity { ImageView imageView; ActionMode actionMode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView=(ImageView)findViewById(R.id.image_view); imageView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if (actionMode!=null){ return true; } else { actionMode=MainActivity.this.startActionMode(callback); return true; } } }); } private ActionMode.Callback callback=new ActionMode.Callback() { @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater menuInflater=getMenuInflater(); menuInflater.inflate(R.menu.my_nenu,menu); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { mode.setTitle("1 item is selected"); return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { if (item.getItemId()==R.id.id_delete) { Toast.makeText(getApplicationContext(),"you really want to delete?",Toast.LENGTH_SHORT).show(); return true; } else if (item.getItemId()==R.id.id_share) { Toast.makeText(getApplicationContext(), "you really want to share?", Toast.LENGTH_SHORT).show(); return true; } return false; } @Override public void onDestroyActionMode(ActionMode mode) { } }; }
Run the Application of contextual action mode
Image may be NSFW.
Clik here to view.
When you long press in a pic that are show in emulator then this output will show
Image may be NSFW.
Clik here to view.
Thank you for reading this lecture Hope you got the idea.
The post Contextual Action mode in android studio appeared first on The Master World.