Android Quick Contextual bar on textview - android

In Android if the textview is selected then a word gets selected and a contextual action bar comes at the top...i want to modify that CAB and make it look like a quick action bar...keeping the text selection feature intact...please help me...

you can select textview or anything like only in simple layout CAB appear in the top and there was some method onActionItemClicke that you want to click an item and then you modify your action in CAB default behavior................................
public class MainActivity extends Activity {
protected Object mActionMode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.lay);
view.setOnLongClickListener(new View.OnLongClickListener() {
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
mActionMode = MainActivity.this.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show();
return true;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
// Assumes that you have "contexual.xml" menu resources
inflater.inflate(R.menu.cab, menu);
return true;
}
// Called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
Toast.makeText(MainActivity.this, "Selected menu",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
case R.id.shan:
Toast.makeText(MainActivity.this, "Selected shani",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// Called when the user exits the action mode
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
and in menu xml file create menu that you would like to appear in top CAB
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/toast"
android:title="Toast">
</item>
<item
android:id="#+id/shan"
android:title="shani">
</item>
</menu>

Related

How to change the menubar Icon when pressed in Android

Here i am having menu bar with two items called notification_icon and favourite_icon when i pressed favourite_icon that should be changed as favourite_icon2.So that i have tried selector. Selector only works for android:state_pressed="true" not android:state_selected="true". s Please guide me to do that.Here i have added the following code for your refreence
menu_clg.xml (under menu)
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".UserDashBoardFragment">
<item
android:id="#+id/action_notify"
android:icon="#drawable/mail_icon"
appmunu:showAsAction="always"
android:title="Notification" />
<item
android:id="#+id/action_favourite"
android:icon="#drawable/icon_selector"
appmunu:showAsAction="always"
android:title="Favourite" />
</menu>
this is icon_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/vijay"
/>
<item
android:state_selected="false"
android:drawable="#drawable/favourite_icon"
/>
</selector>
this is activity 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_clg, menu);
mMenu = menu;
return true;
}
// delete the selected event from event list added here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
navigatetoNotification();
return true;
case R.id.action_favourite:
favouriteClg();
return true;
}
return super.onOptionsItemSelected(item);
}
Now, what we want to do is, when the user clicks on the add icon “+” we will add a new item and change the add icon to remove “X” icon.
onOptionsItemSelected() will be called when a user clicks on an item.
onOptionsItemSelected(MenuItem item) MenuItem object is passed as a parameter which is a reference to the clicked item. Using this object we can know which item has been clicked.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
boolean canAddItem = false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_addItem){
// ( 1 ) add a new item
// ( 2 ) change add to remove
}
else{
// if a the new item is clicked show "Toast" message.
}
return super.onOptionsItemSelected(item);
}
}
We need to call onPrepareOptionsMenu(Menu menu) to add, remove and modify menu items.
On Android 2.3.x and lower, the system calls onPrepareOptionsMenu() each time the user opens the options menu (presses the Menu button).
On Android 3.0 and higher, we need to call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
boolean canAddItem = false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast toast;
if(item.getItemId() == R.id.action_addItem){
invalidateOptionsMenu();
}
else{
toast = Toast.makeText(this, item.getTitle()+" Clicked!", Toast.LENGTH_SHORT);
toast.show();
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(canAddItem){
menu.getItem(0).setIcon(R.drawable.ic_content_remove);
MenuItem mi = menu.add("New Item");
mi.setIcon(R.drawable.ic_location_web_site);
mi.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
canAddItem = false;
}
else{
menu.getItem(0).setIcon(R.drawable.ic_content_new);
canAddItem = true;
}
return super.onPrepareOptionsMenu(menu);
}
}
Hope it helps.

Batch contextual action mode

I want to enable multiple view selection on longClick(). Should I declare an action mode object and call startActionMode()? Also, how would I change menu list for single item click? I used the documentation as reference, as shown.
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
To change menu list for single item click following is the code.
int count=0;
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
if (checked) {
count++;
} else {
count--;
}
mode.invalidate(); // Add this to Invalidate CAB so that it calls onPrepareActionMode
}
Now modify the onPrepareActionMode as follows
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
if (selCount == 1){
//show the menu here that you want if only 1 item is selected
} else {
//show the menu here that you want if more than 1 item is selected
}
}

Hide Items in Context Action Bar (CAB) dynamically when multiple items are selected

I have a listActivity that shows CAB on long click. If more than 1 item is selected I would like to hide one of my menu items.
I keep track of the # of items selected in onItemCheckedStateChanged(). However I don't have access to the menu to remove the item from this function. See comments in code below to get an idea of what I was trying. I feel like I am missing some simple core understanding... code below is called from my onCreate() function.
private void setupActionBarContext() {
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
private int selCount = 0;
ArrayList<Long> idList = new ArrayList<Long>();
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) {
selCount++;
idList.add(id);
} else {
selCount--;
idList.remove(id);
}
mode.setTitle(selCount + " selected");
// I WOULD LIKE TO HIDE ITEM ON MENU IF 'selCount' IS > 1
// For example something like this...
// if (selCount > 1) {
// MenuItem item = menu.findItem(R.id.edit_item);
// item.setVisible(false);
// } else {
// MenuItem item = menu.findItem(R.id.edit_item);
// item.setVisible(false);
// }
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete_item:
for(Long i: idList){
mDbHelper.deleteItem(i);
}
mode.finish();
return true;
case R.id.edit_item:
Toast.makeText(getBaseContext(), "Edit Item", Toast.LENGTH_SHORT).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
selCount = 0;
idList.clear();
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
});
And my menu item...
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/edit_item"
android:title="#string/edit_item"
android:showAsAction="ifRoom"
android:orderInCategory="1"/>
<item android:id="#+id/delete_item"
android:title="#string/delete_item"
android:icon="#drawable/ic_action_delete"
android:showAsAction="ifRoom"
android:orderInCategory="2"/>
</menu>
As suggested in adneal's comment.
Add invalidate() to onItemCheckedStateChanged()
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) {
selCount++;
idList.add(id);
} else {
selCount--;
idList.remove(id);
}
mode.setTitle(selCount + " selected");
mode.invalidate(); // Add this to Invalidate CAB
}
This invalidates the CAB and causes the onPrepareActionMode() function to be called.
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
if (selCount == 1){
MenuItem item = menu.findItem(R.id.edit_item);
item.setVisible(true);
return true;
} else {
MenuItem item = menu.findItem(R.id.edit_item);
item.setVisible(false);
return true;
}
}

Android menu options doesn't higlight on click/touch

I am displaying menu action bar at the bottom of the screen. when user click/touch any of the menu item, i want to highlight it (i.e. the way button click highlighting happens). i tried onClickListener and ontouchListener but it doesn't highlight.
can someone tell me which porperty/method i have set.
Here is code i am using.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.landing_page_layout);
ActionBar actionBar = getActionBar();
actionBar.show();
// business logic }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items, menu);
item1 = menu.findItem(R.id.menu_option1);
item1.getActionView().setOnTouchListener(new OnTouchListener() {
// logic when user touch menu option1 touch
}});
Thanks
Chintan
Check this section in the documentation: http://developer.android.com/guide/topics/ui/menus.html#options-menu
To set the menu up you do this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
Where R.menu.menu points to your res/menu/menu.xml file. This will load the elements from that file
The options menu is listened to in the same way as regular View's with OnClickListeners and such. Instead you onOptionsItemSelected override like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.item1:
// Do something
return true;
case R.id.item2:
// Do something else
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Passing id of ListView item to ActionMode.Callback object

So my problem right now is that right now I am long clicking an item in a ListView which brings up a contextual action bar. The id passed into onItemLongClick is the variable that I would like to use in the mActionModeCallback's on ActionItemClicked() method. This seems like it would be a fairly common procedure since if a user is editing a list of items, you would want to access the id of that row in the database somehow when the user clicked an "edit" or a "delete" action.
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> p, View view, int pos, long id) {
//The id of the row in the database
long variableThatIWantToPassToCallback = id;
mActionMode = getActivity().startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//I would like access to the id of the clicked item here, NOT item.getItemId()
}
public void onDestroyActionMode(ActionMode mode) {}
};
The proper way to do this is to call mActionMode.setTag("1") in onItemCheckedStateChanged and then from the onActionItemClicked function call mode.getTag();
Create your own callback by extending the interface ActionMode.Callback
private interface ActionCallback extends ActionMode.Callback {
public void setClickedView(View view);
}
private ActionCallback mActionModeCallback = new ActionCallback() {
public View mClickedView;
public void setClickedView(View view) {
mClickedView = view;
}
// Called when the action mode is created; startActionMode() was called
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
// Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item ) {
switch ( item.getItemId() ) {
case R.id.menu_delete:
Log.v( TAG, "#onActionItemClicked ready to delete the item with id: " + mClickedView.getTag() );
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
} // end switch
}
// Called when the user exits the action mode
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
For a view which has OnLongClickListener attached, override the onLongClick callback this way.
#Override
// Called when the user long-clicks on someView
public boolean onLongClick( View view ) {
// proceed only when actionmode is not null
// otherwise overlapping action modes will be
// displayed
if( mActionMode != null ) {
return false;
}
mActionModeCallback.setClickedView(view);
// Start the CAB using the ActionMode.Callback defined above
mActionMode = startActionMode( mActionModeCallback );
view.setSelected(true);
return true;
}

Categories

Resources