I am having a problem in trying to set up a Navigaion Drawer for a Android App. Here is the for the tutorial I was following. http://www.recursiverobot.com/post/59404388046/implementing-the-new-navigation-drawer-in-android
This is the error that I get when I try to run the code:
Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
I believe my problem is somewhere in my xml code. Here is my onCreate() method MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
mTitle = "test";
mPlanetTitles = new String[]{"Home", "Settings", "Application", "Contact Us"};
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
ActionBar myBar = getSupportActionBar();
myBar.setDisplayHomeAsUpEnabled(true);
myBar.setHomeButtonEnabled(true);
/*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);*/
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view){
getActionBar().setTitle(mTitle);
}
public void onDrawerOpen(View drawerView){
getActionBar().setTitle(mTitle);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener{
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}
And the XMLs:
drawer_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!--drawer_list_item.xml-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/text_view"
>
</TextView>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--content_main.xml-->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111">
</ListView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--activity_main.xml-->
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
menu_main.xml
<!--menu_main.xml-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/HiBen" android:title="#string/HiBen"
android:orderInCategory="100" app:showAsAction="never" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<!--AndroidManifeset.xml-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stateofnebraska.designstudio.mobileapp" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I apologize for the excessive question but I was no sure with how much information would be needed. I have tried putting the code that is currently in the content_main.xml in the acivity_main.xml, tried using just getActionBar(). I believe the issue might be somewhere in my ActionBarDrawerToggle declaration. Any help would be appreciated.
Related
Please, I integrated the new Navigation Controller Component, using navHostFragment. My project runs on one activity-to-many fragments. It got a drawer & toolbar. I read that it is possible to set the ids in both menus (drawer, toolbar), to same ids in nav_graph for navigation when onClick event occurs.
activity_main.xml
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/colorPrimary"
android:id="#+id/appContainer"
tools:context=".MainFragment"
tools:openDrawer="start"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_toolbar"
app:title="#string/app_name"
android:theme="#style/AppTheme.AppBarOverlay" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph"
android:transitionName="#string/transition_type_container" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="#color/drawer_selector"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/menu_left_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
nav_graph.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/nav_graph"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/mainFragment"
android:name="com.emexrevolarter.worksmart.MainFragment"
android:label="fragment_holder"
tools:layout="#layout/fragment_holder" />
<fragment
android:id="#+id/aboutFragment"
android:name="com.emexrevolarter.worksmart.AboutFragment"
android:label="fragment_about"
tools:layout="#layout/fragment_about" />
<fragment
android:id="#+id/cameraFragment"
android:name="com.emexrevolarter.worksmart.CameraFragment"
android:label="fragment_camera"
tools:layout="#layout/fragment_camera" />
<fragment
android:id="#+id/accountFragment"
android:name="com.emexrevolarter.worksmart.AccountFragment"
android:label="fragment_account"
tools:layout="#layout/fragment_account" />
<fragment
android:id="#+id/formCreateAccountFragment"
android:name="com.emexrevolarter.worksmart.forms.FormCreateAccountFragment"
android:label="fragment_form_create_acount"
tools:layout="#layout/fragment_form_create_acount" />
<fragment
android:id="#+id/formUserLoginFragment"
android:name="com.emexrevolarter.worksmart.forms.FormUserLoginFragment"
android:label="fragment_form_user_login"
tools:layout="#layout/fragment_form_user_login" />
<fragment
android:id="#+id/helpFragment"
android:name="com.emexrevolarter.worksmart.HelpFragment"
android:label="fragment_help"
tools:layout="#layout/fragment_help" />
<activity
android:id="#+id/cameraXActivity"
android:name="com.emexrevolarter.worksmart.media.CameraXActivity"
android:label="activity_camera"
tools:layout="#layout/activity_camera" />
<fragment
android:id="#+id/placeholderFragment"
android:name="com.emexrevolarter.worksmart.ui.main.PlaceholderFragment"
android:label="fragment_main"
tools:layout="#layout/fragment_main" />
<activity
android:id="#+id/mainActivity"
android:name="com.emexrevolarter.worksmart.MainActivity"
android:label="MainActivity" />
<activity
android:id="#+id/splashActivity"
android:name="com.emexrevolarter.worksmart.SplashActivity"
android:label="splash_animation"
tools:layout="#layout/splash_animation" />
<navigation android:id="#+id/nested_settings"
app:startDestination="#id/settingsFragment">
<fragment
android:id="#+id/settingsFragment"
android:name="com.emexrevolarter.worksmart.SettingsFragment"
android:label="fragment_settings"
tools:layout="#layout/fragment_settings" />
<fragment
android:id="#+id/formEditAboutFragment"
android:name="com.emexrevolarter.worksmart.forms.FormEditAboutFragment"
android:label="fragment_form_edit_about"
tools:layout="#layout/fragment_form_edit_about" />
<fragment
android:id="#+id/listUsersFragment"
android:name="com.emexrevolarter.worksmart.view.ListUsersFragment"
android:label="fragment_list_users"
tools:layout="#layout/fragment_list_users" />
</navigation>
<navigation android:id="#+id/nested_tips"
app:startDestination="#id/searchFragment">
<fragment
android:id="#+id/searchFragment"
android:name="com.emexrevolarter.worksmart.SearchFragment"
android:label="fragment_search"
tools:layout="#layout/fragment_search" />
<fragment
android:id="#+id/tipsFragment"
android:name="com.emexrevolarter.worksmart.TipsFragment"
android:label="fragment_tips"
tools:layout="#layout/fragment_tips" />
</navigation>
<navigation android:id="#+id/nested_tasks"
app:startDestination="#id/tasksFragment">
<fragment
android:id="#+id/tasksFragment"
android:name="com.emexrevolarter.worksmart.TasksFragment"
android:label="fragment_tasks"
tools:layout="#layout/fragment_tasks" />
</navigation>
<navigation android:id="#+id/nested_notifications"
app:startDestination="#id/notificationsFragment">
<fragment
android:id="#+id/notificationsFragment"
android:name="com.emexrevolarter.worksmart.NotificationsFragment"
android:label="fragment_notifications"
tools:layout="#layout/fragment_notifications" />
</navigation>
<navigation android:id="#+id/nested_report"
app:startDestination="#id/reportFragment">
<fragment
android:id="#+id/reportFragment"
android:name="com.emexrevolarter.worksmart.ReportFragment"
android:label="fragment_report"
tools:layout="#layout/fragment_report" />
</navigation>
menu_left_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
android:id="#+id/leftDrawer"
tools:viewBindingIgnore="true"
tools:context=".MainActivity">
<group
android:id="#+id/group1"
android:checkableBehavior="single">
<item android:id="#+id/aboutFragment"
android:title="#string/drawer_about"
android:icon="#drawable/ic_baseline_info_24"
android:iconTint="#color/colorPrimary" />
<item android:title="#string/drawer_label">
<menu>
<item android:id="#+id/helpFragment"
android:title="#string/drawer_help"
android:icon="#drawable/ic_baseline_help_24"
android:iconTint="#color/colorPrimary"/>
</menu>
</item>
</group>
main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/notificationsFragment"
android:icon="#drawable/ic_baseline_notifications_24"
app:showAsAction="ifRoom"
android:visible="false"
android:title="Notification">
</item>
<item
android:id="#+id/searchFragment"
android:icon="#drawable/ic_baseline_search_24"
app:showAsAction="ifRoom"
android:visible="false"
android:title="Search">
</item>
<item
android:id="#+id/accountFragment"
android:icon="#drawable/ic_baseline_lock_open_24"
app:showAsAction="ifRoom|withText"
android:title="Account">
</item>
Everything works as per navigation if I use the below:
subText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// action_global_aboutFragment
NavController navController = Navigation.findNavController(v);
navController.navigate(R.id.nested_settings);
}
});
More Info:
minSdkVersion 23
targetSdkVersion 30
Implementation:
// Java language implementation
def nav_version = "2.3.3"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
I need a solution to this, as regards IDs to IDs linking. I love the concept. Who got this working on Android Java?
Solved!
Listeners are needed for both Drawer & Toolbar
// setup click listeners for Drawer & Toolbar
drawerView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
if(id == R.id.accountFragment) {
// add your code here
} else {
NavigationUI.onNavDestinationSelected(menuItem, navController);
}
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
// drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle Menu item selection
switch (item.getItemId()) {
case R.id.aboutFragment:
// add your code here
return true;
default:
// default function;
NavigationUI.onNavDestinationSelected(item, navController);
return true;
}
// return false;
}
});
Thank you.
My MainActivity.xml is as below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_appbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_appbar"
app:menu="#menu/activity_appbar_drawer" />
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_1" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_2" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_3" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
My intention is to have an appbar with navigation drawer (icon on left), and options menu (icon on right). However, it appears to be stuck underneath the system default. Here is an image showing its preview:
Additionally, my styles.xml is:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Changing AppCompat.Light.DarkActionBar to NoActionBar has no effect. The appbar with "AppName" just remains above the other appbar (which should show "BEEP" as text, in addition to the two icons for navbar and options menu) - NoActionBar is also defined in the manifest for the activity:
<activity
android:name=".MainActivity"
android:label="BEEP"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here is the relevant part of the MainActivity.java, if needed:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
...
So, how can I get the toolbar (ID is "toolbar") to display instead of the default appbar that currently contains the text "AppName" in the above image? I also followed this, however there is no change in the result.
EDIT 1: The following screenshot shows my user-defined appbar appearing underneath the default, for some reason. I have also included my app_bar_appbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_appbar" />
</android.support.design.widget.CoordinatorLayout>
As you are having Theme.AppCompat.Light.DarkActionBar to your Application tag of AndroidManifest all activity will include default actionbar.
Create one more theme in your styles.xml with NoActionBar and apply this to your NavigationDrawerActivity.
i.e.
<style name="navigation_theme" parent="Theme.AppCompat.Light.NoActionBar"/>
Set it to your Navigation Activity
<activity
android:name=".NavigationDrawerActivity"
android:label="#string/title_activity_navigation_drawer"
android:theme="#style/navigationtheme" />
Example of default theme with ActionBar and NavigationDrawerActivity with NoActionBar theme
EDIT
As #Talisman said, it was the issue with the TabLayout and ViewPager
of DrawerLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.and.NavigationDrawerActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/holo_blue_light"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!--Your content in this FrameLayout-->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.and.NavigationDrawerActivity">
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_navigation_drawer"
app:menu="#menu/activity_navigation_drawer_drawer" />
</android.support.v4.widget.DrawerLayout>
Now Set the TabLayout and ViewPager in other Fragment and Load that fragment in this FrameLayout on onCreate method of NavigationDrawerActivity
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.replace(R.id.frame_container, fragment);
tr.commitAllowingStateLoss();
where fragment is other fragment which contains your TabLayout and ViewPager.
Everything is looking perfect as it should be. If you just want to change the title of toolbar then you just have to add few lines of code in MainActivity.java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// add few lines here like...
toolbar.setTitle("Your title here");
// ....
setSupportActionBar(toolbar);
If right option menu not showing then override these two method in MainActivity.java
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return false;
}
return super.onOptionsItemSelected(item);
}
First of all select "AppTheme" from layout preview
Select Project Theme >> AppTheme.NoActionBar
To create option menu for your Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_item:
//do need full stuff
break;
case R.id.menu_settings:
//do need full stuff
break;
}
}
create a menu.xml in res>menu folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_item"
android:title="Item"/>
<item
android:id="#+id/menu_settings"
android:title="Settings" />
</menu>
I want to create a NavigationView with checkbox or switch , I just want to have this feature in my navigation to items switch and it should work just like this : When I turn one switch to another it changes to off state. What I did to have a switch is, this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SwitchCompat
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/switchs"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_menu1"
android:icon="#mipmap/ic_launcher"
android:title="Menu 1" />
<item
android:id="#+id/nav_menu2"
android:icon="#mipmap/ic_launcher"
android:title="Menu 2" />
<item
android:id="#+id/nav_menu3"
app:actionLayout="#layout/action_view_switch"
android:icon="#mipmap/ic_launcher"
android:title="Menu 3" />
<item
android:id="#+id/nav_menu4"
app:actionLayout="#layout/action_view_switch"
android:icon="#mipmap/ic_launcher"
android:title="Menu 4" />
</group>
</menu>
But I can not have a value or status a switch and have a listener to see a change status
first of all create a layout action_view_checkbox.xml as below
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/checkbox" />
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/checkbox"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:text="" />
</RelativeLayout>
and add layout as an item to the menu
<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/check_box_menu"
app:actionLayout="#layout/action_view_checkbox"
android:title="Title" />
</menu>
And check click event as
Menu menu = navigationView.getMenu();
MenuItem menuItem = menu.findItem(R.id.check_box_menu);
View actionView = MenuItemCompat.getActionView(menuItem);
actionView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I tried various solution to get toolbar title but none of them worked. like using and others. What went wrong with my code. can you advice. I m getting menu and their options but Title of toolbar no luck.
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("Hello");
Below is my style.xml code:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
My mainactivity code:
package jss.customtoolbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("Hello");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
menu.xml code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/miCompose"
android:icon="#mipmap/ic_launcher"
android:title="Compose"
app:showAsAction="ifRoom" />
<item
android:id="#+id/miProfile"
android:title="Profile" />
</menu>
Toolbar code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:title="Hello">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</LinearLayout>
and activity_main.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="jss.customtoolbar.MainActivity">
<include
layout="#layout/tool"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
You can set title to toolbar for reference no need to get toolbar of support.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Hello");
setSupportActionBar(toolbar);
}
Toolbar xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:title="Hello">
</android.support.v7.widget.Toolbar>
</LinearLayout>
activity xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="jss.customtoolbar.MainActivity">
<include
layout="#layout/tool"/>
</LinearLayout>
I'm using an example out of the android programming book I'm going through. The point of this exercise is that when I click the "about" button, a new activity should start and display some text. For some reason that text is not showing up even though the text shows up in the graphical layout in my IDE. I'm using my phone as the emulator and my phone is running Android 4.0.3. I'm using eclipse. Here's my code:
Main activity:
package org.example.sodoku;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Sudoku extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View continueButton= findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton= findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton= findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton= findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
Main xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background"
android:gravity="center"
android:padding="35dip">
<TextView
android:text="#string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:textSize="24.5sp" />
<TableLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*">
<TableRow>
<Button
android:id="#+id/continue_button"
android:text="#string/continue_label" />
<Button
android:id="#+id/new_button"
android:text="#string/new_game_label" />
</TableRow>
<TableRow>
<Button
android:id="#+id/about_button"
android:text="#string/about_label" />
<Button
android:id="#+id/exit_button"
android:text="#string/exit_label" />
</TableRow>
</TableLayout>
</LinearLayout>
About Class:
package org.example.sodoku;
import android.app.Activity;
import android.os.Bundle;
public class About extends Activity {
protected void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
About xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip">
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" >
</TextView>
</ScrollView>
[EDIT] Forgot about strings xml and manifest:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Sudoku!</string>
<string name="app_name">Sudoku</string>
<string name="main_title">Android Sodoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<color name="background">#3500ffff</color>
<string name="exit_label">Exit</string>
<string name="about_title">About Android Sudoku</string>
<string name="about_text">fuck your ethnicity</string>
</resources>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sodoku"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Sudoku"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".About"
android:label="#string/about_title">
</activity>
</application>
</manifest>
Any help would be much appreciated, thank you.
You have misspelled onCreate() method in your About activity (compare OnCreate()and onCreate()), so you don't actually override base class method. Replace your onCreate with this one:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
It may help you. use this one as about.xml.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" >
</TextView>
</LinearLayout>
</ScrollView>