Android navigation drawer default selected on start - java

I have created a new android studio project using the navigation drawer activity. Currently, the main activity starts when the app is loaded (which is fine), but I want to associate this main activity with a particular menu item in the drawer such that 1) when this menu item is selected the main activity will load, and 2) I want that menu item to be selected/highlighted by default when the app first starts
I think I would handle issue 1 with intents in the onNavigationItemSelected method, but I'm not sure how to have a menu item selected by default.
I've searched around and the only solutions I've found refer to fragments, but I'm not using fragments in this project (and those solutions didn't work). For example I have tried selectItem(0); in onCreate(), and navigationView.getMenu().getItem(0).setChecked(true); but neither seem to work.
Here is my base code:
MainActivity.java:
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, "FAB Pressed", 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);
}
activity_main_drawer.xml:
<item android:title="Menu">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_new"
android:icon="#drawable/ic_menu_share"
android:title="#string/nav_new" />
<item
android:id="#+id/nav_start"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/nav_start" />
<item
android:id="#+id/nav_delete"
android:icon="#drawable/ic_menu_manage"
android:title="#string/nav_delete" />
</group>
</menu>
</item>
Not much has changed from the starting project apart from the menu items. I want to have the New menu item selected by default as I want to associate that item with the MainActivity

You only need to add code:
navigationView.setCheckedItem(R.id.nav_new);

I thing the best approach to this is to include android:checked="true"
<item android:title="Menu">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_new"
android:icon="#drawable/ic_menu_share"
android:checked="true"
android:title="#string/nav_new" />
<item
android:id="#+id/nav_start"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/nav_start" />
<item
android:id="#+id/nav_delete"
android:icon="#drawable/ic_menu_manage"
android:title="#string/nav_delete" />
</group>
</menu>
</item>
Hope this helps you more

Related

How to Control Menu Position in Toolbar Android

I want to change the gravity of my inflated menu item in android xml code but i could not find any attribute to solve the problem. i want an item in the left side and another item in right side of corner in Toolbar.
Do you have any idea guys?
Here's my present state:
Here's my menu 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">
<item
android:id="#+id/dismis"
android:icon="#drawable/close"
android:title="Done"
app:showAsAction="always"></item>
<item
android:id="#+id/saveNote"
android:icon="#drawable/save"
android:title="Done"
app:showAsAction="always"></item>
</menu>
Use Toolbar navigation item(LEFT) for dismiss item and option menu(RIGHT) for saveNote item.
Dismiss:
You can use Toolbar navigation item as dismiss action. Set
close icon as Toolbar navigation icon by using
Toolbar.setNavigationIcon(). To handle the click event, add
NavigationOnClickListener to Toolbar.
SaveNote:
Inflate menu XML to Toolbar using Toolbar.inflateMenu(). To
handle saveNote item click event, add OnMenuItemClickListener to
Toolbar.
Follow below steps:
1. Remove dismiss item from menu XML.
// 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">
<item
android:id="#+id/saveNote"
android:icon="#drawable/save"
android:title="Done"
app:showAsAction="always">
</item>
</menu>
2. In your activity, do below changes for dismiss and saveNote actions.
public class MainActivity extends AppCompatActivity {
// ToolBar
Toolbar mToolBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...........
.....................
// ToolBar
mToolBar = (Toolbar) findViewById(R.id.toolbar);
// Required to use Toolbar as ActionBar
setSupportActionBar(mToolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("StackOverflow");
// Dismiss Action
mToolBar.setNavigationIcon(R.drawable.close);
mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
Toast.makeText(getApplicationContext(), "Dismiss", Toast.LENGTH_SHORT).show();
}
});
// SaveNote Action
mToolBar.inflateMenu(R.menu.menu_main);
mToolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.saveNote)
{
// Do something
Toast.makeText(getApplicationContext(), "Save", Toast.LENGTH_SHORT).show();
}
return true;
}
});
.............
.......................
}
}
OUTPUT:
Hope this will help~
well, you have to make a custom layout and then attach it to your appbar!
you can check here
link1
link2
to make a custom layout for appbar in your activity

Action Bar isn't displaying in API16

I'm trying to use a navigation drawer in my Main activity.
when i run it in API 23 Emulator , everything looks fine.
But when I run it in API 16 Emulator, my action bar goes away!
Here is my manifest file :
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here is my styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/line1</item>
<item name="colorPrimaryDark">#color/line1_dark</item>
<item name="colorAccent">#color/isbc</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Here is my v21/styles.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/line1</item>
<item name="colorPrimaryDark">#color/line1_dark</item>
<item name="colorAccent">#color/isbc</item>
</style>
And this is my Mainactivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
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) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
changeTypeface(navigationView);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#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;
}
private void applyFontToItem(MenuItem item, Typeface font) {
SpannableString mNewTitle = new SpannableString(item.getTitle());
mNewTitle.setSpan(new CustomTypefaceSpan("", font, 16), 0 ,
mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
item.setTitle(mNewTitle);
}
private void changeTypeface(NavigationView navigationView) {
FontTypeface fontTypeface = new FontTypeface(this);
Typeface typeface = fontTypeface.getTypefaceAndroid();
MenuItem item;
item = navigationView.getMenu().findItem(R.id.nav_camera);
item.setTitle("لیست ایستگاه ها");
item.setIcon(R.drawable.ic_action_stations);
applyFontToItem(item, typeface);
item = navigationView.getMenu().findItem(R.id.nav_gallery);
item.setTitle("نقشه");
item.setIcon(R.drawable.ic_action_map);
applyFontToItem(item, typeface);
item = navigationView.getMenu().findItem(R.id.nav_slideshow);
item.setTitle("درباره ما");
item.setIcon(R.drawable.ic_action_us);
applyFontToItem(item, typeface);
}
#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 true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit(); }
else if (id == R.id.nav_gallery) {
MapFragment fragment = new MapFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_slideshow) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Here is my app_bar_main :
<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/fragment_main" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
Here is my activity_main.xml
<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_main"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
When I run this code in emulator API 21, everything looks fine.
But when I run it in API 16, i get this error:
"This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead."
I guess it's telling me not to use actionbar, but how can i actually display my navigation drawer if i set windowActionBar to false?
i've tried this topic but it didn't work

Implementing multiple selection accross multiple groups in Navigation Drawer

I want to have this kind of selection in my Navigation Drawer. This picture shows the selection which are made initially (by default not by user).
I have implemented this by the following code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Category">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/coaching"
android:icon="#drawable/coaching"
android:title="Coaching"
android:checked="true"/>
<item
android:id="#+id/training"
android:icon="#drawable/training"
android:title="Training"/>
</group>
</menu>
</item>
<group android:checkableBehavior="single">
<item
android:id="#+id/new_registrations"
android:icon="#drawable/new_registrations"
android:title="New Registrations"
android:checked="true"/>
<item
android:id="#+id/ready_certificates"
android:icon="#drawable/ready_certificates"
android:title="Certificates Ready To Collect"/>
<item
android:id="#+id/allotted_certificates"
android:icon="#drawable/allotted_certificates"
android:title="Certificates Allotted So Far"/>
</group>
But the thing is when I select any item manually then all the previously selected items from both groups get deselected. So I want to validate one selection from group one and one selection from group two. Looking for missing attributes.
Remove android:checkableBehavior="single" from both groups.
Remove android:checked="true" from both "coaching" and "new_registrations" items.
Set android:checkable="true" for each item individually.
Set unique ids for both groups ("#+id/first_group" and "#+id/second_group").
Your activity_main_drawer.xml should look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Category">
<menu>
<group android:id="#+id/first_group" >
<item
android:id="#+id/coaching"
android:icon="#drawable/coaching"
android:title="Coaching"
android:checkable="true" />
<item
android:id="#+id/training"
android:icon="#drawable/training"
android:title="Training"
android:checkable="true" />
</group>
</menu>
</item>
<group android:id="#+id/second_group" >
<item
android:id="#+id/new_registrations"
android:icon="#drawable/new_registrations"
android:title="New Registrations"
android:checkable="true" />
<item
android:id="#+id/ready_certificates"
android:icon="#drawable/ready_certificates"
android:title="Certificates Ready To Collect"
android:checkable="true" />
<item
android:id="#+id/allotted_certificates"
android:icon="#drawable/allotted_certificates"
android:title="Certificates Allotted So Far"
android:checkable="true" />
</group>
</menu>
Declare two MenuItem objects in the MainActivity.java and make them checked.
Add the following logic to the onNavigationItemSelected.
Your MainActivity.java should look like this:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
MenuItem prevItemOfFirstGroup;
MenuItem prevItemOfSecondGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ... some code
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
prevItemOfFirstGroup=navigationView.getMenu().findItem(R.id.coaching);
prevItemOfFirstGroup.setChecked(true);
prevItemOfSecondGroup=navigationView.getMenu().findItem(R.id.new_registrations);
prevItemOfSecondGroup.setChecked(true);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int groupId = item.getGroupId();
if (groupId == R.id.first_group) {
if (prevItemOfFirstGroup != null) {
prevItemOfFirstGroup.setChecked(false);
}
prevItemOfFirstGroup = item;
} else if (groupId == R.id.second_group) {
if (prevItemOfSecondGroup != null) {
prevItemOfSecondGroup.setChecked(false);
}
prevItemOfSecondGroup = item;
}
item.setChecked(true);
// Handle navigation view item clicks here.
int id = item.getItemId();
// ... some code
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return false; // IMPORTANT! NOT TRUE!
}
}

java.lang.NullPointerException: Attempt to invoke virtual method 'ActionBar.setNavigationMode(int)' on a null object reference

I am getting this error at runtime.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setNavigationMode(int)' on a null object reference
Main Activity.java
public class MainActivity <T extends Fragment> extends ActionBarActivity implements ListView.OnItemClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
ActionBar.Tab tab;
tab = actionBar.newTab()
.setText("Categories")
.setTabListener(new TabListener<CategoriesFragment>(
this, "Categories", CategoriesFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Acceuil")
.setTabListener(new TabListener<AcceuilFragment>(
this, "Acceuil", AcceuilFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Topics")
.setTabListener(new TabListener<TopicsFragment>(
this, "Topics", TopicsFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText("Tendances")
.setTabListener(new TabListener<TendancesFragment>(
this, "Tendances", TendancesFragment.class));
actionBar.addTab(tab);
setRef();
//Set the custom toolbar
if (toolbar != null)
{
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
}
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.open,
R.string.close
)
{
public void onDrawerClosed(View view)
{
Log.d("MainActivity", "OnDrawerClosed");
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
Log.d("MainActivity", "OnDrawerOpened");
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
// specify an adapter (see also next example)
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
private void setRef()
{
// create reference to the xml views
mRecyclerView = (android.support.v7.widget.RecyclerView) findViewById(R.id.my_recycler_view);
fab = (Button) findViewById(R.id.addButton);
drawerLayout = (android.support.v4.widget.DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setStatusBarBackground(R.color.primarydark);
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
leftDrawerList = (ListView) findViewById(R.id.list_drawer);
View list_header = getLayoutInflater().inflate(R.layout.drawerlist_header, null);
leftDrawerList.addHeaderView(list_header);
navigationDrawerAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, leftSliderData);
leftDrawerList.setAdapter(navigationDrawerAdapter);
leftDrawerList.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(getApplicationContext(), "Clicked on " + leftSliderData[position - 1], Toast.LENGTH_LONG).show();
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- Setting this one to true makes the status bar
to have a non-transparent shitty background -->
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<include layout="#layout/actionbarlayout"></include>
<include layout="#layout/content_view"></include>
<Button
android:id="#+id/addButton"
android:text="+"
android:textSize="25sp"
android:textColor="#android:color/white"
android:textAlignment="center"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:background="#drawable/circlebtn"
android:layout_width="56dp"
android:layout_height="56dp"
android:stateListAnimator="#animator/anim"
android:elevation="4dp"
style="?android:buttonStyleSmall"
android:layout_gravity="right|bottom" >
</Button>
</FrameLayout>
<com.androprogrammer.test.materialapp1.ScrimInsetsFrameLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:elevation="8dp"
android:layout_gravity="start"
app:insetForeground="#4000">
<ListView
android:id="#+id/list_drawer"
android:layout_width="#dimen/drawer_width"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:background="#android:color/white"
android:fitsSystemWindows="true">
</ListView>
</com.androprogrammer.test.materialapp1.ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primarydark</item>
<item name="colorAccent">#color/primary</item>
<item name="colorControlHighlight">#color/primary</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="windowActionModeOverlay">true</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
</resources>
v21/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androprogrammer.test.materialapp1" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Help Please :)
Try to change the parent style
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
and ActionBarActivity is deprecated, you should use AppCompatActivity
In your style.xml you have to have the actionbar enabled. If the actionbar is disabled (set to false) then it will not be added in to the activity at runtime (ie getActionBar() returns null).

Android: ActionBar (Android support library)

i have a problem with ActionBarCompat (from support library).
I have no idea how I can add a few buttons on ActionBar where I drew black circle (on screenshot) together with menu in the left of the screen.
Please, I need help!
Code of menu in ActionBar in the left of the screen.
private DrawerLayout mDrawerLayout;
private ListView mDrawer;
private ActionBarHelper mActionBar;
private ActionBarDrawerToggle mDrawerToggle;
...
linearLayout = (LinearLayout)findViewById(R.id.fragment_container);
linearLayout.setId(LAYOUT_ID);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerListener(new DDrawerListener());
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
...
fragments = new Fragment[NUMBER_OF_TABS];
mDrawer.setAdapter(new CustomAdapter(this));
mDrawer.setOnItemClickListener(new DrawerItemClickListener());
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));
actionBar.setDisplayHomeAsUpEnabled(true);
mActionBar = createActionBarHelper();
mActionBar.init();
mDrawer.setBackgroundColor(Color.parseColor("#e5c391"));
mDrawer.setCacheColorHint(Color.parseColor("#e5c391"));
initArrays(this);
mDrawerToggle = new ActionBarDrawerToggle
(this, mDrawerLayout,R.drawable.ic_drawer,R.string.app_drawer_open, R.string.app_drawer_close);
if (savedInstanceState == null)
{
addFragment(0);
}
Welcome other ways too =)
Thank you!
Happy New Year!
From https://developer.android.com/training/basics/actionbar/adding-buttons.html:
Specify the Actions in XML
All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory.
Add an element for each item you want to include in the action bar. For example:
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
Add the Actions to the ActionBar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Respond to Action Buttons
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Please refer to this note in the Android Developer portal.

Categories

Resources