actionbarsherlock using tabnavigation tan navigation - android

I have a late requirement change that requires tab navigation. I have already using ActionBarSherlock in my application. Previously my UI consisted of Activities. However from what i read the TabBar requires Fragment Activities? How is there any way i can keep my existing code and implement the TabBar or would this require a complete re write of my application ? All of my activities extends SherlockActivity ?
Kind Regards,
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class NewMainActivity extends SherlockFragmentActivity implements ActionBar.TabListener,
ViewPager.OnPageChangeListener
{
private ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.textlayout);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));
mViewPager.setOnPageChangeListener(this);
//mViewPager.setPageMarginDrawable(R.drawable.border);
mViewPager.setPageMargin(16);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("First Tab").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Second Tab").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Third Tab").setTabListener(this));
}
#Override
public void onPageScrollStateChanged(int arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
}
private class MainPagerAdapter extends FragmentPagerAdapter
{
public MainPagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
switch (position)
{
case 0:
// return (mFragmentA = new FragmentA());
case 1:
// return (mFragmentB = new FragmentB());
case 2:
// return (mFragmentC = new FragmentC());
}
return null;
}
#Override
public int getCount()
{
return 3;
}
}
}

Of course you won't have to re-write your application. You just need some refractoring that's all.
First you should start by moving your code to SherlockFragment(s).
So let's say you have 3 Activities: ActivityA, ActivityB and ActivityC.
1 - Create 3 SherlockFragments: FragmentA, FragmentB and FragmentC.
2 - Move your code to the fragments and make the necessary modifications (e.g. Overrides, Context references, etc.).
3 - Now to setup your main FragmentActivity with Tab navigation:
public class MainActivity extends SherlockFragmentActivity implements
ActionBar.TabListener, ViewPager.OnPageChangeListener {
private FragmentA mFragmentA;
private FragmentB mFragmenB;
private FragmentC mFragmentC;
private ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new MainPagerAdapter(
getSupportFragmentManager()));
mViewPager.setOnPageChangeListener(this);
mViewPager.setPageMarginDrawable(R.drawable.border);
mViewPager.setPageMargin(16);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText(R.string.tab_a_title)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.tab_b_title)
.setTabListener(this));
actionBar.addTab(actionBar.newTab()
.setText(R.string.tab_c_title).setTabListener(this));
}
#Override
public void onPageSelected(int position) {
getSupportActionBar().setSelectedNavigationItem(position);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
private class MainPagerAdapter extends FragmentPagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return (mFragmentA = new FragmentA());
case 1:
return (mFragmentB = new FragmentB());
case 2:
return (mFragmentC = new FragmentC());
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
}
main_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>

Related

Android: Replacing fragments inside tabs

So I am trying to have a screen with three fragments in different tabs, and on one of them have them switch to be another fragment, but I am unable to figure out how I could do this. I have looked at other similar questions, however, I cannot seem to get them working. So if someone could help me exactly figure it out that would be great.
My tabsadapter is this
public class Tabsadapter extends FragmentStatePagerAdapter {
private int TOTAL_TABS = 3;
public Tabsadapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new fragment1();
case 1:
return new fragment2();
case 2:
return new fragment3();
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return TOTAL_TABS;
}
}
and my main is this:
public class main extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener {
private ViewPager tabsviewPager;
private ActionBar mActionBar;
private Tabsadapter mTabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabsviewPager = (ViewPager) findViewById(R.id.tabspager);
mTabsAdapter = new Tabsadapter(getSupportFragmentManager());
tabsviewPager.setAdapter(mTabsAdapter);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1 = getSupportActionBar().newTab().setText("Month").setTabListener(this);
Tab tab2 = getSupportActionBar().newTab().setText("Week").setTabListener(this);
Tab tab3 = getSupportActionBar().newTab().setText("Day").setTabListener(this);
getSupportActionBar().addTab(tab2);
getSupportActionBar().addTab(tab2);
getSupportActionBar().addTab(tab3);
tabsviewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
getSupportActionBar().setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
}
#Override
public void onTabSelected(Tab selectedtab, FragmentTransaction arg1) {
tabsviewPager.setCurrentItem(selectedtab.getPosition()); //update tab position on tap
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
}
}
Any help anyone can provide would be greatly appreciated. Thanks!
On Android Studio, you can create automatically the code for this :
Right click on a folder of your project, choose New -> Tabbed Activity.
Choose the navigation style. (ActionBarTabs if I understood you well)
Change other fields if needed
Click finish
You now have a brand new Tabbed Activity to use :)
You can read the generated code if you want to understand how it works.
Please ask me if you need any help.

how can I change the color of the tabBar in tab fragment (Viewpager)

I tried many times but it's not OK.
I try to change actionBar in MainActivity but the TabBar doesn't change, so I don't no how to change it.
This is my code :
MainActivivty
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private ActionBar actionBar;
private TabsPagerAdapter mAdapter;
private String[] tabs = { "Bài Hát", "Yêu Thích"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ac = getActionBar();
ac.setBackgroundDrawable(new ColorDrawable(Color.rgb(72, 209, 204)));
//ac.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E64260")));
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
if (position == 1) {
TabYeuThich frag = (TabYeuThich) mAdapter
.getFragmentTabYeuThich(1);
frag.resetPage();
} else {
TabBaiHat frag = (TabBaiHat) mAdapter
.getFragmentTabBaiHat(0);
frag.resetPage();
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
This is class : TabsPagerAdapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
FragmentManager fm;
TabYeuThich fragYeuThich;
TabBaiHat fragBaiHat;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
this.fm = fm;
}
#Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch (index) {
case 0:
fragBaiHat = new TabBaiHat();
return fragBaiHat;
case 1:
fragYeuThich = new TabYeuThich();
return fragYeuThich;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
public Fragment getFragmentTabYeuThich(int post) {
return fragYeuThich;
}
public Fragment getFragmentTabBaiHat(int post) {
return fragBaiHat;
}
Please help me ! Thanks all !
Useally I set up the action bar from themes.xml
I recommend you follow up the both tutorials
developer android
action bar explanation

Android - Swiping without Tabs

NoteActivity Code:
public class NoteActivity extends FragmentActivity implements ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Note", "Note Info"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
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(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
TabsPageAdapter.Java
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index)
{
case 0:
return new NoteFragment();
case 1:
return new NoteInfoFragment();
}
return null;
}
#Override
public int getCount()
{
// get item count - equal to number of tabs
return 2;
}
}
So basically, I used an example of Tabs View and got the Tabs working and it looks like this:
What would I have to do to make it just swipeable without the Tabs showing. An example is like Snapchat. It definitely uses the Swipe view control but the tabs are hidden. Can someone please show me how to get this done?
Since you already have a ViewPager in your code, all you have to do is remove the code that creates the ActionBar tabs (under the comment // Adding tabs), as well as the code that synchronizes the tab selection with the current page (start with the ViewPager.OnPageChangeListener and the ActionBar.TabListener callbacks and see if anything breaks).

ActionBar tabs not working with android:theme="#android:style/Theme.Light.NoTitleBar"

I want my application with no title bar, but there is an activity which has two swipe tabs given below
public class APlus_Post extends FragmentActivity implements TabListener {
ViewPager viewPager;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
actionBar.setSelectedNavigationItem(arg0);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
ActionBar.Tab tab1 = actionBar.newTab(); //First Tab
tab1.setText("QAs Wall");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab(); //Second Tab
tab2.setText("Track Companies");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
}
#Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
viewPager.setCurrentItem(arg0.getPosition());
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
}
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
Fragment fragment=null;
if(arg0==0){
fragment=new APlus_PostFragment1();
}
if(arg0==1){
fragment=new APlus_PostFragment2();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
with layout associated post.xml given below
<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"/>
Whenever i run these above code with android:theme="#android:style/Theme.Holo.Light" (in AndroidMenifesto.xml) it works fine, but when i use android:theme="#android:style/Theme.Light.NoTitleBar" it doesn't work.
I want those swipe tabs on action bar but not the title bar in any activity of my application.
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);

Android sherlock FragmentTab clear data

public class PropertyAddActivity extends SherlockFragmentActivity implements OnPageChangeListener, TabListener {
private String TAG="AddActivity: ";
private FileCache fileCache;
private ViewPager mPager;
private ActionBar ab;
private static final int COUNT = 3;
static ArrayList<Integer> mSelectedPropertyType = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.property_add);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
mPager.setOnPageChangeListener(this);
ab = getSupportActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowHomeEnabled(false);
ab.setNavigationMode(NAVIGATION_MODE_TABS);
GlobalProperty.getInstance().product=new PropertyAdd();
ab.addTab(ab.newTab().setText("Property").setTabListener(this));
ab.addTab(ab.newTab().setText("Property Detail 1").setTabListener(this));
ab.addTab(ab.newTab().setText("Property Detail 2").setTabListener(this));
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
//listAttachImage.clear();
mSelectedPropertyType.clear();
super.onDestroy();
}
#Override
public void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) {
//AppLog.logString(TAG+"onPageScrolled");
}
#Override
public void onPageSelected(int position) {
//AppLog.logString(TAG+"onPageSelected position"+position);
getSupportActionBar().setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
//AppLog.logString(TAG+"onPageScrollStateChanged ");
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabSelected position: "+tab.getPosition());
mPager.setCurrentItem(tab.getPosition());
//ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabUnselected");
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
//AppLog.logString(TAG+"onTabReselected");
}
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
//AppLog.logString(TAG+"COUNT: "+COUNT);
return COUNT;
}
#Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
AppLog.logString(TAG+"position: "+position);
switch (position) {
case 0:
AppLog.logString(TAG+"FRAGGGG1111111");
f = Property1AddFragmentActivity.newInstance(position);
break;
case 1:
AppLog.logString(TAG+"FRAGGGG2222222");
f = Property2AddFragmentActivity.newInstance(position);
break;
case 2:
AppLog.logString(TAG+"FRAGGGGG333333");
f = Property3AddFragmentActivity.newInstance(position);
break;
default:
AppLog.logString(TAG+"Default");
break;
}
return f;
}
}}
i had add add 3 tab Now the problem is that when i set some data in tab1 view and move to tab2 the reselect tab1 the data on tab1 is as it is but when i move to tab3 to tab1 then tab1 data is clear. so cany anyof you have idea to solve this?
At the moment your code appears to be partly action bar tabs and partly ordinary fragments.
If you are trying to use action bar tabs then you need a tab listener. In onTabSelected you should add/replace and in onTabUnselected you should remove.
Please see http://developer.android.com/guide/topics/ui/actionbar.html
section "adding navigation tabs" for a good example.

Categories

Resources