Tabs aren't working in Android - android

I have run into an obstacle where my tabbed layouts are overlapping their content. I am currently creating an Android app. This is when the first tab is opened. (link to picture below):
This is what happens when I open the second tab (link to picture below):
You might notice that the button that says "Button" is overlapping the MainActivity. That is my problem. I don't want them to overlap. I just want the button to show the other activity (no overlapping). Here is part of my MainActivity.java code:
package com.example.currencyconverter;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("Created By Rohit Nandakumar");
actionBar.setTitle("Currency Converter");
// Specify that tabs should be displayed in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab Frag1 = actionBar.newTab().setText("Converter");
ActionBar.Tab Frag2 = actionBar.newTab().setText("Currencies");
ActionBar.Tab Frag3 = actionBar.newTab().setText("News");
Fragment fragment1 = new Fragment1();
Fragment fragment2 = new Fragment2();
Fragment fragment3 = new Fragment3();
Frag1.setTabListener(new MyTabsListener(fragment1));
Frag2.setTabListener(new MyTabsListener(fragment2));
Frag3.setTabListener(new MyTabsListener(fragment3));
actionBar.addTab(Frag1);
actionBar.addTab(Frag2);
actionBar.addTab(Frag3);
Here is Fragment1.java code:
package com.example.currencyconverter;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main, container, false);
}
}
Here is my Fragment2.java code:
package com.example.currencyconverter;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
Here is my MyTabsListener.java file:
package com.example.currencyconverter;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Fragment;
import android.app.FragmentTransaction;
public class MyTabsListener implements TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
What is the problem? What am I doing wrong. I have searched Google, but I could not find the solution. Any help is greatly appreciated.

Related

App Crashes While Trasitioning One Fragment To Another

I am trying to jump from one fragment to another fragment. But whenever i click the button to jump on second fragment my app crashes.
Here are my source files. Please help !
I want to Navigate Tab1 to AlertScreen.
Here is Tab1.java
package com.example.bavarian.sos;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class Tab1 extends Fragment {
ImageButton HelpButton ;
private boolean playing = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
HelpButton = (ImageButton) rootView.findViewById(R.id.imageButton);
final MediaPlayer Alarm = MediaPlayer.create(getContext(),R.raw.sound);
//private boolean playing = false ;
HelpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new AlertScreen();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return rootView ;
}
}
AlertScreen.java
package com.example.bavarian.sos;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AlertScreen extends Fragment {
public AlertScreen(){
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.alert_screen,container,false);
return rootView;
}
}
alert_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Alerrt Screen"
android:id="#+id/alerttext"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="176dp" />
</RelativeLayout>
What i am missing here ???

Moving from one fragment to another in a custom listview in android

I have created a navigation drawer for my app from androidhive.info. In that navigation drawer, in one fragment, I have created a custom listview which shows the elements with a name and image. Now upon clicking an item of the custom listview, I need to show the details of the clicked list element. For that I have created another fragment. Now upon clicking the listview element, I should move onto the details of the selected element. The code which I have used for moving from one fragment to another is this:
Fragment fragment=new ImportantNumbersFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
But the getSupportFragmentManager() is showing an error in eclipse like this: The method getSupportFragmentManager() is undefined for the type PagesFragment. This is the code of my custom listview fragment.
PagesFragment.class
public class PagesFragment extends ListFragment implements OnItemClickListener{
Typeface tf;
ListView lv;
List<SimpleRow>rowItems;
public PagesFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
Helper help=new Helper(getActivity().getApplicationContext());
String choice1="സര്‍ക്കാര്‍ സ്ഥാപനങ്ങള്‍";
String choice1new=help.changemalayalam(new Data(choice1));
String choice2="പത്രം";
String choice2new=help.changemalayalam(new Data(choice2));
String choice3="ന്യൂസ് ചാനല്‍";
String choice3new=help.changemalayalam(new Data(choice3));
String choice4="യൂണിവേഴ്സിറ്റികള്‍";
String choice4new=help.changemalayalam(new Data(choice4));
String choice5="പഞ്ചായത്ത് ഓഫീസ്";
String choice5new=help.changemalayalam(new Data(choice5));
String choice6="ഇന്‍ഷുറന്‍സ്";
String choice6new=help.changemalayalam(new Data(choice6));
String choice7="പോലീസ് സ്റ്റേഷന്‍";
String choice7new=help.changemalayalam(new Data(choice7));
String choice8="ഫയര്‍ സ്റ്റേഷന്‍";
String choice8new=help.changemalayalam(new Data(choice8));
ArrayList<String>choice=new ArrayList<String>();
choice.add(choice1new);
choice.add(choice2new);
choice.add(choice3new);
choice.add(choice4new);
choice.add(choice5new);
choice.add(choice6new);
choice.add(choice7new);
choice.add(choice8new);
lv=(ListView)rootView.findViewById(R.id.list1);
rowItems=new ArrayList<SimpleRow>();
for (int i = 0; i < choice.size(); i++) {
SimpleRow item = new SimpleRow(choice.get(i));
rowItems.add(item);
CustomSimpleListAdapter adapter = new CustomSimpleListAdapter(getActivity().getApplicationContext(),R.layout.list_single, rowItems);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
return rootView;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity().getApplicationContext(), "You clicked on position "+position,Toast.LENGTH_LONG).show();
//Here upon clicking the list element, I need to go to ImportantNubersFragment
Fragment fragment=new ImportantNumbersFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getActivity().getApplicationContext(), "You clicked on position "+arg2,Toast.LENGTH_LONG).show();
}
}
ImportantNumbersFragment.class
public class ImportantNumbersFragment extends Fragment {
public ImportantNumbersFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_container, container, false);
return rootView;
}
}
fragment_important_numbers.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout android:name="fragments.YourInitialFragment"
android:id="#+id/fragment_container"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dip" />
</LinearLayout>
I want to move from PagesFragment to ImportantNumbersFragment upon clicking the list element in the custom listview. So in the OnListItemClick, I have given the code for moving from fragment to another. I have to do this in the sameway as that of moving from one activity to another activity using intents like:
Intent i=new Intent(First.this,Second.class);
startActivity(i);.
Since I'm new to fragments, I don't know how this can be achieved in fragments. That's why I have asked here. Can someone please point out the errors in this code. Thanks in advance..
Are you using Support package for fragment in all extended Fragment
Classes, Check !
.
I myself have used the code from AndroidHive and it works perfectly
.
There it does not uses Support package for extended fragment classes
.
Somewhere in code you are using a Support import, Make sure you
maintain uniformity among imports
.
Programatically Speaking for ex::
If you are using support package use codes like these
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
If you are not using support package use codes imports ::
import android.app.ActionBarDrawerToggle;
import android.app.Fragment;
import android.app.FragmentActivity;
import android.app.FragmentTransaction;
import android.widget.DrawerLayout;
{EDIT - Sample on how one of the way to perform fragment transaction}
MainActivity.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
public class MainActivity extends FragmentActivity {
Fragment fragment;
String className;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("MainActivity", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Store the name of the class
className=MainActivity.class.getSimpleName();
//First fragment should be mounted on oncreate of main activity
if (savedInstanceState == null) {
/*fragment=FragmentOne.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).addToBackStack(className).commit();
*/
Fragment newFragment = FragmentOne.newInstance();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, newFragment).addToBackStack(null).commit();
Log.d("FRAGMENT-A", "fragment added to backstack");
}
}
}
FragmentOne.java
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Spinner;
import com.sample.screennavigation.UtilRangeSeekBar.OnRangeSeekBarChangeListener;
public class FragmentOne extends Fragment{
//Declare variables that hold the data for configuration change
public static FragmentOne newInstance(){
Log.d("FragmentOne", "newInstance");
FragmentOne fragment = new FragmentOne();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("FragmentOne", "onCreateView");
View view=inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
}
{EDIT-2}
PagesFragment ... is using a support package somewhere, check
it !
Check imports of pagefragment
But the getSupportFragmentManager() is showing an error in eclipse like this: The method getSupportFragmentManager() is undefined for the type PagesFragment.
Error in your code clearly says you are using a supportpackage import
and it is conflicting the execution
Rememmber you cannot mix the imports
Hope it helps !, let me know if you need any additional info

Use Actionbarsherlock in Program

i use "Actionbarsherlock" in my project
First i import "Actionbarsherlock" in my project then delete folder "libs"
But my question is how can I change the code below؟
mainactivity
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
#SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "water"," time" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
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));
}
/**
* 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) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}}
MoviesFragment
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MoviesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies,container,false);
return rootView;
}
}
GamesFragment
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class GamesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container,false);
return rootView;
}}
TabsPagerAdapte
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.GamesFragment;
import info.androidhive.tabsswipe.MoviesFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Games fragment activity
return new GamesFragment();
case 1:
// Movies fragment activity
return new MoviesFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}}
Please help me to solve this problem
bro remove your email adress from the question.Now coming to your question your actionbar should be an object of supportActionbar.Change the getActionBar() to getSupportActionBar()l.And secondly,your main activity should extend sherlockActivity.Lastly,change the importation from the android actionbar to the sherlock actionbar

Different when write function in onCreateView and method in class extends from Fragment

In Main class, I declare a StartActivity type variable.StartActivity fragment1 = new StartActivity();
Here is StartActivity class. I call drawTab() method on onCreateView. And It runs OK.
package com.example.android.navigationdrawerexample;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class StartActivity extends Fragment {
public static Context appContext;
ActionBar actionbar;
int state = 0;
/** Called when the activity is first created. */
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
drawTab();
return rootView;
}
// call draw Tab method
public void drawTab() {
// ActionBar
actionbar = getActivity().getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");
Fragment PlayerFragment = new AFragment();
Fragment StationsFragment = new BFragment();
PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
StationsTab.setTabListener(new MyTabsListener(StationsFragment));
actionbar.addTab(PlayerTab);
actionbar.addTab(StationsTab);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
}
return false;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActivity().getActionBar()
.getSelectedNavigationIndex());
}
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
However, it don't need so. I want when I declare a StartActivity type variable StartActivity fragment1 = new StartActivity();
it doesn't call drawTab() method. It only called, when I call:
fragment1.drawTab(). And I tried remove drawTab() method on onCreateView
And in Main class I have:
StartActivity fragment1 = new StartActivity();
fragment1.drawTab();
However ERROR happens. I dont know reason. What is different? I think error happens when I call: actionbar = getActivity().getActionBar(); on drawTab() not onCreateView
help me solve problem? Thanks you!

Fixed Tabs in the same view

I'm new in Android. I wanna create an application that shows an action bar with fixed tabs. I've reached to do this.
The problem is that the tabs don`t fit in the same view.
This is my Main Activity:
package com.pestana.pestana;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.app.ActionBar;
import android.app.Fragment;
public class MainActivity extends Activity {
// Declare Tab Variable
ActionBar.Tab Tab1, Tab2, Tab3,Tab4;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
Fragment fragmentTab3 = new FragmentTab3();
Fragment fragmentTab4 = new FragmentTab4();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// Hide Actionbar Icon
//actionBar.setDisplayShowHomeEnabled(false);
// Hide Actionbar Title
//actionBar.setDisplayShowTitleEnabled(false);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setIcon(R.drawable.tab1);
Tab2 = actionBar.newTab().setText("Tab2");
Tab3 = actionBar.newTab().setText("Tab3");
Tab4 = actionBar.newTab().setText("Tab4");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
Tab4.setTabListener(new TabListener(fragmentTab4));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
actionBar.addTab(Tab4);
}
#Override public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true; /** true -> el menú ya está visible */
}
}
And this is the layout
`
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
`
I've created also 4 classes for FragmentTab1... FragmentTab4,
FragmentTab1
package com.pestana.pestana;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
public class FragmentTab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab1, container, false);
return rootView;
}
}
FragmentTab2
package com.pestana.pestana;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
public class FragmentTab2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab2, container, false);
return rootView;
}
}
FragmentTab3
package com.pestana.pestana;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
public class FragmentTab3 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab3, container, false);
return rootView;
}
}
FragmentTab4
package com.pestana.pestana;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
public class FragmentTab4 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab4, container, false);
return rootView;
}
}
TabListener.java
package com.pestana.pestana;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
public class TabListener implements ActionBar.TabListener {
Fragment fragment;
public TabListener(Fragment fragment) {
// TODO Auto-generated constructor stub
this.fragment = fragment;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(fragment);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
4 XML layout for each fragment this is an example, just have to change Fragment1 for each XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/Fragment1" />
</RelativeLayout>
I am stuck for 3 days, any help please?? With the same code can I implement a swipe view with the different tabs?? Thanks in advance
Check the documentation (Creating Swipe Views with Tabs), you can download a working example of how to do exactly what you want.

Categories

Resources