I am using the sample code from the developer.android.com for navigation drawer. I am unable to know what exactly needs to be changed to start activities instead of images which now appear. So what parts do I need to delete so that I can make listviewitemclick to open activities?
I am working here
private void selectItem(int position) {
switch(position){
case 0:
Intent a = new Intent(MainActivity.this, sampleopen.class);
startActivity(a);
}
}
The problem is that it opens the second activity first then when we press back it goes to first activity and there the drawer is implemented
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
Here you define a listener - you need to provide your own instead of new DrawerItemClickListener() and there you'll be able to launch activities according to position received.
// set the on item click listener for the listview object
mNavigationListView.setOnItemClickListener(mOnNavigationItemClickListener);
// handle clicks here
private AdapterView.OnItemClickListener mOnNavigationItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) then launch ACtivity #1
//....
}
};
Related
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Data data = mListDatas.get(i);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ThirdFragment thirdFragment = new ThirdFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("detail", data);
thirdFragment.setArguments(bundle);
transaction.replace(R.id.content,ThirdFragment).addToBackStack(null).commit();
BottomNavigationView navigation = (BottomNavigationView)view.findViewById(R.id.navigation_bottomview);
navigation.setSelectedItemId(R.id.navigation_third_tab);
}
});
In the above code i need to open a new fragment on the bottom tab from a list item click,
i am able to open a new fragment but the current tab remains unchanged i.e, after redirecting to the third fragment on third bottom tab ,the current tab i.e, Second Tab is active.
How can i change the active state of bottom tab from second tab to third tab on the list item click ??
Your activity needs to implement the BottomNavigationView.OnNavigationItemSelectedListener. This can be done simply by adding implements BottomNavigationView.OnNavigationItemSelectedListener after your class name, this is done so you can get the click callback whenever one of the bottom menu items are clicked on.
Then you need to override the onNavigationItemSelected function so add the following code to your activity.
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.tab_1:
handleFragmentChange(Fragment_1.newInstance());
return true;
}
return false;
}
Also add navigation.setOnNavigationItemSelectedListener(this); in the same activity once you have found the view using findViewById. This is done so that the system knows that your class is handling the event when a bottom menu is clicked so it calls the above function in your class.
The return true is what notifies the android system that it needs to switch the tab.
if this code locates in fragment then you need to replace
BottomNavigationView navigation = view.findViewById(R.id.navigation_bottomview);
navigation.setSelectedItemId(R.id.navigation_third_tab);
before
...commit();
and add getRootView() because your BottomNavigationView locate inside main container - root activity
BottomNavigationView navigation = view.getRootView().findViewById(R.id.navigation_bottomview);
navigation.setSelectedItemId(R.id.navigation_third_tab);
I have an application where onItemClick() activity determines which item in the drawer is clicked and the corresponding activity is started. However, when if I am already at the 'Settings' screen and I open the drawer and click on 'Settings' again, it starts a new settings activity. How can I optimize my code here to just detect that user was already on the Settings screen and therefore the drawer should just slide close?
Here's my code:
//#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Drawer.closeDrawers();
Intent i = new Intent(DrawerActivity.this, SignUpActivity.class);
switch(position){
case 7:
i = new Intent(DrawerActivity.this, UserSettingActivity.class);
break;
}
startActivity(i);
For that you can store that last clicked number in one variable.
Then next time do check whether current clicked position is not equal to last store position
If it's not equal launch new screen and updated last clicked position else don't do nothing
So I've searched allot about navigation drawers here, and when I was pointed to a tutorial from an answer in another persons question. I did so.
I successfully managed to create and style the nav drawer to my liking.
But now I've been searching tirelessly on how I can launcher activities from the navigation drawer. I've managed to get some code into the the MainActivity but upon clicking the item it does not launch anything? All activities are define in the Manifest. I decided to use Toasts as a trail and error, still no luck.
here's my code for nav drawer, and launch activity.
// Drawer Activity
// 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));
// Run Activity from drawer
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
And this is my DrawerItemClickListener method
private class DrawerItemClickListener implements ListView.OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 0:
Intent a = new Intent(this, AppInfo.class);
startActivity(a);
break;
case 1:
Intent b = new Intent(getBaseContext(), WelcomeActivity.class);
startActivity(b);
}
}
}
Repalce this with MainActivity.this like that:
Intent a = new Intent(MainActivity.this, AppInfo.class);
startActivity(a);
Also Change that
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
replace
drawerListView.setOnItemClickListener(this);
Check there for Custom Adapter
Intent abc = new Intent(CurrentActivityName.this,TargetActivityName.class);
startActivity(abc);
This is how I've been doing it, directly referencing each activities name.
I asked this question previously and thought it was working and deleted the question, but since have found this isn't working. I have a ViewPager in my app, with each Fragment of the ViewPager holding a ListView. When an item in the ListView is selected, I am starting a new activity in onListItemClick:
#Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent browser = new Intent(activity, Browser.class);
startActivity(browser);
}
The problem is that when I press either the back button, or the Home button in the Actionbar, the ViewPager has reset itself to show the first fragment.
This is the code I'm using for the Home button:
#Override
public boolean onOptionsItemSelected(final MenuItem item)
{
if (item.getItemId() == android.R.id.home) {
Intent intent = new Intent(this, Main.class);
startActivity(intent);
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
It was recommended, in my original question, to set the launch mode in the Android Manifest to singleTop, which I did for the startup activity as well as the activity launched from the ListView, but that didn't seem to fix it.
I thought about passing the ViewPager's position to the new activity, then retrieving it in onResume of the original activity and manually setting the ViewPager's position, but that seems really brute force to me.
I'm developing for android 3+
In my action bar i have a drop-down list(see how to hide/unhide the actionbar list on android 3? for the dropdown i intend). The problem is i need to do a certain action when the user selects something, but Android calls onNavigationItemSelected() as soons as it draws the view, so no selection actually happened.
How can i detect if the user actually pressed something and it is not a fake call from android ?
public class ListDittaListener implements OnNavigationListener{
private BaseActivity activity;
private ListDittaListener()
{
}
public ListDittaListener(BaseActivity activity)
{
this.activity = activity;
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId)
{
MyApp appState = ((MyApp)this.activity.getApplicationContext());
appState.setDittaSelezionata( (int) itemId);
SharedPreferences settings = this.activity.getSharedPreferences(MyApp.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("ditta_id_selezionata", (int) itemId);
////////restart activity this.activity.recreate();
return false;
}
}
You can easily just ignore the first call to onNavigationItemSelected if you like:
public class Whatever implements OnNavigationListener {
private boolean synthetic = true;
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (synthetic) {
synthetic = false;
return true;
}
// do whatever you really wanted here
}
}
Method onNavigationItemSelected(int itemPosition, long itemId) will be called anyway by the action bar.
What you may want to do is to tell action bar what itemPosition it should pass to the method on the first call. (In other words, to tell action bar what navigation item should be set after activity is created). Here is the code:
mActionBarMenuSpinnerAdapter = ...;
mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
mActionBar.setListNavigationCallbacks(mActionBarMenuSpinnerAdapter, this);
mActionBar.setSelectedNavigationItem(###your_default_navigation_item_here###);
After doing this you can solve your problem by applying changes in the onNavigationItemSelected(int itemPosition, long itemId) if only itemPosition is different.
The android system will call onNavigationItemSelected(0, 0) after the activity is setup. (Which means later than onResume()).
As other guys mentioned, you'd better not do any hack like ignore first call, otherwise the android system won't call onNavigationItemSelected() again when you select the first index. (The system thought the first item is already selected)
My solution is call actionbar.setSelectedNavigationItem(the real item# you want) after you setup the actionbar. Then the system will call onNavigationItemSelected() twice. First onNavigationItemSelected(0, 0) and then the onNavigationItemSelected(the real item#).
Well I cannot see anything wrong in your current code.
How did you create your dropdown elements. And what element is "select" by Android after the view is created. And what are your doing in your onCreate method where the ActionBar is initialized.
I did it as instructed here and it worked for me:
http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown
I have viewpager with fragments and I need set custom action bar for every fragment in pager
In desired page I have navigation list, fragment fires onNavigationItemSelected automatically when I swipe pages, want to avoid this behavior and run tasks only if I selected nav item manually.
public class MyFragment extends Fragment implements ActionBar.OnNavigationListener {
private boolead fireReady = false;
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// every time make it false, this method invoked on swipe action
fireReady = false;
if (isVisibleToUser) {
// setup actionbar, you also can setup action bar in activity
String[] array = getActivity().getResources().getStringArray(R.array.users_order);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, array);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActivity().getActionBar().setListNavigationCallbacks(adapter, this);
}
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (fireReady) {
// task fire only when you directly press navigation item
UsersTask task = new UsersTask(getActivity());
task.setTaskListener(this);
task.execute(usersUrls[itemPosition]);
} else {
// make it true first time when page displayed
fireReady = true;
}
return false;
}
}
}