Java android NavigationView with checkbox - java

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

Related

IDs to IDs linking on menus of drawer and toolbar to nav_graph not working

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.

customize bottom navigation bar item layout

I want create following customized bottom navigation bar. How I can set custom layout for menu item? or is there exists ready library for creating this type bottom bar easily?
My code below work fine:
First, you need create a Bottom Navigation Activity template project in Android Studio and apply my code below:
button_custom.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#drawable/button_border"
android:src="#drawable/ic_baseline_add_24"/>
</FrameLayout>
button_border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="#android:color/holo_blue_light"/>
</shape>
ic_baseline_add_24.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#android:color/white"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#android:color/white"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" />
</vector>
bottom_nav_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_dashboard"
android:title="" />
<item
android:id="#+id/navigation_notifications"
android:icon="#drawable/ic_notifications_black_24dp"
android:title="#string/title_notifications" />
</menu>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<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:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
//Add custom tab menu
val bottomMenuView = navView.getChildAt(0) as BottomNavigationMenuView
val view = bottomMenuView.getChildAt(1)
val itemView = view as BottomNavigationItemView
val viewCustom = LayoutInflater.from(this).inflate(R.layout.button_custom, bottomMenuView, false)
itemView.addView(viewCustom)
}
}
My result:
This is just themed and the icon is set with: <item android:icon="#drawable/some_icon" ... />.
Requesting a library for such a simple task is a) questionable and b) also considered as off-topic.

The Toolbar shows only the Title from the Item in menu_main for toolbar

The Toolbar shows only the Title from the Item in menu_main for toolbar
My MainActivity Toolbar
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="#style/MyToolbar"
app:theme="#style/Theme.Mytheme"
android:elevation="4dp"
/>
menu_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/coins_value_item"
android:orderInCategory="4"
android:actionLayout="#layout/coin_bar"
android:title="OnlyThisTitleShownOnPhone"
app:showAsAction="always"/>
</menu>
coin_bar
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="50dp"
android:paddingVertical="5dp"
android:background="#drawable/bg_round_corner"
android:layout_gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:src="#drawable/coin_icon"/>
<TextView
android:id="#+id/coins_value_text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginLeft="25dp"
android:textSize="25sp"
android:textColor="#color/white"
android:fontFamily="#font/main_font"
android:text="1000" />
</FrameLayout>
MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Correctly shown in Android Studio:
What's wrong in that situation?
Incorrectly displayed only on the device, namely, only the title property of the item itself is displayed
I just changing this:
android:actionLayout="#layout/coin_bar"
to this:
app:actionLayout="#layout/coin_bar"

Customizing autocompletetextview

I need to implement a custom search functionality with autocomplete feature with custom adapter. I wrote my own custom adapter, Code is given below:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="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:background="#drawable/icon_bg"
android:orientation="vertical"
tools:context="com.emc.kulkaa.myfinancials.MainActivity">
<AutoCompleteTextView
android:id="#+id/edt_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView"
android:layout_alignStart="#+id/imageView"
android:layout_below="#+id/imageView"
android:layout_margin="16dp"
android:layout_marginTop="20dp"
android:background="#drawable/round_border_edittext"
android:drawableEnd="#drawable/clear"
android:drawableLeft="#drawable/white"
android:drawableStart="#drawable/white"
android:ems="10"
android:hint="#string/search_hint"
android:singleLine="true"
android:textColor="#color/colorWhite"
android:textColorHint="#color/colorWhite" />
</LinearLayout>
custom_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/cardview_color">
<TextView
android:id="#+id/autoCompleteItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="15sp"
style="#style/simple"/>
</LinearLayout>
styles.xml
<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>
<item name = "android:autoCompleteTextViewStyle">#style/simple</item>
</style>
<style name="simple" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:popupBackground">#drawable/simpleautocustom </item>
</style>
</resources>
simpleautocustom.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/colorWhite" />
<stroke
android:width="0dip"
android:color="#color/colorBlue" />
<corners android:radius="20dip" />
<padding
android:bottom="10dip"
android:left="25dip"
android:right="25dip"
android:top="10dip" />
</shape>
java code
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView mEdtSearch;
#Override
protected void onCreate(Bundle savedInstanceState) {
String arr[] = {"DSA LSA", "CHILD", "AICHILES", "CHILDREN", "FRANCE", "FRENCH"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.autoCompleteItem, arr);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdtSearch = (AutoCompleteTextView) findViewById(R.id.edt_search);
mEdtSearch.setFocusable(true);
mEdtSearch.setFocusableInTouchMode(true);
mEdtSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mEdtSearch.setAdapter(adapter);
}
});
}
}
Above code works fine, here is the screenshot of output:
However it doesn't still match expected output UI which is given below:
How can I change styles and custom shape so that I can achieve the output?
I have tried something for you, hope it will solve your problem and you can customize xmls i.e background of custom_item.xml TextView according to your need.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9A000000"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/edt_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView"
android:layout_alignStart="#+id/imageView"
android:layout_below="#+id/imageView"
android:layout_margin="16dp"
android:layout_marginTop="20dp"
android:background="#drawable/round_border_edittext"
android:drawableEnd="#drawable/clear"
android:drawableLeft="#drawable/white"
android:drawableStart="#drawable/white"
android:ems="10"
android:hint="#string/search_hint"
android:popupBackground="#android:color/transparent"
android:singleLine="true"
android:textColor="#color/colorWhite"
android:textColorHint="#color/colorWhite"/>
</LinearLayout>
custom_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
<TextView
android:id="#+id/autoCompleteItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/bg_rounded_border"
android:maxLines="1"
android:padding="15dp"
android:singleLine="true"
android:textColor="#color/white"
android:textSize="15sp"/>
</RelativeLayout>
bg_rounded_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/hint_color"/>
<corners android:radius="#dimen/padding_less"/>
</shape>
rest your MainActivity (java) code is same, there is no need to set a style to your list item text view.
Here is the screen shot of the output.
You can remove styles.
Only you should add android:padding="7dp" to parent LinearLayout and android:background="#drawable/simple_auto" to TextView in custom_item. It is working.
custom_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
android:padding="7dp"
android:background="#color/cardview_color">
<TextView
android:id="#+id/autoCompleteItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/simple_auto"
android:padding="10dp"
android:textSize="15sp"/>
</LinearLayout>

Android Studio: getSupportActionBar() returns null

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.

Categories

Resources