I'm currently using the brilliant ActionBarSherlock (http://actionbarsherlock.com/) together with the jfeinstein's Sliding menu which is working nicely. I'd like to be able to auto hide the action bar on user inactivity. So if the user does not touch the screen for say 10 seconds, the actionbar hides and the activity displays full screen. If the user then touches the screen again the actionbar re-appears. Is this possible? Would I have to do this from within the activity class or the fragment class? I'd appreciate any advice.
Please find the code for my Main Activity:
public class MainActivity extends SlidingFragmentActivity {
protected ListFragment mFrag;
public MainActivity(){
super();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setSlidingActionBarEnabled(true);
// set the Behind View
setBehindContentView(R.layout.menu_frame);
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mFrag = new SampleListFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
} else {
mFrag = (ListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
}
// set the Above View
setContentView(R.layout.content_frame);
this.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new StartUpFragment())
.commit();
// 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);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Handle other fragments here
// Setup SlidingMenu again
setBehindContentView(R.layout.menu_frame);
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mFrag = new SampleListFragment();
t.add(R.id.menu_frame, mFrag);
t.commit();
SlidingMenu slidingMenu = getSlidingMenu();
slidingMenu.setBehindScrollScale((float) 0.0);
slidingMenu.setBehindWidthRes(R.dimen.actionbar_home_width);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
slidingMenu.setSelectorEnabled(true);
slidingMenu.setSlidingEnabled(true);
}
private void setRetainInstance(boolean b) {
// TODO Auto-generated method stub
return;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
//this configures - clicking the icon also makes the sliding menu slide
case android.R.id.home:
toggle();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class BasePagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragments = new ArrayList<Fragment>();
private ViewPager mPager;
public BasePagerAdapter(FragmentManager fm, ViewPager vp) {
super(fm);
mPager = vp;
mPager.setAdapter(this);
for (int i = 0; i < 3; i++) {
addTab(new SampleListFragment());
}
}
public void addTab(Fragment frag) {
mFragments.add(frag);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
}
}