Multiple Selection on ActionBarSherlock - android

In my application, I show some data to the user depending on which category he chooses. For that, I'm using ActionBarSherlock, to display a menu of categories he can choose from. When one category is clicked, this content is loaded. What I now want to do is enable a multi-select option, with checkboxes, and an OK button to trigger the content loading. I've been searching for some time and couldn't figure out how to enable this multi-select menu. Below is part of my current code
public class CategoriesActivity extends SherlockActivity implements
ActionBar.OnNavigationListener {
private ArrayList<Category> categoryList;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(Application.THEME);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_ringtones);
Context context = getSupportActionBar().getThemedContext();
categoryList = new ArrayList<Category>();
ArrayAdapter<Category> adapter = new ArrayAdapter<Category>(context,
R.layout.sherlock_spinner_item, categoryList);
adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter, this);
getSupportActionBar().setIcon(R.drawable.abs__ic_search);
// By default, load data for the first category
loadCategoryData(categoryList.get(0).getId());
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
loadCategoryData(categoryList.get(itemPosition).getId());
return false;
}

Related

put Activity into Tablayout

I have this activity with some adapters , I would put it in tableLayout I already try some methods ,
unfortunately they didn't work , ( extends FragmentActivity , Convert Activity to Fragment with using onCreateView() )
`public class MainActivity_Delete extends AppCompatActivity` {
private SwipeMenuListView listView;
private ArrayList<Data> dataArrayList;
private ListAdapter listAdapter;
private Data data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete);
listView = (SwipeMenuListView) findViewById(R.id.listview);
dataArrayList = new ArrayList<>();
listAdapter = new ListAdapter(this, dataArrayList);
listView.setAdapter(listAdapter);
listView.setMenuCreator(creator);
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
Toast.makeText(MainActivity_Delete.this, "Delete", Toast.LENGTH_SHORT).show();
Log.e("item", String.valueOf(listView.getAdapter().getItem(position)));
Log.e("name", String.valueOf(dataArrayList.get(position).getName()));
dataArrayList.remove(position);
listAdapter.notifyDataSetChanged();
break;
case 1:
// delete
break;
}
// false : close the menu; true : not close the menu
return false;
}
});
}
SwipeMenuCreator creator = new SwipeMenuCreator() {
#Override
public void create(SwipeMenu menu) {
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(
getApplicationContext());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.parseColor("#F45557")));
// set item width
deleteItem.setWidth(150);
deleteItem.setTitle("Delete");
deleteItem.setTitleColor(Color.WHITE);
deleteItem.setTitleSize(15);
// add to menu
menu.addMenuItem(deleteItem);
}
};
}
There is android.app.ActivityGroup but this class has been deprecated since API level 13. So, in essence you cannot embed an activity in another activity. You can reuse layout files, however. See Re-using Layouts with <include/> The best option is, of course, to make use of fragments. They are the standard for such things.

onNavigationItemSelected set active item position title on actionbar

ActionBar
Let me try and explain this, I have a dropdown Actionbar with the selectable items, Home, Contact Us and About.
When the main activity loads, the default item shown on the ActionBar dropdown is Home. OK.
When I select the second item (contact us), the CURRENT ACTIVITY (main activity) sets itself as “Contact Us” then..
..then the NEXT ACTIVITY (contact us) starts up but sets the ActionBar back to default “Home” instead of ‘contact us’
Please see my code below:
public class MainActivity extends Activity {
private boolean mNaviFirstHit = true;
….
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SpinnerAdapter sadapter = ArrayAdapter.createFromResource(this, R.array.actions, R.layout.dropdown_textcolor);
// Callback
OnNavigationListener callback = new OnNavigationListener() {
String[] items = getResources().getStringArray(R.array.actions)
#Override
public boolean onNavigationItemSelected(int position, long id) {
if (mNaviFirstHit) {
mNaviFirstHit = false;
return true;
}
if (items[position].equals("Home"))
{
System.out.println("Selected Home");
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
if (items[position].equals("Contact Us"))
{
System.out.println("Selected Contact US");
startActivity(new Intent(getApplicationContext(),ActionContact.class));
}
if (items[position].equals("About"))
{
System.out.println("Selected About");
startActivity(new Intent(getApplicationContext(),ActionAbout.class));
}

Change layout using a spinner in Android

I'm trying to change the layout of my activity according to my selection inside the spinner.
But after the first selection, the spinner become white and I'm not able to decide another selection.
The code I'm using is the following:
public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner spinner;
String[] options = { "Modulo1", "Modulo2" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, options);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter_state);
spinner.setOnItemSelectedListener(this);
}
int check = 0;
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
check = check + 1;
if (check > 1) {
int selState = spinner.getSelectedItemPosition();
switch (selState) {
case 0:
setContentView(R.layout.activity_main);
break;
case 1:
setContentView(R.layout.activity2_main);
break;
}
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
//
#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;
}
}
Any suggestion?
Thanks
The spinner you are referring to is in your first activity. After you have replaced the content, the spinner isn't there anymore. If you have another spinner in the second layout, you have to reconnect it and set the listener again.
Basically you have to run your onCreate stuff after every setContentView...
As a side note, whatever you are trying to do, this is probably not the way to go. To show another full layout, better use another activity.

Android menu item list navigation

I need help please could someone help me with switch and case...
I have 3 item in action bar item1, item2, item3 and i have 3 activity item1Activity.java, item2Activity, item2Activity.. I want to call those activity from menu when its item selected..
public class MainActivity extends Activity {
/** An array of strings to populate dropdown list */
String[] actions = new String[] {
"Item1",
"Item2",
"Item3"
};
protected int position;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Create an array adapter to populate dropdownlist */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, actions);
/** Enabling dropdown list navigation for the action bar */
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
/** Defining Navigation listener */
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch (itemPosition) {
case 1:
Intent i = new Intent();
i.setClass(getApplicationContext(), Item1Activity.class);
startActivity(i);
return true;
case 2:
Intent i = new Intent();
i.setClass(getApplicationContext(), Item2Activity.class);
startActivity(i);
return true;
}
}
};
/** Setting dropdown items and item navigation listener for the actionbar */
getActionBar().setListNavigationCallbacks(adapter, navigationListener);
}
}
Use MainActivity.this instead of getApplicationContext()
Activity is child of Context

Android Listview - can not select multiple items when using a cursor

I have a listview populated with data from a cursor using SimpleCursorAdapter. I want to make it so I can select multiple items with a checkbox against each item but I can only get it to check a single item at a time i.e. each time I select an item, it will clear the currently selected item.
It works fine if I populate the listview using an ArrayAdapter. I can select multiple items. So I dont know why it doesn't work with the SimpleCursorAdapter.
This is being created in a DialogFragment if that matters.
Really pulling my hair out on this, pleae help!!
Here's the code:
Cursor attributesCursor = mDBHelper.getItemAttributesbyType(menuID, itemID, "M");
getActivity().startManagingCursor(attributesCursor);
ListView lv = new ListView(this.getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
lv.setLayoutParams(params);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getActivity(), android.R.layout.simple_list_item_multiple_choice,
attributesCursor, new String[] { "AttributeDescription" },
new int[] { android.R.id.text1 },0);
attributesLinearLayout.addView(lv);
lv.setAdapter(adapter);
lv.setItemsCanFocus(false);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Edit :
Just to add some extra info, the multi choice listview works if i use this code, where "items" is a simple String array:
lv.setAdapter(new ArrayAdapter(this.getActivity(),
android.R.layout.simple_list_item_multiple_choice, items));
Also, this listview is being dynamically added to an existing Linearlayout (attributesLinearLayout) in a dialogfragment which contains other controls. I also tried extending other adapters, including the array adapter and customer item layouts but that again didnt allow me to select multiple items.
Please help!!
I would use the Contextual Action mode in this project if you wish to select multiple items in a listview. This is how it is done.
First of all, the code must extend a ListActivity and implement an ActionMode.Callback
In the onCreate medthod you need to code the following:
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "data list goes here" };
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
setListAdapter(adapter);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (mActionMode != null) {
return false;
}
selectedItem = position;
// Start the CAB using the ActionMode.Callback defined above
mActionMode = MyListActivityActionbar.this.startActionMode(MyListActivityActionbar.this);
view.setSelected(true);
return true;
}
});
}
The you need to call the show method:
private void show() {
Toast.makeText(MyListActivityActionbar.this, String.valueOf(selectedItem), Toast.LENGTH_LONG).show();
}
The following needs to be called each time the action mode is shown. It is 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
}
Then when the user selects a list item, the following method needs to be called:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menuitem1_show:
show();
// Action picked, so close the CAB
mode.finish();
return true;
default:
return false;
}
}
Finally when the user exits the selection:
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
selectedItem = -1;
}

Categories

Resources