very practically android_learners_hub
will lid the most recent and most present instruction not far off from the world. admission slowly correspondingly you comprehend competently and appropriately. will progress your data precisely and reliably
android_learners_hub
Context menu in Android
When the person presses lengthy, the merchandise’s context menu (View) will seem. You possibly can present a context menu for any view, however they’re most frequently used for objects in ListViews, GridViews, or different collections of views the place the person can carry out direct actions on every merchandise. Affords actions that have an effect on a particular component or context body within the person interface. Menu extension for context menus that gives performance to switch the context menu header. Doesn’t assist shortcuts or icons for menu objects.
To make use of the context menu, we have to import the next class within the MainActivity.java file.
import android.view.ContextMenu;
To create the context menu we use
onCreateContextMenu(ContextMenu Menu, View v, ContextMenu.ContextMenuInfo menuInfo)
// code to execute
technique.
Necessary
We additionally have to register the view with the context menu in onCreate(Bundle SavedInstanceState) by calling registerForContextMenu() and passing it the View.
Instance: – registerForContextMenu(record);
There are two methods to supply contextual actions:
Floating context menu | contextual motion mode |
---|---|
1. It’s used when the person needs to carry out a contextual motion on |
1. It’s used when the person needs to carry out an motion on |
2. Seems as a floating record of menu objects when the person long-clicks on a view that declares assist for a context menu. |
2. Seems as a contextual motion bar on the prime of the display with motion objects (or menu objects) that have an effect on the chosen merchandise(s). |
Instance | Instance |
public strategies:
strategies | Description | Instance |
---|---|---|
1.clearheader() |
Clears the context menu header. |
@Cancel tremendous.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.context_menu, menu); |
2.setHeaderIcon(int iconRes) |
Units the context menu header icon to icon |
@Cancel ;tremendous.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.context_menu, menu); |
3.setHeaderIcon(Drawableicon) |
Units the context menu header icon to the icon offered in icon Drawable. |
@Cancel tremendous.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.context_menu, menu); |
4.setHeaderTitle(int titleRes) |
Units the title of the context menu header to the title given in titleRes |
@Cancel tremendous.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.context_menu, menu); |
5.setHeaderTitle(CharSequence title) |
Units the title of the context menu header to the title given in title. |
@Cancel tremendous.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.context_menu, menu); |
6.setHeaderView(view view) |
Units the context menu header to the View given within the view. This replaces the header title and icon. |
@Cancel tremendous.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.context_menu, menu); |
Tips on how to deal with click on occasions on context menu?
To deal with click on occasions (by the person), we’ve to implement onContextItemSelected(MenuItem merchandise). The merchandise that the person chosen is saved in its parameter (merchandise). It would assist establish the precise job to be carried out when that merchandise is chosen. The getItemId() technique queries for the ID of the chosen menu merchandise, which you could assign to every menu merchandise in XML utilizing the android:id attribute. You’ll perceive it higher by means of the next instance:-
@Cancel
// the chosen menu merchandise is saved in component
public boolean onContextItemSelected(MenuItem)
//if component worth = id = edit, the next code shall be executed.
change(merchandise.getItemId())
case R.id.edit:
// code to execute
return true;
//if component worth = id = delete, the next code shall be executed.
case R.id.take away:
// code to execute
return true;
// if the worth of the chosen component doesn’t match any case written right here, the default perform shall be executed.
default:
return tremendous.onContextItemSelected(merchandise);
With the assistance of the next instance, let’s examine how you can create a floating context menu:
Right here we are going to create context menu(menu) in record view(view) much like what we see within the Contacts app. We’ll design a context menu that may show calls, textual content messages, and so forth. to the contacts displayed within the record view.
As all the time, first create a brand new folder menu inside our undertaking useful resource listing (res/menu/) after which create a file(context_menu.xml) on it to create a menu merchandise and a submenu.
code for context_menu.xml
<?xml model="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<merchandise
android:id="@+id/context_call"
android:title="Name" >
<menu>
<merchandise
android:id="@+id/context_video"
android:title="Video Name" />
<merchandise
android:id="@+id/context_voice"
android:title="Voice Name" />
</menu>
</merchandise>
<merchandise
android:id="@+id/context_text"
android:title="Ship Textual content" />
<merchandise
android:id="@+id/context_location"
android:title="Ship location"/>
</menu>
Now, create the app bar, textual content view, and record view with the assistance of the toolbar, textual content view tag, and record view tag, respectively, in activity_main.xml.
code for main_activity.xml
<?xml model="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:instruments="http://schemas.android.com/instruments"
android:layout_width="match_parent"
android:layout_height="match_parent"
instruments:context=".MainActivity"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C59169"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<TextView
android:id="@+id/textual content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4FCCBD5C"
android:textual content="All Contacts"
android:textAlignment="middle"
android:textSize="24sp"
android:textStyle="daring"
android:textColor="#FF000000"/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#C59169" />
</LinearLayout>
In MainActivity.java Initialize the views we created within the activity_main.xml file. To retailer the record merchandise, we have to create an array after which go it to the adapter. After that, set the adapter to the record after which register the view with the context menu.
code for MainActivity.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity@Override
protected void onCreate(Bundle savedInstanceState)
tremendous.onCreate(savedInstanceState);
setContentView(R.structure.activity_main);
//declare the toolbar and provides the toolbar the power to have these choices menu present up
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//Referencing and Initializing Checklist View
ListView record=findViewById(R.id.list_view);
String[] listitem="A","Amandeep","Anchal","Ankita","Amritpreet","B","Babita","Bableen","Child","C","Cherry","Chetan","Charanjeet";
ArrayAdapter adapter=new ArrayAdapter(this, android.R.structure.simple_list_item_1,android.R.id.text1,listitem);
record.setAdapter(adapter);
// Referencing and Initializing textual content View
TextView text_pop=findViewById(R.id.textual content);
// registering record view for context menu
registerForContextMenu(record);
After that, now let’s write
code to create a context menu and deal with the clicking occasion. I am right here utilizing a easy Toast message to show the title when the merchandise is chosen.
// this code is written exterior Oncreate(Bundle savedInstancestate) and inside class
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
tremendous.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu, menu);
menu.setHeaderView(v);@Override
public boolean onContextItemSelected(@NonNull MenuItem merchandise)Toast.makeText(this, "You choose : "+merchandise.getTitle(), Toast.LENGTH_LONG).present();
return tremendous.onContextItemSelected(merchandise);
// shut brace of sophistication MIainactivity
When will execute the above code Manufacturing be:-
I want the article virtually android_learners_hub
provides perspicacity to you and is beneficial for totaling to your data