SherlockFragmentActivity tabs onBackPressed() - android

SherlockFragmentActivity with 3 tabs How can i add onBackPressed() in all tabs? It is possible? this code is default abs Viewpage totorial from http://www.androidbegin.com/tutorial/android-actionbarsherlock-viewpager-tabs-tutorial/. Any help?
MainActivity.jav
public class MainActivity extends SherlockFragmentActivity {
private FragmentTab2 F2;
ActionBar mActionBar;
ViewPager mPager;
Tab tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
mActionBar.setSelectedNavigationItem(position);
}
};
mPager.setOnPageChangeListener(ViewPagerListener);
ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
mPager.setAdapter(viewpageradapter);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
mActionBar.addTab(tab);
tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
mActionBar.addTab(tab);
tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
mActionBar.addTab(tab);
}
#Override
public void onBackPressed()
{
super.onBackPressed();
F2.onBackPressed();
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
}
FragmentTab2.java
public class FragmentTab2 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmenttab2, container, false);
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
public boolean onBackPressed() {
Toast.makeText(getActivity(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
return false;
}
}
Error
Exception dispatching finished signal.
Exception in MessageQueue callback: handleReceiveCallback
java.lang.NullPointerException
at com.androidbegin.absviewpagertutorial.MainActivity.onBackPressed(MainActivity.java:87)
at android.app.Activity.onKeyUp(Activity.java:2193)
at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
at android.app.Activity.dispatchKeyEvent(Activity.java:2423)
at com.actionbarsherlock.app.SherlockFragmentActivity.dispatchKeyEvent(SherlockFragmentActivity.java:121)

Use onPageSelected to check if user is on FragmentTab2 then initialize F2 as:
public void onPageSelected(int position) {
super.onPageSelected(position);
mActionBar.setSelectedNavigationItem(position);
if(position==1)
F2 = (FragmentTab2)viewpageradapter.getItem(position);
else
F2 =null;
}
and also put null check in onBackPressed of Activity :
#Override
public void onBackPressed()
{
super.onBackPressed();
if(F2 !=null)
F2.onBackPressed();
}

Related

How to disable/enable the ActionBar.TabListener on some condition

I have implemented ActionBar.TabListener with viewpager. Now I want to disable tab click for some condition
I have stopped swipe using TouchListner but want to do same for Tab Click.
Here is my code.
MainActivity.java
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
String check ="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdpter(fragmentManager));
viewPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if ("".equals(check)) {
return true;
}
return false;
}
});
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Tab 1");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Tab 2");
tab2.setTabListener(this);
ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("Tab 3");
tab3.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
viewPager.setCurrentItem(2, false);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
MyAdpter.java
class MyAdpter extends FragmentPagerAdapter{
public MyAdpter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
if(position==0){
fragment = new FragmentA();
}
if(position==1){
fragment = new FragmentB();
}
if(position==2){
fragment = new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
FragmentA.java
public class FragmentA extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a , container,false);
}
}
FragmentB/FragmentC are similar to FragmentA
activity_main.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
ScreenShot
you can do something like this..
in OnCheckedChanged listener for checkbox, you can specify which tabs are enable..
just by doing
// to enable
tabLayout.getChildAt(position).setEnabled(true);
// or to disable
tabLayout.getChildAt(position).setClickable(false);.
use this property to enable or disable clicks on tab.

How to add child fragment without ActionBar and swipe to class fragment

I have Fragment MainActivity class with ActionBar and swipe which work well.
Acording to action user the class FragmentA,FragmentB,FragmentC show in the scren.
How can I add child fragment without ActionBar and without swipe from FragmentA class
public class FragmentA extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a,container,false);
}
}
so from FragmentA I will have
-- FragmentAA
-- FragmentAB
Many Thanks For any help.
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ViewPager viewPager=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.pager);
final ActionBar actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
addTabs(actionBar);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
}
#Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
}
private void addTabs(ActionBar actionBar)
{
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("Tab 1");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("Tab 2");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText("Tab 3");
tab3.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment=null;
if(i==0)
{
fragment=new FragmentA();
}
if(i==1)
{
fragment=new FragmentB();
}
if(i==2)
{
fragment=new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
}

Access variable from within a fragment

I have a main activity consisting of three tabs with each its corresponding fragment. In each of the fragment, there is one textview. Also, I am using ViewPager to help me navigate through the tabs.
When I try to access a TextView from within a fragment, I get a NULL POINTER error. Here is my code:
Fragment 1 (tab1):
public class FragmentA extends Fragment{
TextView tv1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View parentView = inflater.inflate(R.layout.fragment_a, container, false);
//******* Error Here **********
tv1 = (TextView) getView().findViewById(R.id.fragAtv1);
return parentView;
}
}
Main Activity:
public class SwipeTabs extends FragmentActivity implements TabListener{
ViewPager viewPager;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.main_swipe);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MySwipeAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int tab_pos) {
actionBar.setSelectedNavigationItem(tab_pos);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
// Initialize action bar with tabs
actionBar = setupActionBar();
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction arg1) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction arg1) {
}
private ActionBar setupActionBar(){
ActionBar newAB = getActionBar();
newAB.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = newAB.newTab();
tab1.setText("Tab 1");
tab1.setTabListener(this);
newAB.addTab(tab1);
ActionBar.Tab tab2 = newAB.newTab();
tab2.setText("Tab 2");
tab2.setTabListener(this);
newAB.addTab(tab2);
ActionBar.Tab tab3 = newAB.newTab();
tab3.setText("Tab 3");
tab3.setTabListener(this);
newAB.addTab(tab3);
return newAB;
}
}
class MySwipeAdapter extends FragmentStatePagerAdapter{
//Construstor: retuns the
public MySwipeAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch(position){
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
case 2:
fragment = new FragmentC();
break;
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
You can't access getView() and get a non-null value before onCreateView() returns. That doesn't make sense logically. Instead, do this:
tv1 = (TextView) parentView.findViewById(R.id.fragAtv1);
You could also override onViewCreated() but this is simpler.
Instead of using getView.findviewbyId try parentView.findViewById.
tv1 = (TextView) parentView.findViewById(R.id.fragAtv1);

how to renew the fragment when click at the tabs

I would like to create a new fragment every time when I click its tab.
I tried to get the tab and to create a fragment. But it doesn't seem to work.
Here is my code.
Main Activity
public class MainActivity extends FragmentActivity {
private static ViewPager viewPager;
private static TabsPagerAdapter mAdapter;
private static ActionBar actionBar;
// Tab titles
private String[] tabs = { "TopRated", "Hots", "NEW"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
//actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(tabListener));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
static ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
};
TabsPagerAdapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new TopRatedFragment();
case 1:
return new HotStoryFragment();
case 2:
return new AllClassFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
One of the fragments
public class HotStoryFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("Refresh_F","(2) HotStory");
View rootView = inflater.inflate(R.layout.fragment_hotstory, container, false);
TextView tvStudy = (TextView)rootView.findViewById(R.id.text);
return rootView;
}

Selecting tabs not working

currently i have tabs in my activity, i can swipe between the tabs easily.
But here's a bug:
I Can't select a tab!
Here i'm on the new tab.
If i click on upcomming i will get this
I Will need to swype in order to see the content of upcoming!
Why is this happening?
Here's is my codes:
public class Taby extends SherlockFragmentActivity {
FragmentTransaction transaction;
static ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Fragment tabOneFragment = new TabOne();
Fragment tabTwoFragment = new TabTwo();
PagerAdapter mPagerAdapter = new PagerAdapter(
getSupportFragmentManager());
mPagerAdapter.addFragment(tabOneFragment);
mPagerAdapter.addFragment(tabTwoFragment);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setOffscreenPageLimit(2);
mViewPager.setCurrentItem(0);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
getSupportActionBar().setSelectedNavigationItem(
position);
}
});
ActionBar ab = getSupportActionBar();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
com.actionbarsherlock.app.ActionBar.Tab tab1 = ab
.newTab()
.setText("New")
.setTabListener(
new TabListener<TabOne>(this, "tabone", TabOne.class));
com.actionbarsherlock.app.ActionBar.Tab tab2 = ab
.newTab()
.setText("Upcoming")
.setTabListener(
new TabListener<TabTwo>(this, "tabtwo", TabTwo.class));
ab.addTab(tab1);
ab.addTab(tab2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.like, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
case R.id.facebooklike:
String facebookScheme = "fb://profile/" + "192346130896055";
Intent facebookIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(facebookScheme));
startActivity(facebookIntent);
break;
}
return true;
}
#SuppressLint("NewApi")
public static class TabListener<T extends Fragment> implements
ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(Tab arg0,
android.app.FragmentTransaction arg1) {
}
#SuppressLint("NewApi")
public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
mViewPager.setCurrentItem(arg0.getPosition());
}
public void onTabUnselected(Tab arg0,
android.app.FragmentTransaction arg1) {
}
#Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(
com.actionbarsherlock.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(
com.actionbarsherlock.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
public class PagerAdapter extends FragmentPagerAdapter {
private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
public PagerAdapter(FragmentManager manager) {
super(manager);
}
public void addFragment(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
}
mViewPager.setCurrentItem(position); inside the onPageSelected() like that:
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
getSupportActionBar().setSelectedNavigationItem(
position);
mViewPager.setCurrentItem(position);
}
});

Categories

Resources