Android menu item list navigation - android

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

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));
}

Call notifyDataSetChanged() on adapter from menu

I have an app that creates custom array adapter called StabelArrayAdapter which based on the ArrayAdapter class. It is created in the onCreate() method of the activity and I want to refresh it from a menu when the view changes. I can't access the adapter from the menu and I can't make it public.
How can I refresh the list view from a menu?
Greg
public void onCreate(Bundle savedInstanceState) {
final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, listNames);
listview.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.list_add:
Toast.makeText(getApplicationContext(), "Add new list" , Toast.LENGTH_LONG)
.show();
return true;
case R.id.list_delete:
//change data here
final ListView listview = (ListView) findViewById(R.id.ListLists);
//--> Cannot resolve symbol 'adapter'
adapter.notifyDataSetChanged();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You need to make the adapter a class member and then you can reference it.
private StableArrayAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, listNames);
listview.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.list_add:
Toast.makeText(getApplicationContext(), "Add new list" , Toast.LENGTH_LONG)
.show();
return true;
case R.id.list_delete:
//change data here
final ListView listview = (ListView) findViewById(R.id.ListLists);
adapter.notifyDataSetChanged();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Navigation Drawer to switch between activities

I have browsed the website for a bit and I can't find an answer to my problem, I am trying to get my Navigation Drawer to switch between activities instead of fragments. I have tried switch statements and all that does is crash the app, I don't know how to get the separate elements of the drawer in order to set them up so that if one is pressed, it will go to this page and if the other is pressed it will go to this page etc etc.
Here's my code,
package com.example.ColeraineTown;
imports...
public class HomeScreen extends Activity {
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// 2.1 create ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
// 2.2 Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
// 2.3 enable and show "up" arrow
getActionBar().setDisplayHomeAsUpEnabled(true);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
drawerLayout.closeDrawer(drawerListView);
}
}
}
I have been at this all day, trying to fix it and get it working, but no luck. It took me so long to get the actual drawer working in the first place it would be a shame to see it all gone.
If you guys have the answer to being able to switch between the activities, that would be great!
Suppose you have 5 items (from 0 index to 4), each index identifying an Activity of your project. You can create a method selectItem(int position)to know what drawer item has been chosen by user.
public void selectItem(int position) {
Intent intent = null;
switch(position) {
case 0:
intent = new Intent(this, Activity_0.class);
break;
case 1:
intent = new Intent(this, Activity_1.class);
break;
...
case 4:
intent = new Intent(this, Activity_4.class);
break;
default :
intent = new Intent(this, Activity_0.class); // Activity_0 as default
break;
}
startActivity(intent);
}
Finally, add this method to your DrawerItemClickListener :
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
drawerLayout.closeDrawer(drawerListView);
}
}
It's easier than using Fragments, I think !!!

Multiple Selection on ActionBarSherlock

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;
}

Categories

Resources