Contextual action bar in android

android_learners_hub

android_learners_hub

Previous

Contextual Motion Bar (CAB)

The contextual motion bar (CAB) has a sophisticated consumer interface, so Floating context menu as a result of the lengthy press gesture is now used to pick out a number of gadgets and may carry out a operate on them concurrently. When zoomed in, a contextual motion bar (CAB) is a brief motion bar that overlays the applying motion bar at some stage in a selected subtask. When a consumer permits this mode by deciding on an merchandise, a contextual motion bar seems on the prime of the exercise with menu gadgets. When the consumer presses the default again arrow (proven on the highest left) or performs the motion, the contextual motion bar disappears and the motion mode turns off. To invoke the contextual motion mode on the view
1. the consumer makes a protracted click on on the view
2. the consumer selects a checkbox or related UI part inside the view
We are able to invoke CAB on one or each of the above two occasions.

The applying and conduct of every motion depends upon the next 2 designs:-

1. Contextual actions on particular person and arbitrary views (like Button, TextView, and so on.).
2. Contextual actions on teams of things (ListView, GridView, and so on.).
We are going to talk about each layouts with an instance on this put up. Let’s begin with the primary,Enabling contextual motion mode for single view (Button).

Step 1(in MainActivity.java) Create motion mode variable.
non-public ActionMode mActionMode;

Step 2(in main_activity.xml) Create a button within the exercise and set its id.
code for main_activity.xml:
<?xml model="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:instruments="http://schemas.android.com/instruments"
android:layout_width="match_parent"
android:layout_height="match_parent"
instruments:context=".MainActivity"
android:orientation="vertical">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:textual content="Button"/>

</LinearLayout>

Step 3(in contextual_action_bar.xml) Create menu folder in res. after which create the contextual_action_bar.xml file to retailer the menu gadgets.
code for contextual_action_bar.xml

<?xml model="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<merchandise
android:id="@+id/Contextual_1"
android:title="Assist"
android:icon="@drawable/ic_help_"
app:showAsAction="ifRoom"
/>
<merchandise
android:id="@+id/Contextual_2"
android:title="data"
android:icon="@drawable/ic_baseline_info_24"
app:showAsAction="ifRoom"
/>
<merchandise
android:id="@+id/Contextual_3"
android:title="Delete"
android:icon="@drawable/ic_baseline_delete_24"
app:showAsAction="ifRoom"
/>
</menu>
Step 4 (in MainActivity.java) implement the ActionMode.Callback Interface. Whenever you write the next code, a crimson bulb will seem (error signal).
non-public ActionMode.Callback actionModeCallback = new ActionMode.Callback()
Keep in mind: the contextual motion bar isn’t essentially related to the applying bar. They work independently, regardless that the contextual motion bar visually overrides the place of the applying bar.
Callback strategies:-
The primary methodology is ‘onCreateActionMode‘. This methodology is known as as soon as when the Action mode is created first and is the place we should configure the Menu.
@Cancel
public boolean onCreateActionMode(ActionMode mode, menu menu) {
// Inflate a menu useful resource by offering context menu gadgets
MenuInflater inflater = mode.getMenuInflater();
inflator.inflate(R.menu.context_menu, menu);
return true;
}
Movin’ to ‘onPrepareActionMode‘, this methodology can be known as every time the CAB is invalidated.
@Cancel
public boolean onPrepareActionMode(ActionMode mode, menu menu) {
false return; // Return false if nothing is finished
}
Subsequent is the ‘onActionItemClicked’ methodology, this methodology is known as to deal with click on occasions (each time one in every of your CAB’s menu gadgets is clicked).
@Cancel
public boolean onActionItemClicked(ActionMode mode, MenuItem ingredient) {
change(merchandise.getItemId()) {
case R.id.menu_share:
shareCurrentItem();
mode.end(); // Motion chosen, so shut the CAB
return true;
flaw:
false return;
}
}
Final however not least is ‘onDestroyActionMode‘. This methodology is known as every time the consumer leaves the CAB otherwise you name ‘end()’ in ActionMode.
@Cancel
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null; // set null to the ActionMode variable, which we created in step 1.
}
step 5 Set the OnLongclick listener on the button (single view), after which name startSupportActionMode(ActionModeCallback) to allow contextual motion mode.
         
button.setOnLongClickListener(new View.OnLongClickListener() {
@Cancel
public boolean onLongClick(View v) {
if (mActionMode != null) {
false return;
}
mActionMode = startSupportActionMode(ActionModeCallback);
return true;
}
});
}
 code for MainActivity.java

bundle com.instance.contextualaction_bar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ActionMode;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
non-public ActionMode mActionMode;// step 1

@Override
protected void onCreate(Bundle savedInstanceState) {
tremendous.onCreate(savedInstanceState);
setContentView(R.format.activity_main);
Button button=findViewById(R.id.button);

------ Step 5------- Begin
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mActionMode != null) {
return false;
}
mActionMode = startSupportActionMode(ActionModeCallback);
return true;
}
})
;
}// Step 4 ----- start-------
non-public ActionMode.Callback ActionModeCallback = new ActionMode.Callback() {
// Referred to as when the motion mode is created; startActionMode() was known as
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu useful resource offering context menu gadgets
mode.getMenuInflater().inflate(R.menu.contextual_action_bar, menu);
// to set Title on Contextual motion bar(CAB)
mode.setTitle("Select any:");
return true;
}

// Referred to as every time the motion mode is proven. At all times known as after onCreateActionMode, however
// could also be known as a number of occasions if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;// Return false if nothing is finished
}

// It will likely be known as when the consumer selects a contextual menu merchandise.
// Dealing with click on occasions.
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem merchandise) {
String Title= (String) merchandise.getTitle()
;
Toast.makeText(MainActivity.this, Title, Toast.LENGTH_SHORT).present();
// to vanish or shut contextual bar after the motion is carried out.
mode.end();
return false;
}
// Referred to as when the consumer exits the motion mode
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}

}
;
}



Manufacturing:-

cabin exit

CAB output after long press on button

CAB exit after selecting menu item

Associated subjects:-
android_learners_hub

By admin

x
THE FUTURE - BENEFIT NEWS - DANA TECH - RALPH TECH - Tech News - BRING THE TECH - Tech Updates - News Update Viral - THE TRUTH - WORLD TODAY - WORLD UPDATES - NEWS UPDATES - NEWS FLASH - TRUTH NEWS - RANK NEWS - PREMIUM NEWS - FORUM NEWS - PROJECT NEWS - POST NEWS - WORLD NEWS - SPORT NEWS - INDICATOR NEWS - NEWS ROOM - HEADLINE NEWS - NEWS PLAZA