I am using the [SlidingMenu][1] Library in my App and have made a menu but I am not sure how to customize this.
First, the Up Navigation button on the action bar does not pull up the menu. It just does not do anything when clicked. The menu works by sliding in anywhere on the screen but not with that button.
Also, is there a way to change that up navigation '<' icon and make it work like Google + or others with the animation of the three horizontal lines?
My code is as follows:
public class BaseActivity extends SlidingFragmentActivity {
private int mTitleRes;
protected ListFragment mFrag;
public BaseActivity(int titleRes) {
mTitleRes = titleRes;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(mTitleRes);
// set the Behind View
setBehindContentView(R.layout.menu_frame);
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mFrag = new MenuListFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
} else {
mFrag = (ListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
}
// customize the SlidingMenu
SlidingMenu sm = getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
And the main activity:
public class StatusActivity extends BaseActivity {
public StatusActivity() {
super(R.string.title_status_page);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
setSlidingActionBarEnabled(false);
..
..
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
getSlidingMenu().showContent();
}
return false;
}
Related
Here is my drawerActivity
public class DrawerActivity extends AppCompatActivity {
public Toolbar toolbar;
public static DrawerLayout drawerLayout;
public ActionBarDrawerToggle drawerToggle;
public NavigationView navigationView;
public Context mContext;
private TextView empname, businame;
private ImageView busiimg;
private String[] drawerItem;
public NavigationView getNavigationView() {
return navigationView;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = DrawerActivity.this;
setContentView(R.layout.activity_drawer);
}
#Override
public void setContentView(int layoutResID) {
drawerLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_drawer, null);
FrameLayout activityContainer = (FrameLayout) drawerLayout.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(drawerLayout);
initToolbar();
}
public void initToolbar() {
try{
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Manage vendor");
setSupportActionBar(toolbar);
}catch (Exception e){
e.printStackTrace();
}
}
private void setUpNav() {
drawerLayout = findViewById(R.id.activity_container);
drawerToggle = new ActionBarDrawerToggle(DrawerActivity.this, drawerLayout, R.string.app_name, R.string.app_name);
drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView = findViewById(R.id.nav_main);
drawerToggle.syncState();
}catch (Exception e){
e.printStackTrace();
}
}
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setUpNav();
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
/* #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_search, menu);
return true;
}*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item))
return true;
// 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();
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static void openDrawer(){
drawerLayout.openDrawer(drawerLayout);
}
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
Here is the code where I am accessing drawer when image clicked in another activity
drawerImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DrawerActivity.openDrawer();
}
});
I am implementing navigation drawer in my android application. I have a separate activity for navigation drawer. I will extend the drawer activity where ever I want.
Now the issue is I have drawer image in bottom bar. When I click the image in bottom app bar I have to open the drawer. For one activity I want to open drawer by clicking the image in bottom bar. I know if I extend the drawer activity the hamburger icon will visible in toolbar that I don't want to achieve in this activity.
So how to avoid this scenario by using bottom bar?
Extend your draweractivity to your other activity.
try this:
public class MainActivity extends DrawerActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
drawerImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDrawer();
}
});
}
}
Try implementating an interface.
Create an interface where you want to access the drawer activity.
public interface OnDrawerListener{
void onDrawerClick();
}
OnDrawerListener onDrawerListener;
drawerImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onDrawerListener.onDrawerClick();
}
});
Then implement this interface in your DrawerActivity.
public class DrawerActivity extends AppCompatActivity implements InterfaceActivity.OnDrawerListener{ //InterfaceActivity is activity where you have created your interface.
private InterfaceActivity.OnDrawerListener someInterface;
#Override
protected void onCreate(Bundle savedInstanceState) {
someInterface = (InterfaceActivity.OnDrawerListener) this;
}
//Your interface override
#Override
public void onDrawerClick(){
openDrawer();
}
}
I am trying to debug an app built with fragments. Since I don't know nearly anything about fragments, I have some problems with them.
I want the action button to be transformed in back button when I open a fragment from MainActivity or another fragment.
I kinda made that work, but when I press back from a fragment and it resumes the other the back button transforms back to home button.
I want to know how I can make that home button behave like the back button on Android when it transforms to the back arrow. It does nothing currently.
Here is some code I think it's relevant, I will provide more if you need:
MainActivity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_trash_all:
DataHolder.cart = null;
isTrashButtonPresent = false;
supportInvalidateOptionsMenu();
inflateFragment(CartFragment.newInstance(null, null));
resolveCircularText(DataHolder.getOverallCartQuantity());
return true;
case R.id.btnMyMenu:
if (drawer.getDrawerLockMode(GravityCompat.START)
==DrawerLayout.LOCK_MODE_LOCKED_CLOSED) {
onBackPressed();
} else {
drawerToggle();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void drawerToggle() {
if (drawer.isDrawerVisible(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
private void setupNavigationDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(
this, drawer, toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close);
mDrawerToggle.setHomeAsUpIndicator(R.drawable.drawer_icon);
drawer.addDrawerListener(mDrawerToggle);
navigationView.setNavigationItemSelectedListener(this);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
setDrawerState(true);
}
public void setDrawerState(boolean isEnabled) {
if (isEnabled) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,
GravityCompat.START);
mDrawerToggle.syncState();
} else {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
GravityCompat.START);
//Drawable backButton = ContextCompat.getDrawable(this,
R.mipmap.ic_back_button);
getSupportActionBar().setHomeAsUpIndicator(null);
}
}
Fragments:
OnCreate:
if (getActivity() != null) {
((MainActivity) getActivity()).setDrawerState(false);
}
#Override
public void onDestroy() {
super.onDestroy();
if (getActivity() != null) {
((MainActivity) getActivity()).setDrawerState(true);
}
}
Thank you in advance!
Try using Interface class:
public interface DrawerViewInterface {
void lockDrawer();
void unlockDrawer();
void showBackIcon();
void showHamburgerIcon();
}
Now, Implement this Interface class in your MainActivity:
public class MainActivity extends AppCompactActivity implements DrawerViewInterface {
#Override
public void lockDrawer() {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
#Override
public void unlockDrawer() {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
#Override
public void showBackIcon() {
mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public void showHamburgerIcon() {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
mActionBarDrawerToggle.setDrawerIndicatorEnabled(true);
}
}
Now, In your Fragment, call whatever you want by:
((DrawerViewInterface) getActivity()).showHamburgerIcon();
or
((DrawerViewInter) getActivity()).showBackIcon();
I'm newbee on the android programming then I have a problem to integrate a part of code.
public class HomeActivity extends SherlockFragmentActivity
implements ActionBar.OnNavigationListener, VideoListFragment.OnVideoSelectedListener{
// create object of ActionBar and VideoListFragment
ActionBar actionbar;
VideoListFragment videoListFrag;
int selectedItem;
private AdController ad;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// add channel list array to actionbar spinner
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.channel_name, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
// remove actionbar title and add spinner to actionbar
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
}
// create option menu
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.home, menu);
return true;
}
// listener for option menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuShare:
// share google play link of this app to other app such as email, facebook, etc
Intent iShare = new Intent(Intent.ACTION_SEND);
iShare.setType("text/plain");
iShare.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.subject));
iShare.putExtra(Intent.EXTRA_TEXT, getString(R.string.message)+" "+getString(R.string.gplay_web_url));
startActivity(Intent.createChooser(iShare, getString(R.string.share_via)));
return true;
case R.id.menuRate:
// open google play app to ask user to rate & review this app
Intent iRate = new Intent(Intent.ACTION_VIEW);
iRate.setData(Uri.parse(getString(R.string.gplay_url)));
startActivity(iRate);
return true;
case R.id.menuAbout:
// open About app page
Intent iAbout = new Intent(this, AboutActivity.class);
startActivity(iAbout);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
// TODO Auto-generated method stub
selectedItem = itemPosition;
// create object of VideoListFragment and send data position to that fragment
videoListFrag = new VideoListFragment();
Bundle bundle = new Bundle();
bundle.putInt("position", itemPosition);
videoListFrag.setArguments(bundle);
// call video list fragment with new data
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, videoListFrag, "VIDEO_LIST_FRAGMENT")
.commit();
return true;
}
#Override
public void onVideoSelected(String ID) {
// TODO Auto-generated method stub
// call player page to play selected video
Intent i = new Intent(this, PlayerActivity.class);
i.putExtra("id", ID);
startActivity(i);
}
#Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_home);
ad = new AdController(this, "MY_LB_SECTION_ID");
ad.loadStartAd("MY_LB_AUDIO_ID", "MY_LB_REENGAGEMENT_ID");
AppTracker.startSession(this, "APPFIREWORKS_API_KEY");
}
#Override
public void onPause() { super.onPause();
if(ad != null) { ad.destroyAd();
} if(!isFinishing()) { AppTracker.pause(getApplicationContext());
} }
#Override
public void onResume() { super.onResume();
AppTracker.resume(getApplicationContext());
}
#Override
public void onDestroy() { super.onDestroy();
if(ad != null) { ad.destroyAd();
} AppTracker.closeSession(getApplicationContext(),true);
}
}
The problem is on on create.
Sorry for my bad english.
Thanks for your response.
Regards
You have 2 onCreate functions in the same Activity. You can only have one. That's why its complaining that there is already a method with same signature. Delete one of them and move the stuff from one to other. As mentioned by #Ravn in comments, you can have multiple functions with same name but ofcourse with different arguments.
You cannot have method with same signature twice
#Override
public void onCreate(Bundle savedInstanceState)
#Override
public void onCreate(Bundle b)
I need to extend Two classes from a Single Class.My class Wants to extend Both ListActivity & MainActivity.
I found a question similar to this.
But i don't know how to Implement this https://stackoverflow.com/a/5836735/2781359
Thanks for your Help.
The Class which has to be Extended is ConnectionEditActivity.
public class ConnectionEditActivity extends ListActivity implements OnClickListener
{
public static Connection connectionParam;
private Connection connection;
private Button save;
private EditText name;
private EditText password;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.connection = connectionParam;
this.save = (Button) this.findViewById(R.id.save);
this.save.setOnClickListener(this);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD)
{
// Don't need the Save button on newer devices
android.widget.LinearLayout.LayoutParams a = (LayoutParams) this.save.getLayoutParams();
a.height = 0;
this.save.setLayoutParams(a);
this.save.forceLayout();
}
this.name = (EditText) this.findViewById(R.id.name);
this.password = (EditText) this.findViewById(R.id.password);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu)
{
// Inflate the menu items for use in the action bar
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.connection_edit_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item)
{
// Handle presses on the action bar items
switch (item.getItemId())
{
case R.id.action_save:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void onResume()
{
super.onResume();
this.name.setText(this.connection.getName());
this.password.setText(this.connection.getPassword());
}
protected void onPause()
{
super.onPause();
this.connection.setName(this.name.getText().toString());
this.connection.setPassword(this.password.getText().toString());
finish();
}
public void onClick(View v)
{
if (v == this.save)
{
this.finish();
}
}
}
Mainactivity
public abstract class MainActivity extends ActionBarActivity
{
protected ListView mDrawerList;
protected DrawerLayout mDrawer;
private CustomActionBarDrawerToggle mDrawerToggle;
private String[] menuItems;
String LOG_TAG = "Remote It";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
// getSupportActionBar().hide();
setContentView(R.layout.activity_main_drawer);
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
_initMenu();
mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
mDrawer.setDrawerListener(mDrawerToggle);
}
private void _initMenu()
{
NsMenuAdapter mAdapter = new NsMenuAdapter(this);
// Add Header
mAdapter.addHeader(R.string.ns_menu_main_header);
// Add first block
menuItems = getResources().getStringArray(R.array.ns_menu_items);
String[] menuItemsIcon = getResources().getStringArray(R.array.ns_menu_items_icon);
int res = 0;
for (String item : menuItems)
{
int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
int id_icon = getResources().getIdentifier(menuItemsIcon[res], "drawable", this.getPackageName());
NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
// if (res==1) mItem.counter=12; //it is just an example...
// if (res==3) mItem.counter=3; //it is just an example...
mAdapter.addItem(mItem);
res++;
}
// Second Block
mAdapter.addHeader(R.string.ns_menu_main_header2);
menuItems = getResources().getStringArray(R.array.ns_menu_itemss);
String[] menuItemsIcons = getResources().getStringArray(R.array.ns_menu_items_iconss);
int ress = 0;
for (String item : menuItems)
{
int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
int id_icon = getResources().getIdentifier(menuItemsIcons[ress], "drawable", this.getPackageName());
NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
// if (res==1) mItem.counter=12; //it is just an example...
// if (res==3) mItem.counter=3; //it is just an example...
mAdapter.addItem(mItem);
res++;
}
mDrawerList = (ListView) findViewById(R.id.drawer);
if (mDrawerList != null)
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
/*
* The action bar home/up should open or close the drawer.
* ActionBarDrawerToggle will take care of this.
*/
if (mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle
{
public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout)
{
super(mActivity, mDrawerLayout, R.drawable.ic_drawer, R.string.ns_menu_open, R.string.ns_menu_close);
}
#Override
public void onDrawerClosed(View view)
{
getSupportActionBar().setTitle(getString(R.string.ns_menu_close));
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView)
{
getSupportActionBar().setTitle(getString(R.string.ns_menu_open));
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
mDrawer.closeDrawer(mDrawerList);
switch (position)
{
case 1:
Intent a = new Intent(MainActivity.this, Home.class);
startActivity(a);
break;
case 2:
Intent ac = new Intent(MainActivity.this, ConnectionListActivity.class);
startActivity(ac);
break;
default:
}
}
EDIT
I need to Extend it.Because the MainActivity has the navigation drawer.Now ConnectionEditActivity
doesn't shows the navigationDrawer nor the ActionBar .But i need to show the ActionBar
Any Suggestions ??
In Java you can't extend multiple classes, and for a good reason. Take for example what you are trying to accomplish by extending MainActivity and ListActivity. In your new class, when you call:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...
}
Which onCreate() are you overriding? The one from ListActivity, or the one from MainActivity?
What the link you posted is saying is that instead of inheriting from another object, you compose your new object of the one you are trying to use. For example:
public class NewClass extends OldClass1 {
private OldClass2 mOldClass2 = new OldClass2();
#Override
public methodFromOldClass1() {
}
public methodFromOldClass2() {
mOldClass2.methodFromOldClass2();
}
}
The problem with this approach is that the methods from MainActivity and ListActivity are still going to have the same name, which although you can work around, it will become a headache quickly.
So the problem is a result of how you designed your class hierarchy. You will need to think about what functions you need from MainActivity, and what functions from ListActivity and choose how to reimplement your objects.
My Suggestion, since ListActivity only makes it slightly easier to work with lists (not that much easier) you can just skip it and implement the code related to the list on your own, and that way you can just extend MainActivity
You need to start by identifying what parts of MainActivity you need to inherit from, and what do you need from ListActivity.
Then, you have various possibilities:
Trivially, not extending ListActivity. Extending ListActivity only provides you with utility methods to work with the ListView, but you can totally have a ListView in an Activity without it being a ListActivity.
Create a utility class that contains extracted methods you need from MainActivity and call these methods from both your new class and MainActivity.
Modify MainActivity so that it extends ListActivity. After all it does contain a ListView (you'd loose the ActionBar thing, though).
I'm currently using FragmentStatePagerAdapter and I would like it to have a "navigate up" feature (DisplayHomeAsUpEnabled). When I set the following code it shows up as a correct navigate back button. But nothing happens when I click on it, can you guys see anything that I', doing wrong in my code, I get no error while pressing the "navigate up" button and I get no compiler errors.
Here is the code, its in the "public boolean onOptionsItemSelected(MenuItem item) {" it gets interesting at the bottom.
public class ShareholdingDetailFragment extends FragmentActivity {
final int NUM_ITEMS = Portfolio.getPortfolio().count();
MyAdapter mAdapter;
ViewPager mPager;
Bundle extras;
#Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println(NUM_ITEMS + "e");
super.onCreate(savedInstanceState);
setContentView(R.layout.shareholdingdetailview_fragment_wrapper);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
public int getCount() {
return NUM_ITEMS;
}
public Fragment getItem(int position) {
return ShareholdingFragment.newInstance(position);
}
}
public static class ShareholdingFragment extends Fragment {
int mNum;
static ShareholdingFragment newInstance(int num) {
ShareholdingFragment f = new ShareholdingFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
#SuppressLint({ "NewApi", "NewApi" })
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shareholdingdetailview_fragment, container, false);
/****************Setting the Display as home and it shows*******************/
ActionBar bar = getActivity().getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
/**********************************/
return view;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home://Not working
System.out.println("test");//This isn't printed out
Intent upIntent = new Intent(getActivity(), DetailShareHoldingActivity.class);
if (NavUtils.shouldUpRecreateTask(getActivity(), upIntent)) {
getActivity().finish();
} else {
NavUtils.navigateUpTo(getActivity(), upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
Try adding: setHasOptionsMenu(true); in your onCreate() in ShareholdingFragment.
Add setHomeButtonEnabled(true) on API Level 14 and higher when you configure your action bar. On API Level 11-13, the home button is enabled automatically.
If you're using the new Toolbar and ActionbarDrawerToggle. You can assign clickHandler directly. For my activities that have this drawer toolbar I implemented an interface to enable drawer if at root,
#Override
public void enableDrawer(boolean enable) {
mDrawerToggle.setDrawerIndicatorEnabled(enable);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Pop fragment back stack or pass in a custom click handler from the fragment.
}
});
}