I have seen many examples around, but I could not understand how to have the navigation drawer, always open in the tablet. I'm using this navigation drawer https://github.com/kanytu/android-material-drawer-template. I will have to create another xml file? and in OnCreate () what kind of code I need to add?
public class MainActivity extends AppCompatActivity implements NavigationDrawerCallbacks {
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
..
..
}
#Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
public void onResume() {
super.onResume();
}
}
I do not have the precise knowledge but a workaround could be to make a different xml file for large devices without the DrawerLayout so when it runs on a tablet it will return null and no other boolean will be neede in the code and this will make the coding part very simple :-) kudos !
Related
Is it possible to change extended drawer's style and use it in activity? If yes how? I'm trying to use this drawer making it transparent in StoreActivity
If you are curious here is my code. HomeNavigation.java, Drawer code
public class HomeNavigation extends AppCompatActivity {
public DrawerLayout mDrawerLayout;
public ActionBarDrawerToggle mToggle;
public Toolbar mToolbar;
public NavigationView mNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_navigation);
mToolbar = (Toolbar) findViewById(R.id.navigation_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Welcome Back!");
mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/**ON NAVIGATION ITEM SELECTED*/
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// enabling drawer toggle by clicking on the app icon.
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Class I'm trying to use it in StoreActivity.java
public class StoreActivity extends HomeNavigation {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_drawer_navigation_transparent, null, false);
mDrawerLayout.addView(contentView, 0);
getSupportActionBar().setTitle("");
mNavigationView.getMenu().getItem(1).setChecked(true);
}
}
I want to define a single nav drawer I can use throughout my application. I followed the instructions of the chosen answer here as my first approach: Same Navigation Drawer in different Activities
I made a few modifications, namely calling onCreateDrawer from inside an overridden onCreate. I updated my subsequent activity to extend DashboardActivity ("base activity" from the example). When I launch my second activity I get a null pointer exception complaining that the nav UI doesn't exist when onCreateDrawer tries to set the toggle listened on the drawer.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference
Here are the base (dashboard) and subsequent (log workout) activity - please ask if there is other code you want to see. The code for the UI of the drawer and associated activity are what came out of the box when creating a new Nav Drawer Activity in Android Studio.
DashboardActivity.java
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
onCreateDrawer();
Realm realm = Realm.getDefaultInstance();
RealmQuery<Exercise> query = realm.where(Exercise.class);
RealmResults<Exercise> result = query.findAll();
Log.d(TAG, "There are " + result.size() + " exercises ready for use.");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent logWorkoutIntent = new Intent(getApplicationContext(), LogWorkoutActivity.class);
startActivity(logWorkoutIntent);
}
});
//TODO: Remove this sign out button
Button signOutButton = (Button) findViewById(R.id.button_sign_out);
signOutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Signed out", Toast.LENGTH_LONG).show();
}
});
//TODO: Remove this data tester
updateUserName();
}
//#Override
protected void onCreateDrawer() {
Log.d(TAG, "onCreateDrawer called");
//super.onCreate(savedInstanceState);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
...
-
LogWorkoutActivity.java
public class LogWorkoutActivity extends DashboardActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_log_workout);
setContentView(R.layout.activity_log_workout);
super.onCreateDrawer();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
I think you are missing a drawer with id drawer_layout in your activity_log_workout layout.
In order for this approach to work, you must have a DrawerLayout with id drawer_layout in all your activity layouts that should have the drawer.
I also noticed something peculiar with your code. In every activity that extends DashboardActivity you are first setting setContentView(R.id.activity_dashboard), then calling onCreateDrawer(), then you are changing the content view and creating the drawer again. I think this a very suboptimal approach.
I suggest you create a BaseDrawerActivity class to encapsulate the drawer creation and UI binding logic. Then you just extend it in the activities where you need a drawer. You can do it like this:
public abstract class BaseDrawerActivity extends AppCompatActivity {
// move all your drawer related fields here
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate();
setContentView(getLayoutResId());
// the same method you have right now
onCreateDrawer();
}
/*
* Extending activities use this class to supply the
* id of their layout file. This way you can set the view
* only once and there is no need to create the drawer twice.
*/
#LayoutResId
public abstract int getLayoutResId();
}
I am using DrawerToggle with toolbar in my project. I have a requirement to make drawer toggle in active (i.e it should not open or close on click or swipe). But I am completely struct how to achieve the things by making the toggle event standstill. I know that my question is quite different but my requirement is to control dynamically on a runtime to control the property of drawertoggle.
I am also posting piece of code I am using for drawertoggle in my project for your reference
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
findViews();
createFragments ();
materialColorNames = getResources ().getStringArray(R.array.color_names);
drawerAdpater = new DrawerAdapter(this,materialColorNames);
mDrawerList.setAdapter(drawerAdpater);
if (toolbar != null) {
changeTitleText ("Droid");
toolbar.setTitleTextColor (getResources ().getColor (R.color.droid_white));
setSupportActionBar (toolbar);
}
initDrawer();
updateFragment (0);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
protected void onResume () {
super.onResume ();
}
private void changeTitleText (CharSequence title) {
toolbar.setTitle(title);
}
private void createFragments () {
myProjectsFragment = new MyProjectsFragment();
}
private void findViews() {
mDrawerList = (ListView) findViewById(R.id.left_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
}
private void initDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// mDrawerLayout.closeDrawer(mDrawerList);
}
};
mDrawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
private void updateFragment(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
switch (position) {
case 0:
ft.replace(R.id.content_frame, myProjectsFragment);
break;
}
Kindly please help me with this solution. I am seraching this to be done for the long time. Thanks in advance. Please let me know through comments if my question is not clear.
In your onCreate() after drawer layout and list are found call:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerList);
drawerToggle.setDrawerIndicatorEnabled(false);
From the Javadoc:
public void setDrawerLockMode (int lockMode, View drawerView)
Enable or disable interaction with the given drawer.
This allows the application to restrict the user's ability to open or close the given drawer. DrawerLayout will still respond to calls to openDrawer(int), closeDrawer(int) and friends if a drawer is locked.
I have a Navigation Drawer in my Main Activity. Now i need to call all the fragments from onNavigationDrawerItemSelected method. But the odd part is that I don't have fragments.All I have activities. So i extended it with Fragment Activity.
MainActivity is the navigation Drawer Activity.
FeedListActivity is the Fragment activity.
Now i need to add the Feedlistactivity as a Fragment in my Main Activity. How can i achieve this ?
I saw many questions Related to this. But i can't relate it with me.
Main Activity
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks{
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
if (position == 1){
Fragment newFragment = new Fragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment);
transaction.commit();
}
}
}
FeedListActivity
public class FeedListActivity extends FragmentActivity{
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
private static final String TAG = "FeedListActivity";
private ListView NewsView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
ProgressBarCircularIndetermininate progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_list);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
progressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
NewsView = (ListView) findViewById(R.id.feed_list);
NewsView.setAdapter(listAdapter);
// making fresh volley request and getting json
// Adding request to volley request queue
AppController.getInstance().addRequest(gsonRequest, TAG);
getimg();
}
}
FeedListActivity is the Fragment activity.
Now i need to add the Feedlistactivity as a Fragment in my Main
Activity. How can i achieve this ?
A fragmentActivity is not a fragment. It's an activity with support for fragments, it was introduced to make older android builds backward compatible with fragments -- if you're targeting newer API's only you can ignore it. To make make FeedListActivity() a fragment...you must extend from fragment.
Fragment newFragment = new Fragment();
This line here should be creating a new instance of FeedList after you make it a fragment.
I want my navigation drawer to be disabled as the application loaded, and if user to some certain tasks it would be enabled.
in brief is there a way to disable navigation drawer's toggle button, and enable it again with subject to a user action.
Edit :
I have updated my activity as follows;
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
private NavigationDrawerFragment mNavigationDrawerFragment;
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
DrawerLayout navigationDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
navigationDrawerLayout);
navigationDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
if (item.getItemId() == android.R.id.home) {
}
return super.onOptionsItemSelected(item);
}
...
}
but when the application launches I can open navigation drawer, which I don t desire
You just have to lock and unlock your DrawerLayout using DrawerLayout setDrawerLockMode() method.
So, for locking your DrawerLayout in close mode, use:
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
If you prefer to lock it in open mode, use drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
Finally, when you want to unlock your DrawerLayout, use:
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
write condtion in onoptionsitemselected and block it
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if(yourconditionsatisfied)
{
if (mDrawerLayout.isDrawerOpen(mDrawerLinearLayout)) {
mDrawerLayout.closeDrawer(mDrawerLinearLayout);
} else {
mDrawerLayout.openDrawer(mDrawerLinearLayout);
}
}
}
return super.onOptionsItemSelected(item);
}