Open Navigation Drawer at startup - java

I'm fairly new to android programming and developed an app that has a Navigation Drawer implemented. Per Google guidelines, i'd like the NavDraw to start open but am unable to do this.
This is my onCreate (I guess this is where I should implement this feature, right?)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, menuEntries);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.drawer);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerToggle = new ActionBarDrawerToggle(
this,
drawer,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
)
(...)

Use drawer.openDrawer(Gravity.LEFT);

You can use
drawer.openDrawer(Gravity.LEFT);
or
drawer.openDrawer(Gravity.RIGHT`);

Related

How to make Navigation drawer toggle button visible on Toolbar?

I am getting error message : ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
But I have called this in my code , still getting same error and the toggle button is not visible.
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//layout variables
mDrawerLayout = findViewById(R.id.main_drawer_layout);
mNavigationView = findViewById(R.id.main_nav_view);
mActionDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mActionDrawerToggle);
mActionDrawerToggle.syncState();
//changed as keeps throws NULLPOINTEXCEPTION
if(getSupportActionBar() != null) {
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// assigning ID of the toolbar to a variable
toolbar = (Toolbar) findViewById(R.id.my_toolbar);
// using toolbar as ActionBar
setSupportActionBar(toolbar); //set
ActionBar actionbar = getSupportActionBar(); //get
actionbar.setTitle("SharePixel");
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_home_menu);
}
setHomeAsUpIndicator() won't get executed as the if(getSupportActionBar() != null) condition shouldn't be valid in drawer-based activity, because it requires a custom Toolbar, and you call this condition before setting the custom toolBar.
This condition won't be achieved unless your activity theme has an ActionBar set in your styles/themes XML. (if you've no customized activity theme set, then it's inherited from the base application theme.
To fix this:
Make sure that your activity theme inherits from a NoActionBar decedent; for instance Theme.MaterialComponents.DayNight.NoActionBar (or if your activity doesn't have customized style; do that on the base application theme).
Remove the if(getSupportActionBar() != null) condition.

ActionBarDrawerToggle without custom toolbar

I want to use ActionBarDrawerToggle without having custom toolbar instead using current actionBar
my code goes as follow :
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.grid);
ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, drawerLayout, actionbar, R.string.app_name, R.string.app_name);
I know I should use :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
and use toolBar instead of action bar in ActionBarDrawerToggle but can I use ActionBarDrawerToggle without having new toolbar.
Error I get :
error: incompatible types: ActionBar cannot be converted to Toolbar
Solved that by removing actionBar my code now goes like this :
ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.grid, R.drawable.contact) {
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
// creates call to onPrepareOptionsMenu()
}
};

Trying to setDisplayHomeAsUpEnabled(true) in my Android app and it's giving me a NullPointerException

I have my app as a single Activity with different fragments. I navigate the fragments via the Navigation Drawer on the left. The thing is that I need to have different toolbars for each fragment.
What I did was set the toolbar in the layout of the fragment like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hectorviov.###.activity.HomeFragment">
<include
android:id="#+id/toolbar_logo"
layout="#layout/toolbar_logo" />
...
And set it as SupportActionBar on my fragment with with:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatActivity act = (AppCompatActivity)getActivity();
Toolbar mToolbar = act.findViewById(R.id.toolbar_logo);
act.setSupportActionBar(mToolbar);
}
It works correctly, the thing is, it doesn't show the hamburger icon on the left of the action bar. It shows me a warning:
W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
So I tried different ways to do this after googling a lot. The last one I tried is:
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
And no matter what I try, it always gives me a NullPointerException:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hectorviov.###/com.hectorviov.###.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
Please help!!
try with this
AppCompatActivity activity = (AppCompatActivity) getActivity();
if (activity != null) {
ActionBar ab = activity.getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
}
refer this link will help you.
doesn't show the hamburger icon?
you need to add ActionBarDrawerToggle object
public class DrawerActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private void setupDrawerLayout() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
drawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}

How to add button listener to ListView?

I have a custom list item XML with 2 Buttons and I want to get when one is clicked (and a way of knowing which one). My ListView is made with data from a database. This is what I use for populating the list:
private DBManager manager;
private Cursor cursor;
private ListView lista;
SimpleCursorAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.m_home_layout);
manager = new DBManager(this);
lista = (ListView) findViewById(R.id.listview);
String[] from = new String[]{manager.CN_NAME, manager.CN_DESCRIPTION, manager.CN_TIME, manager.CN_DAY};
int[] to = new int[] {R.id.list_title, R.id.list_description, R.id.list_time, R.id.list_day};
cursor = manager.cargarCursorData("data");
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to,0);
lista.setAdapter(adapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
I've seen people saying that you have to use bindView but I don't have anything like that and I don't know what it is. Sorry for my ignorance.
You can go through this tutorials for Creating Custom Adapter, in which you can get the which Button is clicked:
http://www.survivingwithandroid.com/2012/10/android-listview-custom-adapter-and.html
http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial

Adding tabs to app with navigationDrawerActivity

I am new to android and I am trying to learn by making a simple chat app in android studio.
I started by creating a navigation drawer. Now I want to add tabs to my app like this - chats / contact / calls below navigation drawer, but I could not find a simple step by step tutorial for doing this.
Before I started this chat app I worked on a simpler app and added some tabs to that old app like this:
public class MainActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources resources = getResources();
TabHost tabHost = getTabHost();
// First tab
Intent intentTabOne = new Intent().setClass(this, TabOneActivity.class);
TabSpec tabSpecTabOne = tabHost
.newTabSpec("Tab One")
.setIndicator("", resources.getDrawable(R.drawable.icon_one_config))
.setContent(intentTabOne);
// Second tab
Intent intentTabTwo = new Intent().setClass(this, TabTwoActivity.class);
TabSpec tabSpecSecondTab = tabHost
.newTabSpec("Tab Two")
.setIndicator("", resources.getDrawable(R.drawable.icon_two_config))
.setContent(intentTabTwo);
// Third tab
Intent intentTabThree = new Intent().setClass(this, TabThree.class);
TabSpec tabSpecSent = tabHost
.newTabSpec("Tab Three")
.setIndicator("", resources.getDrawable(R.drawable.icon_three_invitations_config))
.setContent(intentTabThree);
// add all tabs
tabHost.addTab(tabSpecTabOne);
tabHost.addTab(tabSpecTabTwo);
tabHost.addTab(tabSpecTabThree);
//set Windows tab as default
tabHost.setCurrentTab(0);
}
But now my main activity looks like this:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed()...
Also, since TabActivity is now deprecated and I think I should use fragment, I got a little bit lost. I would really appreciate if someone could point me in the right direction. Thanks in advance.
I'd suggest to use a TabLayout that you can put below your Toolbar/Actionbar, so you can use it in parallel with the navigation drawer.
This answer shows how setup the Tabbar with a ViewPager. In short: You need to include the Design Support Library to use the TabLayout. In your XML layout, put the TabLayout and the ViewPager somewhere below the Toolbar, such that it fits your intentions. Then you can setup the different tabs with their own Fragments in your Activity.

Categories