In my application I want to set a custom menu for the toolbar. The icon that I defined in the menu_settings.xml is an arrow, but when I start the application, it's a three-point menu instead of the arrow (as seen in the Pictures). After hours of research I still can't figure out why it loads this icon instead of the arrow (yes I checked the drawable ;) ). Thank you for solutions and hints!
SettingsActivity:
public class SettingsActivity extends PreferenceActivity {
private AppCompatDelegate mDelegate;
#Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
boolean hasConfiguredSettings = intent.getBooleanExtra(HAS_CONFIGURED_SETTINGS, false);
if (hasConfiguredSettings) {
toolbar.setTitle("Wähle deine Linien!");
} else {
// Back button in Toolbar
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.content, new SettingsFragment())
.commit();
}
private void setSupportActionBar(Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Intent intent = getIntent();
boolean hasConfiguredSettings = intent.getBooleanExtra("HAS_CONFIGURED_SETTINGS", false);
if (hasConfiguredSettings) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_settings, menu);
return true;
}
return false;
}
}
menu_settings.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=".SettingsActivity">
<item
android:id="#+id/action_checked"
android:orderInCategory="101"
android:title="#string/configuration_action_button"
app:showAsAction="always"
android:icon="#drawable/ic_action_arrow_right"/>
</menu>
toolbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/primary"
/>
The toolbar is included with:
<include
android:id="#+id/tool_bar"
layout="#layout/toolbar" />
You need to use AppCompatDelegate#getMenuInflater instead of Activity#getMenuInflater().
Replace the below line:
MenuInflater inflater = getMenuInflater();
with
MenuInflater inflater = getDelegate().getMenuInflater();
Related
Menu Item click not working I tried various things but it won't work.
Here are my code:
Home Activity:
public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private Button mBtRequest;
private Button mBtContinue;
private ProfileFragment mProfileFragment;
private SetPreferences Setpref;
private SlideUp slideUp;
private View dim;
private View sliderView;
Toast toast;
private TextView mTvDrawerName;
private TextView mTvDrawerMobile;
private TextView mtoolbarTitle;
private String mfullName;
private String mMobile;
private SharedPreferences mSharedPreferences;
private CompositeSubscription mSubscriptions;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Checking for first time launch - before calling setContentView()
Setpref = new SetPreferences(this);
if (!Setpref.IsLoggined()) {
gotoMain();
finish();
}
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
mSubscriptions = new CompositeSubscription();
initSharedPreferences();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mBtRequest = (Button) findViewById(R.id.btn_request);
mBtContinue = (Button) findViewById(R.id.btn_continue);
mBtRequest.setOnClickListener(view -> slideUp());
mBtContinue.setOnClickListener(view -> checkout());
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);
sliderView = findViewById(R.id.slideView);
sliderView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (toast != null){
toast.cancel();
}
toast = Toast.makeText(HomeActivity.this, "click", Toast.LENGTH_SHORT);
toast.show();
}
});
dim = findViewById(R.id.dim);
slideUp = new SlideUpBuilder(sliderView)
.withListeners(new SlideUp.Listener.Events() {
#Override
public void onSlide(float percent) {
dim.setAlpha(1 - (percent / 100));
}
#Override
public void onVisibilityChanged(int visibility) {
if (visibility == View.GONE){
// fab.show();
}
}
})
.withStartGravity(Gravity.BOTTOM)
.withLoggingEnabled(true)
.withGesturesEnabled(true)
.withStartState(SlideUp.State.HIDDEN)
.build();
Typeface myraid_bold = Typeface.createFromAsset(getAssets(),"fonts/MyriadPro-Bold_0.otf");
Typeface myraid_semibold = Typeface.createFromAsset(getAssets(),"fonts/MyriadPro-Semibold_0.otf");
View header = navigationView.getHeaderView(0);
mTvDrawerName = (TextView) header.findViewById(R.id.nav_user_name);
mTvDrawerMobile = (TextView) header.findViewById(R.id.nav_mobile_number);
mtoolbarTitle = (TextView) findViewById(R.id.toolbar_title);
mTvDrawerName.setText(mfullName);
mTvDrawerMobile.setText(mMobile);
mTvDrawerName.setTypeface(myraid_bold);
mTvDrawerMobile.setTypeface(myraid_bold);
mtoolbarTitle.setTypeface(myraid_semibold);
mBtRequest.setTypeface(myraid_bold);
mBtContinue.setTypeface(myraid_bold);
}
private void initSharedPreferences() {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mfullName = mSharedPreferences.getString(Constants.FULLNAME,"");
mMobile = mSharedPreferences.getString(Constants.MOBILE,"");
}
private void gotoProfile(){
if (mProfileFragment == null) {
mProfileFragment = new ProfileFragment();
}
getFragmentManager().beginTransaction().replace(R.id.fragmentFrame,mProfileFragment,ProfileFragment.TAG).commit();
}
private void gotoMain(){
Setpref.setIsLoggined(false);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
private void slideUp(){
slideUp.show();
}
private void checkout(){
}
#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;
}
#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);
}
private void logout() {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(Constants.MOBILE,"");
editor.putString(Constants.TOKEN,"");
editor.putString(Constants.FULLNAME,"");
editor.apply();
gotoMain();
}
private void showSnackBarMessage(String message) {
Snackbar.make(findViewById(R.id.drawer_layout),message,Snackbar.LENGTH_SHORT).show();
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_my_addresses) {
// Handle the camera action
} else if (id == R.id.nav_my_orders) {
} else if (id == R.id.nav_refer) {
} else if (id == R.id.nav_logout) {
logout();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
activity_home_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_my_addresses"
android:icon="#drawable/ic_location_on_white_24px"
android:title="My Addresses" />
<item
android:id="#+id/nav_my_orders"
android:icon="#drawable/ic_history_white_24px"
android:title="My Orders" />
<item
android:id="#+id/nav_refer"
android:icon="#drawable/ic_record_voice_over_white_24px"
android:title="Refer & Earn" />
</group>
<group
android:id="#+id/menu_bottom"
android:checkableBehavior="none">
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_white_24px"
android:title="Logout" />
</group>
</menu>
nav_header_main.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:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Aashish Kaushik"
android:id="#+id/nav_user_name"
android:textColor="#android:color/white"/>
<TextView
android:id="#+id/nav_mobile_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9910454530"
android:textColor="#android:color/white"/>
</LinearLayout>
activity_home.xml
<?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">
<FrameLayout
android:alpha="0"
android:id="#+id/dim"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dimBg" />
<include
layout="#layout/app_bar_main"
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_main"
app:menu="#menu/activity_home_drawer" />
<!-- The Fragment -->
<FrameLayout
android:id="#+id/fragmentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>
When I try to view activity_home.xml as a design it will not showing left drawer but when I install the app on device it will show the left drawer on click but when I click on any time it will not showing check able box and not working.
Update These lines of code works for me.. after update clean and rebuild Project.
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.bringToFront();
Here is a simple solution to address this issue. You can just link your HomeActivity methods to the menu items (whichever you want to). Now just create your methods and go to the menu layout. In the menu layout just add the android:onClick inside the item tag and put the particular method name inside it. And you are done. Let me know if this was useful.
I have toolbar and framelayout(for fragments) in my main activity layout. I have to show the toolbar in every fragments. But the issue is each fragment has different background image that is dynamically changing. Toolbar menu icon is used for navigation drawer open and close operation. How I will handle these issue?
Before posting the code block let's first understand what are we are going to do.
1.Create a HomeActivity that holds all the fragments.
Here is the activity_home.xml
<?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_home"
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_home"
app:menu="#menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
Here is the app_bar_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
the other xmls of navigationview -- activity_home_drawer.xml contains navigation drawer contents and nav_header_home.xml is the navigation drawer header layout.
2.Let's come to java part of the HomeActivity.java
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
private HomeBean bean;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
initListener();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//this menu contains all the common menu item actions
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
//common options bar item click here i have notifications icon common for all the fragments
#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.
switch (item.getItemId()){
case R.id.action_notifications:
AppUtils.launchActivity(HomeActivity.this, NotificationActivity.class,intentData);
return false;
default:
return super.onOptionsItemSelected(item);
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.nav_dashboard:
//you can cache this fragment in case of repeated creations
selectedFragment = new DashBoardFragment();
}
break;
}
if(selectedFragment != null){
displayFragment(selectedFragment);
DrawerLayout drawer = ButterKnife.findById(this,R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
return true;
}
private void displayFragment(Fragment fragment) {
if(fragment !=null){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
}
#Override
public void initToolBar() {}
public void setToolbar(Toolbar toolbar){
setSupportActionBar(toolbar);
setActionBarToggle(toolbar);
}
private void setActionBarToggle(Toolbar toolbar) {
DrawerLayout drawer = ButterKnife.findById(this, 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();
}
#Override
public void initListener() {
NavigationView navigationView = ButterKnife.findById(this,R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//as the first item is show by default
navigationView.getMenu().getItem(0).setChecked(true);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
Now as an example let see how the fragments are written.
public class SettingsFragment extends Fragment {
#BindView(R.id.toolbar)
Toolbar toolbar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings,container,false);
ButterKnife.bind(this,view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((HomeActivity)getActivity()).setToolbar(toolbar);
}
}
Corresponding fragment xml fragment_settings.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:fitsSystemWindows="true"
tools:context="Fragment Name">
<android.support.design.widget.AppBarLayout
android:id="#+id/notification_appbar"
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"
app:title="#string/settings"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_settings" />
</android.support.design.widget.CoordinatorLayout>
the corresponding xml content_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/min_widget_padding"
android:id="#+id/nsv_settings_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
// your content goes here
</<android.support.v4.widget.NestedScrollView>
I hope this gives clear picture on how to do in case if you have any other doubts please comment.
Use this code. I helped
public class MyFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);// To enable actionbar in fragments
super.onCreate(savedInstanceState);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Here it is necessary to connect the layout menu
inflater.inflate(R.menu.fragment2, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
My app has two activities with a DrawerLayout and a NavigationView (android.support.design.widget.NavigationView).
I use an app theme with parent Theme.AppCompat.Light.DarkActionBar, and for my Lollipop emulator, there are no problems.
But for my Jellybean device and emulator, I have a very strange behaviour:
The background of the NavigationView items is sometimes rendered as light grey (which would be ok for me), sometimes as a light blue shade (I think it's the Theme Holo default).
For my "real" app, the grey tint would always appear on the first drawer and the blue tint always on the other one. At first I thought the difference may be due to some detail like the first uses an ActionBarDrawerToggle and the other one doesn't, so maybe I had to somehow set the android:listSelector for the second one only.
So I created a MCVE-like sample and noticed that both drawers show both tints now and then. Is this a bug or am I missing something?
What can I do to get rid of Theme Holo?
Screenshots:
By the way, I have the same random behaviour for the ActionButton background:
My code (~ 95% of it for those who like to run their own tests):
activity_main.xml just a 'Blank Activity' with a big "Hello World" at the center:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<TextView
android:text="#string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
activity_main_decor.xml
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
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">
<FrameLayout
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"/>
<android.support.design.widget.NavigationView
android:id="#+id/main_drawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/menu_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
activity_other.xml
<merge>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Hello again!"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</merge>
activity_other_decor.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="#+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/activity_other"/>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/other_drawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="end"
app:menu="#menu/menu_other_drawer" />
</android.support.v4.widget.DrawerLayout>
MainActivity - the navigation drawer has to appear from the left and above the ActionBar( BTW thanks to #Peter Cai for some very useful lines )
public class MainActivity extends AppCompatActivity
{
private static final String TAG = "StyleForDrawers main";
private static final String DRAWER_OPEN = "main_drawer_open";
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle drawerToggle;
private boolean isDrawerOpen;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initNavDrawer();
}
private void initNavDrawer()
{
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this);
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
drawerLayout = (DrawerLayout) inflater.inflate(R.layout.activity_main_decor, null);
ViewGroup decor = (ViewGroup)activity.getWindow().getDecorView();
View child = decor.getChildAt(0);
decor.removeView(child);
FrameLayout container = (FrameLayout)drawerLayout.findViewById(R.id.container);
container.addView(child);
decor.addView(drawerLayout);
navigationView = (NavigationView) findViewById(R.id.main_drawer);
navigationView.setItemTextColor(ContextCompat.getColorStateList(this, R.color.nav_item));
navigationView.setItemTextAppearance(R.style.MyNavdrawerTextAppearance);
View headerView = navigationView.inflateHeaderView(R.layout.navdrawer_list_header);
// header View should appear below status bar, so set padding:
(headerView.findViewById(R.id.nav_header_root)).setPadding(0, getStatusbarHeight(), 0, (int) getResources().getDimension(R.dimen.activity_vertical_margin));
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(MenuItem menuItem)
{
Intent intent = null;
int id = menuItem.getItemId();
if (id == R.id.action_other_activity)
{
intent = new Intent(MainActivity.this, OtherActivity.class);
}
// -- skipped some code for Help etc. --
drawerLayout.closeDrawer(navigationView);
isDrawerOpen = false;
if (intent != null)
{
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
return true;
}
});
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.sDrawerOpen, R.string.sDrawerClose)
{
#Override
public void onDrawerClosed(View drawerView)
{
super.onDrawerClosed(drawerView);
sPrefs.edit().putBoolean(DRAWER_OPEN, false).apply();
}
#Override
public void onDrawerOpened(View drawerView)
{
isDrawerOpen = true;
super.onDrawerOpened(drawerView);
sPrefs.edit().putBoolean(DRAWER_OPEN, true).apply();
}
};
drawerLayout.setDrawerListener(drawerToggle);
if (getSupportActionBar() != null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.nav_drawer);
}
if (sPrefs.getBoolean(DRAWER_OPEN, true)) // open at first launch
{
drawerLayout.openDrawer(navigationView);
}
else
{
drawerLayout.closeDrawer(navigationView);
}
}
#Override
protected void onPause()
{
super.onPause();
SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sPrefs.edit().putBoolean(DRAWER_OPEN, isDrawerOpen).apply();
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
return drawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
public static int getStatusbarHeight()
{
int retValue = 0;
int resID = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resID > 0) {
retValue = getResources().getDimensionPixelSize(resID);
}
return retValue;
}
}
OtherActivity - the drawer is like a context menu and appears from the right without overlapping the ActionBar:
public class OtherActivity extends AppCompatActivity
{
private static final String TAG = "StyleForDrawers other";
private static final String DRAWER_OPEN = "other_drawer_open";
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;
private boolean mIsDrawerOpen;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other_decor);
initMenuDrawer();
}
private void initMenuDrawer()
{
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationView = (NavigationView) findViewById(R.id.other_drawer);
mNavigationView.setItemTextColor(ContextCompat.getColorStateList(this, R.color.nav_item));
mNavigationView.setItemTextAppearance(R.style.MyNavdrawerTextAppearance);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(MenuItem menuItem)
{
int id = menuItem.getItemId();
if (id == R.id.action_back_to_main)
{
finish();
}
// -- skipped some code for doing something (else) --
mDrawerLayout.closeDrawer(mNavigationView);
mIsDrawerOpen = false;
return true;
}
});
mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener()
{
#Override
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
mIsDrawerOpen = true;
sPrefs.edit().putBoolean(DRAWER_OPEN, true).apply();
}
#Override
public void onDrawerClosed(View drawerView)
{
super.onDrawerClosed(drawerView);
mIsDrawerOpen = false;
sPrefs.edit().putBoolean(DRAWER_OPEN, false).apply();
}
});
if (sPrefs.getBoolean(DRAWER_OPEN, false)) // don't open at first launch
{
mDrawerLayout.openDrawer(mNavigationView);
}
else
{
mDrawerLayout.closeDrawer(mNavigationView);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_other, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_opendrawer)
{
if (mIsDrawerOpen)
{
mDrawerLayout.closeDrawer(mNavigationView);
}
else
{
mDrawerLayout.openDrawer(mNavigationView);
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_main.xml unnecessary
menu_other.xml contains an action button to open/close the drawer:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_opendrawer"
android:title="open/close drawer"
android:icon="#drawable/nav_drawer"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
styles.xml
<resources>
<color name="accent_orange">#ff6d00</color>
<color name="my_background">#eeeeee</color>
<color name="my_text">#311b92</color>
<color name="my_sec_text">#B1311b92</color>
<color name="my_actionbar">#1a237e</color>
<color name="my_statusbar">#1a237e</color>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/my_actionbar</item>
<item name="colorPrimaryDark">#color/my_statusbar</item>
<item name="colorAccent">#ffc400</item>
<item name="android:windowBackground">#color/my_background</item>
<item name="android:textColorPrimary">#color/my_text</item>
<item name="android:textColor">#color/my_text</item>
<item name="android:textColorSecondary">#color/my_sec_text</item>
</style>
<style name="MyNavdrawerTextAppearance" parent="TextAppearance.AppCompat.Small">
<item name="android:textStyle">bold</item>
</style>
</resources>
UPDATE (May 2016)
Meanwhile I've also tried setting my own background for the NavigationView items. The background is changed but on touching I still get a ripple for Lollipop and higher (which is ok) and unfortunately still the alternating effect on Jellybean, probably from some kind of list selector which I don't know how to manipulate.
UPDATE 2 (September 2016)
I installed a new version of Android Studio, am using the newest versions of build tools and all the libraries (24.0.2 respectively 24.2.0) and even ditched the ActionBar in favour of Toolbar. The last means I won't have to use the hack in Main to get the nav drawer to cover the ActionBar, so I'm not using a LayoutInflater in my code any more.
But still no luck :(
I'm pretty sure that this is a bug: the same code behaving differently from one app run to the next and the inconsistency between activities Main and Other during one and the same run.
I've a single activity(MainActivity) which extends actionbaractivity and implements NavigationDrawer. I've setup the drawertoggle in the activity itself.
I'm creating new fragments from a fragment that was created by another fragment which is in turn created by the MainActivity. ( MainActivity -> HomeFragment ->AnotherFragment).
MainActivity.java
public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener, TitleSetter, NavigationDrawerEnabler{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), myToolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
displayView(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.OncreateOptionsMenu(menu);
}
#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.
/* This function is never called even if the Up caret button is pressed!! */
if (mDrawerToggle.isDrawerIndicatorEnabled() &&
mDrawerToggle.onOptionsItemSelected(item))
return true;
switch (item.getItemId()) {
case android.R.id.home:
/*This case doesn't occur*/
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onDrawerItemSelected(View view, int position) {
mDrawerLayout.closeDrawer(containerView);
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new SomeFragment();
title = getString(R.string.title_something);
break;
case 2:
//fragment = new SomeotherFragment();
//title = getString(R.string.title_somethings);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
getSupportActionBar().setTitle(title);
}
}
#Override
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
containerView = findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getSupportActionBar().invalidateOptionsMenu();
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
toolbar.setAlpha(1 - slideOffset / 2);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
enableNavigationDrawer(true);
}
#Override
public void enableNavigationDrawer(boolean isEnabled) {
if ( isEnabled ) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.syncState();
}
else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle.syncState();
}
}
}
HomeFragment.java
public class HomeFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
// Inflate the layout for this fragment
fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
fab.setOnClickListener(this);
((TitleSetter) getActivity()).setTitle("Home");
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fab:
((NavigationDrawerEnabler) getActivity()).enableNavigationDrawer(false);
Fragment newFragment = new NewWord();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, newFragment);
fragmentTransaction.addToBackStack(null);
// Commit the transaction
fragmentTransaction.commit();
break;
}
}
}
AnotherFragment.java
public class NewWord extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Get item selected and deal with it
/*Not even this*/
Log.d("KEY: ", String.valueOf(item.getItemId()));
switch (item.getItemId()) {
case android.R.id.home:
/* Doesn't execute*/
Log.d("Fragment", "I'm here");
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.new_word, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((TitleSetter) activity).setTitle("Text");
}
Also the drawer layout
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.calgen.wordbook.activity.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_nav_drawer"
tools:layout="#layout/fragment_nav_drawer" />
And 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_search"
android:title="#string/action_search"
android:orderInCategory="100"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
I actually referred this tutorial to setup navigation drawer fragment : Android Hive.
I also referred similar questions answered here on stackoverflow :
1.Switching between Android Navigation Drawer image and Up caret when using fragments
2.My problem is similiar to this : Android Navigation Drawer Show Up Indicator for Lower Level Fragments.
- I couldn't comment here as i do not have 50 reps. I implemented what was exactly told in the comments.
But still clicking on the up caret doesn't run the function onOptionItemSelected, even though i've homebutton enabled.!
I'm now adding my custom styles.xml, may be some error in this?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base"></style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:activatedBackgroundIndicator">#drawable/nav_background</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
Also the custom toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
SOLVED FINALLY
I figured out the problem, it is due to the custom toolbar that I used. When a custom toolbar is set up, we also have to set a listener on the up caret using the method called drawertoggler.setNaviagtionOnClickListener
In the onCreate() method of the activity we need to add the following code:
drawertoggler.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getSupportFragmentManager().popBackStackImmediate();
}
});
Other cases like OnBackPressed can be handled by setting a OnStackChangeListener in the activity and make necessary changes. It is clearly explained here :#riwnodennyk.
I would like to add a badge over ActionBar MenuItem
But the digit icon didn't shows.
Here's what I have done so far
public class Main extends SherlockFragmentActivity
{
private Fragment menuFrag=null;
private MenuItem menuMsg=null;
private BadgeView badge=null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
//Do my stuff...
initUI();
}
private void initUI()
{
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
menuFrag=fm.findFragmentByTag("f1");
if(menuFrag==null)
{
menuFrag=new MenuFragment();
ft.add(menuFrag, "f1");
}
ft.commit();
// badge=new BadgeView(Main.this, (View)menuMsg); //Not working
badge=new BadgeView(Main.this, menuMsg.getActionView()); //Not working as well
badge.setBackgroundResource(R.drawable.badge_ifaux);
badge.setTextSize(10);
badge.setBadgeMargin(2);
badge.setText("1");
badge.show();
}
private class MenuFragment extends SherlockFragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
menu.add("Cloud").setIcon(R.drawable.icon_cloud).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("List").setIcon(R.drawable.icon_list).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menuMsg=menu.add("Msg");
menuMsg.setIcon(R.drawable.icon_msg).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
Toast.makeText(Main.this, "Got click: " + item.toString(), Toast.LENGTH_SHORT).show();
return true;
}
}
}
Where do I did wrong ?
RRTW,
The library you are using doesn't support badging Actionbar menu items natively.
https://github.com/jgilfelt/android-viewbadger/commit/e08c3a78cb92c0c8587790b15e73434f972912cf
However, it doesn't mean you can't get it to work.
The setup is going to be as follows (this assumes you already have the viewbager library setup in your project)
(1) onCreateOptionsMenu --> (2) Add a R.menu.your_place_holder_item --> (3) setActionView with custom xml layout --> (4) findViewById of the MenuItem object to get your button/view to set the badge.
1) setup your onCreateOptionsMenu and create a the R.menu.actionbar_menu_messages
R.menu.actionbar_menu_messages:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:showAsAction="ifRoom" android:icon="#drawable/action_bar_pk_content_email"
android:id="#+id/menuMessages" android:title="More"></item>
</menu>
onCreateOptionsMenu:
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater(); //If you are using the support library otherwise use: getMenuInflater();
inflater.inflate(R.menu.actionbar_menu_messages, menu);
this.setupMessagesBadge(menu.findItem(R.id.menuMessages)); //This is part of step 2
return true;
}
2) Defined the common_messages_indicator
R.layout.common_messages_indicator:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="64dp"
android:layout_height="fill_parent"
android:paddingTop="10dp"
android:gravity="center">
<ImageView
android:id="#+id/imgMessagesIcon"
android:layout_width="32dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/messages_button"
android:background="#android:color/transparent"
android:focusable="false"
/>
</FrameLayout>
perform setActionView to add your custom xml layout to the ActionView
private void setupMessagesBadge(final MenuItem msgItem) {
msgItem.setActionView(R.layout.common_messages_indicator);
if(msgItem.getActionView().findViewById(R.id.imgMessagesIcon) != null)
{
ImageView imgMessagesIcon = ((ImageView)msgItem.getActionView().findViewById(R.id.imgMessagesIcon));
imgMessagesIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your click on the action bar item will be captured here
}
});
int badgeCnt = 20;// Add your count here
if(messageCenterBadge == null && badgeCnt > 0)
{
//imgMessagesIcon is the imageview in your custom view, apply the badge to this view.
messageCenterBadge = new BadgeView(this, imgMessagesIcon);
messageCenterBadge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
messageCenterBadge.setBadgeMargin(0);
messageCenterBadge.setTextSize(12);
messageCenterBadge.setText(String.valueOf(badgeCnt));
messageCenterBadge.show();
}
else if(messageCenterBadge != null && badgeCnt > 0 )
{
messageCenterBadge.setText(String.valueOf(badgeCnt));
messageCenterBadge.show();
}
else if(messageCenterBadge != null && badgeCnt == 0) {
messageCenterBadge.hide();
}
}
}