How can I convert tabActivity to fragment - android

I used Tab-activity in my application,but this class is depreciated now,how can I replace this with fragment.I have implemented sub tabs also for each tabs.Can any one help me by providing sample code to implement this changes?

Check out these links
http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html
https://stackoverflow.com/questions/9714650/converting-tabactivity-into-fragmentactivity
The first has sample code on how to build tabs using fragments (you can pretty much use it as is), and the latter is a discussion about the same.

Use actionbar with only tabs.ActionBarSherlock library can do this very easily. Go through TabNavigationCollapsed class in samples of ActionBarSherlock.
public class TabNavigationCollapsed extends SherlockActivity implements ActionBar.TabListener {
private TextView mSelected;
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_navigation);
mSelected = (TextView)findViewById(R.id.text);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
mSelected.setText("Selected: " + tab.getText());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}
}

Related

Need some explanations about Android fragments and ActionBar tabs

I am trying to understand Android fragments and navigation, but there is something I just don't know how to do. I have created an app, with a MainActivity containing a viewPager :
public class MainActivity extends FragmentActivity implements TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabNames = {"Tab 1", "Tab 2", "Tab 3"};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(int i = 0 ; i < tabNames.length ; i++)
actionBar.addTab(actionBar.newTab().setText(tabNames[i]).setTabListener(this));
}
#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) {}
}
Here is TabsPagerAdapter :
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int index)
{
if(index == 0) return new FirstFragment();
else if(index == 1) return new SecondFragment();
else return new ThirdFragment();
}
#Override
public int getCount()
{
return 3;
}
}
And my FirstFragment is a list so it extends ListFragment. Here is what it looks like :
Now I want to go to another view if I click an item. Before I used to do it like this in apps without action bar and tabs :
Intent i = new Intent(this.getActivity().getApplicationContext(), MyNewActivity.class);
startActivity(i);
But now when I do this it doesn't display the action bar on top of the screen anymore, and I also want to keep the navigation state on this tab, if I go to another tab and then come back. What should I do?
Thanks for your help.
It is better to let each individual fragment manage its own menu items (actionbar) so you have to call setHasMenuOptions(true) in each fragment that you want to have menu options in. Get a reference to the actionbar in onActivityCreated() and configure your actionbar how you want it there. You will also have to override the oncreateoptionsmenu and onOptionsItemSelected in the fragment to handle menu item clicks.
Also using the view pager and tabs you want to make each tab a fragment. I don't know about making each tab an activity, and I don't even think that is possible, and if you are doing that then that is your problem. I don't see that from your code, and that is good.
Each tab needs to be a Fragment, so convert all of your activities into fragments and then use the supportFragmentManager to dynamically add and replace fragments to your framelayout resource, or override getItem and return the correct fragment as needed.

ActionBar: Swipe between Tabs with ViewPager

I tried to make swiping between my tabs possible in my app. To do this i followed this
answer:
How do I implement swiping between tabs on Android?
Now that I´ve inserted this code, I get lots of errors.
This is my code:
public class MyActivity extends Activity implements OnClickListener {
public ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener{
#Override
public void onPageSelected(int position){
getActionBar().setSelectedNavigationItem(position);
}
});
tabA = mActionBar.newTab();
tabA.setIcon(R.drawable.logo);
tabA.setCustomView(R.layout.tab_a);
tabA.setTabListener(new ActionBar.TabListener(){
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction ft){
mViewPager.setCurrentItem(tab.getPosition());
}
});
mActionBar.addTab(tabA);
}}
Errors:
tabA.setTabListener(new ActionBar.TabListener(){
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction ft){
mViewPager.setCurrentItem(tab.getPosition());
}
});
Class 'Anonymous derived from TabListener' must either be declared abstract or declare abstract method 'OnTabUnselected(Tab,FragmentTransaction)' in 'TabListener'.
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener{
public void onPageSelected(int position){
getActionBar().setSelectedNavigationItem(position);
}
});
Error at the "{" after SimpleOnPageChangeListener:
'(' or '[' expected
')' expected
';' expected
and another Error at the end: ')' 'Unexpected Token'
Any help is appreciated
ViewPager is part of the Support Library. You need to add the android-support-v4.jar file (found in <sdk>\extras\android\support\v4 as a library for your project.
See the Setup guide for instructions.
EDIT As to the new errors:
"()" are missing in new ViewPager.SimpleOnPageChangeListener(){
TabListeners must implement a few more methods. See http://developer.android.com/reference/android/app/ActionBar.TabListener.html

Change colour of line under the tab if selected

I have used a TabHost to setup my tabs, but now i want to change the color of line under the tab if selected. I know this is possible only with action bar or Action bar sherlock. So i searched a lot on SO but found no proper answer as to how to change the colour of line under the tabs. I have managed to set up the tabs using action bar sherlock library.
Here is a image of what i want
Hope it shows what i want.
This is the main class of
public class MainActivity extends SherlockActivity implements
ActionBar.TabListener {
private TextView mSelected;
public int THEME = R.style.Theme_Sherlock;
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(THEME); // Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSelected = (TextView) findViewById(R.id.text);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
tab.setTabListener(this);
getSupportActionBar().addTab(tab);
}
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
mSelected.setText("Selected: " + tab.getText());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}
}
and the xml is
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="tab_navigation_content" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
and i have used this library.

ActionBarSherlock - selecting tab by clicking on other tabs

I have an example with 3 tabs and one button.
public class MainActivity extends SherlockActivity implements TabListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
setContentView(R.layout.activity_main);
addTab("1", 0, false);
addTab("2", 1, false);
addTab("3", 2, false);
Button cmdClick = (Button) findViewById(R.id.cmdClick);
cmdClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getSupportActionBar().setSelectedNavigationItem(0);
}
});
}
private void addTab(String tabTitle, int position, boolean setSelected) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText(tabTitle);
tab.setTabListener(this);
getSupportActionBar().addTab(tab, position, setSelected);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Selected " + tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Unselected " + tab.getPosition());
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Reselected " + tab.getPosition());
}
}
When I click on the button it automatically selects first tab. I would like to automatically select first tab whenever I click on the second or on the third tab. I tried like this
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("example", "Selected " + tab.getPosition());
getSupportActionBar().setSelectedNavigationItem(0);
}
but it doesn't work. Any ideas?
Thanks.
Edit:
Maybe this example doesn't have any sense but this is just a simplified example of what I'm trying to do. I would like to have 2 tabs by default, one with title "1" and second one with title "+". When user selects "+" tab I would like to create new tab with title "2" (between tabs "1" and "+") and to automatically select tab "2".
First of all I'd like to discourage this kind of behavior. This will be VERY confusing for your users and frankly doesn't make any sense.
That said I tried hacking it but it doesn't seem like it's that easy to do. I did following:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getPosition() == 0) {
int tabs = mActivity.getSupportActionBar().getTabCount();
if(tabs > 3) {
ActionBar.Tab test = mActivity.getSupportActionBar().getTabAt(2);
test.select();
return;
}
}
Edit:
For the behavior your describing I think it would be much easier to implement it by creating your own tabs. You could use radio buttons and/or buttons to implement it easily.
Now this causes the tab indicator to still point at the old tab but the fragment inside is updated to the selected one =/ so not quite there...

OnTabSelected not being called at all when using actionbarsherlock

I've done stuff like this before and this is a super simple example but I can't seem to get it working using actionbarsherlock v4.1.
This is the main activity
public class VanityActivity extends SherlockFragmentActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
//restore
}
com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
com.actionbarsherlock.app.ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Website");
tab1.setTabListener(new HeaderTabListener(getApplicationContext()));
tab1.setTag(1);
actionBar.addTab(tab1);
com.actionbarsherlock.app.ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Portfolio");
tab2.setTabListener(new HeaderTabListener(getApplicationContext()));
tab2.setTag(2);
actionBar.addTab(tab2);
com.actionbarsherlock.app.ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("Team");
tab3.setTabListener(new HeaderTabListener(getApplicationContext()));
tab3.setTag(3);
actionBar.addTab(tab3);
}
}
And my HeaderTabListener class:
public class HeaderTabListener implements
com.actionbarsherlock.app.ActionBar.TabListener {
private Context context;
public HeaderTabListener(Context context){
this.context = context;
}
#Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
int i=((Integer)tab.getTag()).intValue();
if(tab.getPosition()==0){
ft.replace(android.R.id.content, CompanyFragment.newInstance(i));
}else if(tab.getPosition()==1){
ft.replace(android.R.id.content, PortfolioFragment.newInstance(i));
}
}
#Override
public void onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
Log.v("Tab selected", tab.getText().toString());
}
#Override
public void onTabReselected(com.actionbarsherlock.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
Log.v("Tab selected2", tab.getText().toString());
}
}
And finally one of the fragments (both fragments are the same but the text in each layout is different)
public class PortfolioFragment extends SherlockFragment {
private static final String KEY_POSITION="position";
private static final String KEY_TEXT="text";
static PortfolioFragment newInstance(int position) {
PortfolioFragment frag=new PortfolioFragment();
Bundle args=new Bundle();
args.putInt(KEY_POSITION, position);
frag.setArguments(args);
return(frag);
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.fragment_portfolio, container, false);
int position=getArguments().getInt(KEY_POSITION, -1);
return(result);
}
}
So when it first loads the the tabs are all displayed correctly, the first one his highlighted and the text in CompanyFragment layout displays. But then I am unable to click any of the other tabs to change the content and my logs inside of onTabSelected just never gets called. Any thoughts? Thanks for reading.
First off, let me tell you that I have no experience with ActionBarSherlock. I looked at your code to get a few ideas for my application. Seeing as it hasn't been answered since you posted it a few month ago, I would like to share my first impression.
As far as I know, ActionBarSherlock exposes the same functionality as the most recent Android APIs. Therefore, I do think that you are missing a simple method call. You would need to call commit() (Android Developer Guide reference) after you replace the fragment. Therefore, change the line, and all similar lines from;
ft.replace(android.R.id.content, PortfolioFragment.newInstance(i));
to
ft.replace(android.R.id.content, PortfolioFragment.newInstance(i)).commit();.
I hope this will solve your problem although I do realise you state that the ònTabSelected()` method is never called. Without debugging the difference between the problems would, however, seem transparent.

Categories

Resources