im trying to add a (+) icon on right of navigation bar in android, and then launch a new intent when clicked, but i can't seem to make it work, it's showing me the left icon to open/close the drawer, but the right icon is nowhere to be found.
Can anyone give me a hand on why its not working? im loosing my mind here...
The navigation bar looks like this, there's enough room for the right side
ActivityHome.java
package com.roneskinder.x111.activity.fragment;
import java.util.ArrayList;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.roneskinder.x111.PagerSlidingTabStrip;
import com.roneskinder.x111.R;
import com.roneskinder.x111.adapter.MenuAdapter;
import com.roneskinder.x111.config.AppConfig;
import com.roneskinder.x111.manager.ResourcesContentManager;
import com.roneskinder.x111.model.NavDrawerItem;
import com.roneskinder.x111.util.AppUtils;
public class ActivityHome extends ActionBarActivity {
private Context mContext;
DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] navMenuTitles;
private FragmentManager mFragmentManager;
private Fragment mFragment;
private ActionBarDrawerToggle mDrawerToggle;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> mNavDrawerItems;
private MenuAdapter menuAdapter;
private Dialog mCustomDialog;
private RelativeLayout mAdsView;
protected static String TAG = ActivityHome.class.toString();
protected static EditText nameList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = ActivityHome.this;
showNavigationSlider();
if (savedInstanceState == null) {
selectItem(0);
}
// Init Ads
// initAds();
}
@Override
protected void onResume() {
super.onResume();
setTheme();
}
/**
* Method to show navigation drawer
*/
private void showNavigationSlider() {
if (mDrawerLayout == null) {
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(
R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(
R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mNavDrawerItems = new ArrayList<NavDrawerItem>();
mNavDrawerItems.add(new NavDrawerItem(navMenuTitles[0],
navMenuIcons.getResourceId(0, -1)));
mNavDrawerItems.add(new NavDrawerItem(navMenuTitles[1],
navMenuIcons.getResourceId(1, -1)));
mNavDrawerItems.add(new NavDrawerItem(navMenuTitles[2],
navMenuIcons.getResourceId(2, -1)));
mNavDrawerItems.add(new NavDrawerItem(navMenuTitles[3],
navMenuIcons.getResourceId(3, -1)));
mNavDrawerItems.add(new NavDrawerItem(navMenuTitles[4],
navMenuIcons.getResourceId(4, -1)));
mNavDrawerItems.add(new NavDrawerItem(navMenuTitles[5],
navMenuIcons.getResourceId(5, -1)));
mNavDrawerItems.add(new NavDrawerItem(navMenuTitles[6],
navMenuIcons.getResourceId(6, -1)));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
menuAdapter = new MenuAdapter(ActivityHome.this);
menuAdapter.setList(mNavDrawerItems);
mDrawerList.setAdapter(menuAdapter);
mDrawerToggle = new ActionBarDrawerToggle
(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.app_name, /* "open drawer" description for accessibility */
R.string.app_name /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
setTheme();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Here im inflating my menu options
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.new_list, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
// here im setting its visibility to true/false depending on the drawer position
menu.findItem(R.id.action_new_list).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
break;
}
// here im adding a click event in the button
case R.id.action_new_list:
aboutClicked();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
onItemClickListerner(position);
}
}
/**
* Display selected navigation drawer list item
* */
private void onItemClickListerner(int position) {
Intent intent = null;
switch (position) {
case 0:
//intent = new Intent(mContext, ActivitySettings.class);
//startActivity(intent);
mDrawerLayout.closeDrawers();
break;
case 1:
//intent = new Intent(mContext, ActivityTheme.class);
//startActivity(intent);
mDrawerLayout.closeDrawers();
break;
case 2:
//updateClicked();
mDrawerLayout.closeDrawers();
break;
case 3:
//moreAppClicked();
mDrawerLayout.closeDrawers();
break;
case 4:
//shareClicked(getString(R.string.share_subject),TabsApplication.getAppUrl());
mDrawerLayout.closeDrawers();
break;
case 5:
//rateAppClicked();
mDrawerLayout.closeDrawers();
break;
case 6:
newListClicked();
mDrawerLayout.closeDrawers();
break;
}
}
/**
* Method to check for updated app.
*/
private void updateClicked() {
try {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id="
+ mContext.getPackageName())));
} catch (ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id="
+ mContext.getPackageName())));
}
}
/**
* Method to rate the app.
*/
private void rateAppClicked() {
try {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id="
+ mContext.getPackageName())));
} catch (ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id="
+ mContext.getPackageName())));
}
}
/**
* Method to share app via different available share apps
*/
private void shareClicked(String subject, String text) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(intent,
getString(R.string.share_via)));
}
/**
* Method to Show more apps from Developer
*/
private void moreAppClicked() {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(String
.format("market://search?q=pub:%s",
AppConfig.PLAYSTORE_ACCOUNT_NAME))));
} catch (ActivityNotFoundException anfe) {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse(String
.format("https://play.google.com/store/apps/developer?id=%s&hl=en",
AppConfig.PLAYSTORE_ACCOUNT_NAME))));
}
}
/**
* Method to show about
*/
private void aboutClicked() {
try {
String appname = null;
appname = getString(R.string.app_name_message) + " "
+ getString(R.string.app_name) + "" + "\n\n";
String appversion = getString(R.string.app_version)
+ " "
+ mContext.getPackageManager().getPackageInfo(
mContext.getPackageName(),
PackageInfo.CONTENTS_FILE_DESCRIPTOR).versionCode
+ "\n\n";
String name = getString(R.string.response_message);
showAboutCustomDialog(getString(R.string.about), appname
+ appversion + name, getString(R.string.ok),
getString(R.string.cancel), true);
} catch (NameNotFoundException ex) {
ex.printStackTrace();
}
}
/**
* Method to show custom dialog.
*
* @param title
* @param message
* @param okText
* @param cancelText
* @param singleButtonEnabled
*/
private void showAboutCustomDialog(String title, String message,
String okText, String cancelText, boolean singleButtonEnabled) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.about_dialog_view, null);
RelativeLayout titlebarView = (RelativeLayout) dialogView
.findViewById(R.id.title_bar_view);
titlebarView.setBackgroundColor(ResourcesContentManager.getInstance()
.getTitleBarColor(mContext, -1));
Button okButton = (Button) dialogView.findViewById(R.id.ok_button);
Button cancelButton = (Button) dialogView
.findViewById(R.id.cancel_button);
okButton.setBackgroundResource(ResourcesContentManager.getInstance()
.getButtonBackgroundResource(mContext, -1));
okButton.setTextAppearance(mContext, ResourcesContentManager
.getInstance().getButtonTextStyle(mContext, -1));
cancelButton.setBackgroundResource(ResourcesContentManager
.getInstance().getButtonBackgroundResource(mContext, -1));
cancelButton.setTextAppearance(mContext, ResourcesContentManager
.getInstance().getButtonTextStyle(mContext, -1));
TextView titleTextview = (TextView) dialogView
.findViewById(R.id.title_textview);
TextView messageTextview = (TextView) dialogView
.findViewById(R.id.message_textview);
TextView name = (TextView) dialogView.findViewById(R.id.email_textview);
name.setText("test@test.com");
if (mCustomDialog != null) {
mCustomDialog.dismiss();
mCustomDialog = null;
}
mCustomDialog = new Dialog(mContext,
android.R.style.Theme_Translucent_NoTitleBar);
mCustomDialog.setContentView(dialogView);
mCustomDialog.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == keyCode) {
dialog.dismiss();
}
return false;
}
});
mCustomDialog.setOnDismissListener(new OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
}
});
mCustomDialog.setCanceledOnTouchOutside(false);
titleTextview.setText(title);
messageTextview.setText(message);
if (singleButtonEnabled) {
okButton.setText(okText);
cancelButton.setVisibility(View.GONE);
} else {
okButton.setText(okText);
cancelButton.setText(cancelText);
}
/**
* Listener for OK button click.
*/
okButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCustomDialog.dismiss();
}
});
/**
* Listener for Cancel button click.
*/
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCustomDialog.dismiss();
}
});
mCustomDialog.show();
}
/**
* Method to create new list
*/
private void newListClicked() {
showCreateListDialog("Nueva lista de compras", "Crear", getString(R.string.cancel), false);
}
/**
* Method to create new list dialog
*
* @param title
* @param message
* @param okText
* @param cancelText
* @param singleButtonEnabled
*/
private void showCreateListDialog(String title,
String okText, String cancelText, boolean singleButtonEnabled) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.new_list_dialog_view, null);
RelativeLayout titlebarView = (RelativeLayout) dialogView
.findViewById(R.id.title_bar_view);
titlebarView.setBackgroundColor(ResourcesContentManager.getInstance()
.getTitleBarColor(mContext, -1));
Button okButton = (Button) dialogView.findViewById(R.id.ok_button);
Button cancelButton = (Button) dialogView.findViewById(R.id.cancel_button);
okButton.setBackgroundResource(ResourcesContentManager.getInstance().getButtonBackgroundResource(mContext, -1));
okButton.setTextAppearance(mContext, ResourcesContentManager.getInstance().getButtonTextStyle(mContext, -1));
cancelButton.setBackgroundResource(ResourcesContentManager.getInstance().getButtonBackgroundResource(mContext, -1));
cancelButton.setTextAppearance(mContext, ResourcesContentManager.getInstance().getButtonTextStyle(mContext, -1));
TextView titleTextview = (TextView) dialogView.findViewById(R.id.title_textview);
nameList = (EditText) dialogView.findViewById(R.id.list_name);
if (mCustomDialog != null) {
mCustomDialog.dismiss();
mCustomDialog = null;
}
mCustomDialog = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar);
mCustomDialog.setContentView(dialogView);
mCustomDialog.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == keyCode) {
dialog.dismiss();
}
return false;
}
});
mCustomDialog.setOnDismissListener(new OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
}
});
mCustomDialog.setCanceledOnTouchOutside(false);
titleTextview.setText(title);
if (singleButtonEnabled) {
okButton.setText(okText);
cancelButton.setVisibility(View.GONE);
} else {
okButton.setText(okText);
cancelButton.setText(cancelText);
}
/**
* Listener for OK button click.
*/
okButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// if list has valid name, save
String name = nameList.getText().toString();
if(!name.matches("")){
Intent intent = new Intent(mContext, ActivityAddProductsToList.class);
startActivity(intent);
mCustomDialog.dismiss();
}else{
AppUtils.showToast(mContext, "Por favor ingrese el nombre de la nueva lista");
}
}
});
/**
* Listener for Cancel button click.
*/
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCustomDialog.dismiss();
}
});
mCustomDialog.show();
}
private void selectItem(int position) {
switch (position) {
case 0:
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content,
FragmentPageSlidingTabStrip.newInstance(),
FragmentPageSlidingTabStrip.TAG).commit();
break;
default:
// mFragment = new PlanetFragment();
// Bundle args = new Bundle();
// args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
// mFragment.setArguments(args);
//
// getSupportFragmentManager().beginTransaction()
// .add(R.id.content, mFragment).commit();
break;
}
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
protected void onDestroy() {
if (mContext != null) {
if (mCustomDialog != null) {
mCustomDialog.dismiss();
mCustomDialog = null;
}
mDrawerList = null;
mDrawerTitle = null;
mTitle = null;
navMenuTitles = null;
mFragmentManager = null;
mFragment = null;
mDrawerLayout = null;
mDrawerToggle = null;
mContext = null;
super.onDestroy();
}
}
/**
* Method to set themes backgrounds
*/
private void setTheme() {
int actionBarTitleId = Resources.getSystem().getIdentifier(
"action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
TextView title = (TextView) findViewById(actionBarTitleId);
title.setTextAppearance(mContext, R.style.ActionBarTitleTextStyle);
}
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(ResourcesContentManager.getInstance()
.getTitleBarColor(mContext, -1)));
mDrawerList.setBackgroundColor(ResourcesContentManager.getInstance()
.getScreenBgColor(mContext, -1));
PagerSlidingTabStrip tabStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabStrip.setIndicatorColor(ResourcesContentManager.getInstance()
.getTitleBarColor(mContext, -1));
}
/**
* Method to inits the ads banner view
*/
/*
private void initAds() {
if (mAdsView == null) {
mAdsView = (RelativeLayout) findViewById(R.id.bottom_ads_view);
}
if (AppConfig.BANNER_ADS_ENABLED) {
mAdsView.setVisibility(View.VISIBLE);
AppUtils.addAdsBannerView(ActivityHome.this, mAdsView);
} else {
mAdsView.setVisibility(View.GONE);
}
}
*/
}
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/content_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/bottom_ads_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/bottom_ads_view" >
</RelativeLayout>
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="@dimen/slider_width"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:background="@color/slider_bg_color"
android:choiceMode="singleChoice"
android:divider="@color/slider_divider_color"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
new_list.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_new_list"
android:icon="@drawable/action_search"
android:title="@string/action_new_list"
android:showAsAction="ifRoom|withText" />
</menu>
Change following line in onCreateOptionsMenu
return super.onCreateOptionsMenu(menu);
to
return true;