I have got a problem, My app crashes when i try to use the set Current Item(arg0.get Position()) in on Tab selected method while implement a the functionality that i click on tabs the fragments below should also change with it..... it works the other way around that is when i swipe, Tabs changes with it. Any kind of help will be appreciable.. thanks in advance. the code is given below:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class AboutUs extends FragmentActivity implements TabListener {
private ActionBar actionBar;
ViewPager viewPager;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
intialization();
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void intialization() {
// TODO Auto-generated method stub
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("MUMTAZ ANWAR");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("CREDIENTIALS");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
viewPager = (ViewPager) findViewById(R.id.pagger);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
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
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Log.d("HAMZA", "onTabReselected at " + "position" + tab.getPosition()
// + "name" + tab.getText());
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Log.d("HAMZA", "onTabselected at " + "position" + tab.getPosition()
// + "name" + tab.getText());
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Log.d("HAMZA", "onTabUnselected at " + "position" + tab.getPosition()
// + "name" + tab.getText());
}
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragment = null;
if (arg0 == 0) {
fragment = new BlankFragment();
}
if (arg0 == 1) {
fragment = new BlankFragmenta();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
I can't find fragment transaction in your code
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment); // pass your fragment as second parameter
transaction.addToBackStack(null);// it is optional if you want to put it in stack
// Commit the transaction
transaction.commit();
how you replace fragment . it is missing
Related
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
In my android app I am using a tab swipe view having 4 tabs first tab displays data on the UI and is calling a WS via AsyncTask, data returned by the WS is populating a pojo and this pojo is passed to other tab(fragments)
The problem I face is Tabs are not executed in a sequence when I click on first tab, first and 2 tab are executing before calling WS due to this strange behaviour I cant get data in my second tab
Activity:
public class TabSwipeActivity extends FragmentActivity implements ActionBar.TabListener, DetailsFoeFragment.ExistingFeatureDataInt
{
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
ExistingData existingData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabswipe);
ArrayList<String> detailList =this.getIntent().getStringArrayListExtra("FeatureData");
viewpager = (ViewPager) findViewById(R.id.pager1);
ft = new FragmentPageAdapter(getSupportFragmentManager());
if(detailList!=null)
ft.setDetailList(detailList);
// ft.setExistingData(getExistingData());
actionbar = getActionBar();
viewpager.setAdapter(ft);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
actionbar.addTab(actionbar.newTab().setText("Details").setTabListener(this),0,true);
actionbar.addTab(actionbar.newTab().setText("Action Taken").setTabListener(this),1,false);
actionbar.addTab(actionbar.newTab().setText("Before Photos").setTabListener(this),2,false);
actionbar.addTab(actionbar.newTab().setText("After Photos").setTabListener(this),3,false);
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageSelected(int arg0) {
actionbar.setSelectedNavigationItem(arg0);
}
});
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewpager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
////
Adapter:
/////
public class FragmentPageAdapter extends FragmentPagerAdapter {
Bundle bundle= new Bundle();
ExistingData existingData = new ExistingData();;
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
Fragment fragment;
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
fragment = new DetailsFoeFragment();
bundle.putStringArrayList("details",detailList);
fragment.setArguments(this.bundle);
return fragment;
case 1:
fragment= new ActionFoeFragment();
bundle.putParcelableArrayList("actions",(ArrayList)existingData.getActionList());
// bundle.putParcelableArrayList("actions",(ArrayList)existingData.getActionList());
// bundle.putString("featureId",detailList.get(2));
fragment.setArguments(this.bundle);
return fragment;
case 2:
fragment = new PhotoBeforeFoeFragment();
// bundle.putStringArrayList("beforeImage",null);
bundle.putStringArrayList("beforeImage",(ArrayList<String>)existingData.getBeforeImagePath());
fragment.setArguments(this.bundle);
return fragment;
case 3:
fragment = new PhotoAfterFoeFragment();
bundle.putStringArrayList("afterImage",(ArrayList<String>)existingData.getAfterImagePath());
fragment.setArguments(this.bundle);
return fragment;
default:
break;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
ArrayList<String> detailList;
public void setDetailList(ArrayList<String> detailList) {
this.detailList = detailList;
}
public void setExistingData(ExistingData existingData) {
this.existingData = existingData;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Please provide inputs
thanks
One solution:
In your activity, run WS with Asynctask, and then call the fragments that need data to update view
class yourActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
... ...
//set up your tabs with fragments
... ...
//WS: Your asynctask requesting data from server
(new AsyncTask<... ...>() {
... ...
#Override
protected void onPostExecute(dataSet) {
//here you have got your data set from server then inform tabs to update
YourFragment frag = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.your_fragment);
frag.updateView(dataSet);
}
}).execute();
}
}
In each tab/fragment that needs shared data set, define a method to update view with the data set
class YourFragment extebds Fragment{
... ...
public void updateView(dataSet)
{
//update this fragment view with dataSet
//and stop your waiting dialog
}
}
Hope this solution works for you.
I want to hide title bar from swipe tab fragment activity , but when i write no title bar theme in manifest and run the app it have errors and don`t do anything , what should i do ? here is code:
public class Fragment1 extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionbar;
ViewPager viewpager;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.fragment);
viewpager= (ViewPager) findViewById(R.id.pager);
viewpager.setAdapter(new Myadapter(getSupportFragmentManager()));
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
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.Tab tab1=actionbar.newTab();
tab1.setText("All Stuffs");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionbar.newTab();
tab2.setText("Top Stuffs");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionbar.newTab();
tab3.setText("New Stuffs");
tab3.setTabListener(this);
actionbar.addTab(tab1);
actionbar.addTab(tab2);
actionbar.addTab(tab3);
}
#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) {
}
}
class Myadapter extends FragmentPagerAdapter
{
public Myadapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public android.support.v4.app.Fragment getItem(int arg0) {
// TODO Auto-generated method stub
android.support.v4.app.Fragment fragment=null;
if(arg0==0)
{
fragment=new FragmentA();
}
if(arg0==1)
{
fragment=new FragmentB();
}
if(arg0==2)
{
fragment=new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
and it is my manifest code:
activity android:name=".FragmentA"
android:theme="#android:style/Theme.NoTitleBar"
In onCreate method try:
getSupportActionBar().hide();
It will hide title bar
First of all please check your manifest:
activity android:name=".FragmentA"
android:theme="#android:style/Theme.NoTitleBar"
Your activity name is Fragment1. So this line is pointless. You cannot give theme to fragment is xml.
Before setcontentview method place this line:
requestWindowFeature(Window.FEATURE_NO_TITLE);
This will hide the title bar.
im trying to make a basic ui in android with tabs. however i keep getting a nullpointer exception whenever i try to run it. the error originates from initializing viewpager. (i dont know what else to type and i keep receiving an error whenever i try to post.)
public class MainActivity extends FragmentActivity implements TabListener {
ActionBar actionBar;
ViewPager viewPager;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) viewPager.findViewById(R.id.tabs);
viewPager.setAdapter(new adapter(getSupportFragmentManager()));
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab stocks = actionBar.newTab();
stocks.setText("Stocks");
stocks.setTabListener(this);
ActionBar.Tab market = actionBar.newTab();
market.setText("Market");
market.setTabListener(this);
ActionBar.Tab portfolio = actionBar.newTab();
portfolio.setText("Portfolio");
portfolio.setTabListener(this);
actionBar.addTab(stocks);
actionBar.addTab(market);
actionBar.addTab(portfolio);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainsearch, menu);
return true;
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
class adapter extends FragmentPagerAdapter {
public adapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
public Fragment getItem(int arg0) {
Fragment fragment = null;
if(arg0==0) {
fragment = new StocksFragment();
}
if (arg0==1){
fragment = new MarketFragment();
}
if (arg0==2){
fragment = new PortfolioFragment();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
}
Of course this line will give a NPE
viewPager=(ViewPager) viewPager.findViewById(R.id.tabs);
you are calling a method on an object that you are trying to initialize (calling findViewById() on null viewPager)
If tabs is in activity_main.xml then just remove viewPager
viewPager=(ViewPager) findViewById(R.id.tabs);
findViewById is a method of the activity, not of the view. So it should be:
viewPager=(ViewPager) this.findViewById(R.id.tabs);
I need to know how to parse data from online xml in the background and show it in listview.
Presently I have a class which is extending FragmentActivity. Below is the layout of the class
<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"/>
I have declared Action bar tabs and view pager in the class like below
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayUseLogoEnabled(true);
viewPager=(ViewPager)findViewById(R.id.pager);
viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
viewPager.setOffscreenPageLimit(6);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
getActionBar().setSelectedNavigationItem(position);
}
#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.Tab TabOne = actionbar.newTab().setText("TabOne ");
ActionBar.Tab TabTwo = actionbar.newTab().setText("TabTwo ");
ActionBar.Tab TabThree = actionbar.newTab().setText("TabThree ");
ActionBar.Tab TabFour = actionbar.newTab().setText("TabFour ");
ActionBar.Tab TabFive = actionbar.newTab().setText("TabFive ");
ActionBar.Tab TabSix = actionbar.newTab().setText("TabSix ");
TabOneActivity one = new TabOneActivity ();
TabTwoActivity two= new TabTwoActivity ();
TabThreeActivity three= new TabThreeActivity ();
TabFourActivity four= new TabFourActivity ();
TabOFiveActivity five= new TabOFiveActivity ();
TabSixActivity six=new TabSixActivity ();
TabOne .setTabListener(new MyTabsListener(one));
TabTwo .setTabListener(new MyTabsListener(two));
TabThree .setTabListener(new MyTabsListener(three));
TabFour .setTabListener(new MyTabsListener(four));
TabFive .setTabListener(new MyTabsListener(five));
TabSix .setTabListener(new MyTabsListener(six));
actionbar.addTab(TabOne);
actionbar.addTab(TabTwo);
actionbar.addTab(TabThree);
actionbar.addTab(TabFour);
actionbar.addTab(TabFive);
actionbar.addTab(TabSix);
There are two inner classes MyTabListener and ViewPageAdapter . Below is the MyTabListener class
public class MyTabsListener implements ActionBar.TabListener {
protected TabOneActivity oneContext;
protected TabTwoActivity twoContext;
protected TabThreeActivity threeContext;
protected TabFourActivity fourContext;
protected TabFiveActivity fiveContext;
protected TabSixActivity sixcontext;
public MyTabsListener(TabOneActivity onetab) {
// TODO Auto-generated constructor stub
this.oneContext=onetab;
}
public MyTabsListener(TabTwoActivity twoTab) {
// TODO Auto-generated constructor stub
this.twoContext=twoTab;
}
public MyTabsListener(TabThreeActivity threeTab) {
// TODO Auto-generated constructor stub
this.threeContext=threeTab;
}
public MyTabsListener(TabFourActivity fourTab) {
this.fourContext= fourTab;
}
public MyTabsListener(TabFiveActivity fiveTab) {
// TODO Auto-generated constructor stub
this.fiveContext=fiveTab;
}
public MyTabsListener(TabSixActivity sixTab) {
// TODO Auto-generated constructor stub
this.sixContext=sixTab;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
Below is the ViewPagerAdapter class
public class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 6;
public ViewPagerAdapter(FragmentManager supportFragmentManager) {
// TODO Auto-generated constructor stub
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
switch(position){
case 0:
return TabOneActivity.create(position);
case 1:
return TabTwoActivity.create(position);
case 2:
return TabThreeActivity.create(position);
case 3:
return TabFourActivity.create(position);
case 4:
return TabFiveActivity.create(position);
case 5:
return TabSixActivity.create(position);
default :
return TabOneActivity.create(position);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
Each Activity is extending fragment and hitting and parsing online xml using AsyncTask and showing the result in a listview.
Before data gets populated to the listview I am showing a progressbar.
What I need to do is to show a progress bar on the splash screen which in the background should hit and parses the online data and one post execute show the data in a listview.
This will make UI look good, since user don't need to wait for data to get load in the main screen instead it is getting downlaoded in the spalsh screen.
Kindly suggest how to achieve this. Since I am using a FragmentActivity which has six different fragments hitting six different URLs