Android sliding tabs, Fragment_Collection_Object Error - android

am following the Android Developers website to add sliding tabs to my Android app for my project, I am stuck with this one error and do not understand the problem, I know it has something to do with calling the correct Fragment layout, I am doing exactly what the tutorial tells me too, have any of you guys got any information to solve this, thank you.
package com.test.finalproject;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements View.OnClickListener {
DemoCollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;
Button buttonLogin;
Button buttonRegister;
Button buttonTheme;
Button buttonMaps;
#Override
protected void onCreate(Bundle savedInstanceState) {
Utils.setThemeToActivity(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.app.ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// show the given tab
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// probably ignore this event
}
};
// Add 3 tabs, specifying the tab's text and TabListener
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
buttonLogin = (Button)findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(this);
buttonRegister = (Button)findViewById(R.id.buttonRegister);
buttonRegister.setOnClickListener(this);
buttonTheme = (Button)findViewById(R.id.buttonTheme);
buttonTheme.setOnClickListener(this);
buttonMaps = (Button)findViewById(R.id.buttonMaps);
buttonMaps.setOnClickListener(this);
mCollectionPagerAdapter =
new DemoCollectionPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
}
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
// Our object is just an integer :-P
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 100;
}
#Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// The last two arguments ensure LayoutParams are inflated
// properly.
View rootView = inflater.inflate(
R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
Integer.toString(args.getInt(ARG_OBJECT)));
return rootView;
}
}
public void onCreate1(Bundle savedInstanceState) {
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
getActionBar().setSelectedNavigationItem(position);
}
});
}
private void buttonLoginClick()
{
startActivity (new Intent("MainApp"));
}
private void buttonRegisterClick()
{
startActivity (new Intent("TextToo"));
}
private void buttonThemeClick()
{
startActivity (new Intent("SettingsTheme"));
}
private void buttonMapsClick()
{
startActivity (new Intent("GoogleMaps"));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.buttonLogin:
buttonLoginClick();
break;
}
switch (v.getId())
{
case R.id.buttonRegister:
buttonRegisterClick();
break;
}
switch (v.getId())
{
case R.id.buttonTheme:
buttonThemeClick();
break;
}
switch (v.getId())
{
case R.id.buttonMaps:
buttonMapsClick();
break;
}
}
}
The Error is on line 126.
The Error is: Description Resource Path Location Type
fragment_collection_object cannot be resolved or is not a field MainActivity.java /Login/src/com/test/finalproject line 126 Java Problem

The problem you are facing is because of the missing xml file in the layout folder of project/res
Go to http://developer.android.com/training/implementing-navigation/lateral.html
Click on "download the sample app" and unzip it. Add the EffectiveNavigation\res\layout\fragment_collection_object.xml to you project\res\layout. This,I guess will solve the problem.

Related

How to launch activity based on tab bar item?

I want to start different activities depending on the click of the tab bar item but I get a blank page as started activity in the "windows class". What I intended to do is start different activities on click of tab bar item.
Tab Bar Activity Class
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
public class TabBar extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabbardefault);
try {
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager) findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
catch (Exception e)
{
}
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
#Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Home").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Celeb").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Videos").setTabListener(tabListener));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_tab_bar, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
TabPagerAdapter
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
//Fragement for Android Tab
return new Android();
case 1:
//Fragment for Ios Tab
return new Ios();
case 2:
//Fragment for Windows Tab
return new Windows();
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3; //No of Tabs
}
}
Windows
public class Windows extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View windows = inflater.inflate(R.layout.feed_main, container, false);
//((TextView)windows.findViewById(R.id.textView)).setText("Windows");
return windows;
}}
To answer you, you are not actually starting an Activity you a switching through Fragments, Fragment is just a Custom View, and you inflate an xml to represent the Custom View which is the Fragment, if you want to start an Activity after your Tab is selected then you call startActivity(Intent); startActivity(new Intent(classname.this,the_class_i_want_to_start.class));
and your Windows class extends Fragment it should be Activity if what you want is the functionality of Fragment then you aint got a problem
hope its lucid

How to add swiping gesture on fragment TAbHost

I want to change tab by swiping on the screen i am creating tabs using fragment tabhost but when i implement swiping feature in my project it doesn't open nested fragment in each tab means i am opening nested child fragment inside each tab.
Please tell me its very important for me.
here is my code.
this my Main activity.
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
public class Home extends FragmentActivity implements OnTabChangeListener,OnPageChangeListener {
private static final String TAB_1_TAG = "tab_1";
private static final String TAB_2_TAG = "tab_2";
private static final String TAB_3_TAG = "tab_3";
private static final String TAB_4_TAG = "tab_4";
private static final String TAB_5_TAG = "tab_5";
private FragmentTabHost mTabHost;
// ViewPager viewPager;
// TabsPagerAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
viewPager =(ViewPager)findViewById(R.id.viewpager);
initView();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
viewPager.setOnPageChangeListener(Home.this);
}
private void initView() {
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
// mTabHost.addTab(mTabHost.newTabSpec(TAB_1_TAG).setIndicator("Talk", getResources().getDrawable(R.drawable.ic_launcher)), TalkContainerFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_1_TAG).setIndicator("Talk"), TalkContainerFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_2_TAG).setIndicator("Map"), Map_fragment.class, null);
// mTabHost.addTab(mTabHost.newTabSpec(TAB_3_TAG).setIndicator("Go"), GoContainerFragment.class, null);
// mTabHost.addTab(mTabHost.newTabSpec(TAB_4_TAG).setIndicator("Watch"), WatchContainerFragment.class, null);
// mTabHost.addTab(mTabHost.newTabSpec(TAB_5_TAG).setIndicator("More"), MoreContainerFragment.class, null);
/* Increase tab height programatically
* tabs.getTabWidget().getChildAt(1).getLayoutParams().height = 150;
*/
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
final TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
if (tv == null)
continue;
else
tv.setTextSize(10);
}
}
#Override
public void onBackPressed() {
boolean isPopFragment = false;
String currentTabTag = mTabHost.getCurrentTabTag();
if (currentTabTag.equals(TAB_1_TAG)) {
isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(TAB_1_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_2_TAG)) {
isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(TAB_2_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_3_TAG)) {
isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(TAB_3_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_4_TAG)) {
isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(TAB_4_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_5_TAG)) {
isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(TAB_5_TAG)).popFragment();
}
if (!isPopFragment) {
finish();
}
}
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
int pos = this.mTabHost.getCurrentTab();
this.viewPager.setCurrentItem(pos);
}
#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
int pos = this.viewPager.getCurrentItem();
this.mTabHost.setCurrentTab(pos);
}
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
}
This Container fragment class
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
public class BaseContainerFragment extends Fragment {
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
Created Container for each TAb
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TalkContainerFragment extends BaseContainerFragment {
private boolean mIsViewInited;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("test", "tab 1 oncreateview");
return inflater.inflate(R.layout.container_fragment, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("test", "tab 1 container on activity created");
if (!mIsViewInited) {
mIsViewInited = true;
initView();
}
}
TAlk.java file
import java.util.HashSet;
import java.util.Set;
import org.json.JSONObject;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class Talk extends Fragment {
/** Define global variables over here */
//private ProgressDialog pDialog;
// StaticApiList sal;
// TalkModelAll tma;
JSONObject myJasonObject = null;
private ListView lv;
// private ArrayList<TalkModelAll> m_ArrayList = null;
//ArrayList<String> stringArrayList = new ArrayList<String>();
// TalkArrayAdapter taa;
Set<String> uniqueValues = new HashSet<String>();
TextView rowTextView = null;
boolean vivek = false;
int postid;
String title;
String thumsrc;
String largeimg;
String excert;
String description;
String cat;
String myUrl;
String jsonString;
int mCurCheckPosition;
String check_state = null;
String ccc;
LinearLayout myLinearLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.talk, null);
Button btn = (Button) rootView.findViewById(R.id.your_btn_id);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Here TalkDetail is name of class that needs to open
TalkDetail talkfragment = new TalkDetail();
// if U need to pass some data
Bundle bundle = new Bundle();
//
// bundle.putString("title", m_ArrayList.get(arg2).title);
// bundle.putString("largeimg", m_ArrayList.get(arg2).largeimg);
// bundle.putString("excert", m_ArrayList.get(arg2).excert);
// bundle.putString("description", m_ArrayList.get(arg2).description);
// bundle.putString("cat", m_ArrayList.get(arg2).cat);
// //bundle.putInt("postid", m_ArrayList.get(arg2).postid);
talkfragment.setArguments(bundle);
((BaseContainerFragment)getParentFragment()).replaceFragment(talkfragment, true);
}
});
return rootView;
}
}
Talkddetail.class
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class TalkDetail extends android.support.v4.app.Fragment {
/* (non-Javadoc)
* #see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=LayoutInflater.from(getActivity()).inflate(R.layout.activity_talk_detail, null);
return view;
}
}
You have to use a ViewPager. Read more at Android Developer website. You can find a lot of custom view pagers on Android Arsenal. This is the current standard, so you should use this over TabHost.
you can have a look at its demo in eclips go to
file->other->android Activity->tabbed activity
or learn the view Fliper from android developer website
package com.example.mystack;
import java.util.Locale;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements
ActionBar.TabListener {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a {#link FragmentPagerAdapter}
* derivative, which will keep every loaded fragment in memory. If this
* becomes too memory intensive, it may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return new TalkContainerFragment(0, "Talk");
case 1: // Fragment # 0 - This will show FirstFragment different title
return new Map_fragment(1, "map");
//Rest of them add here
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
and xml is
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mystack.MainActivity" />
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.mystack.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Use this method in your activity for all fragment trancation
// For the Fragment Replace And AddtobackStack
void replaceFragment(Fragment fragment) {
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
FragmentManager manager = this.getSupportFragmentManager();
boolean fragmentPopped = manager
.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) {
// fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.container, fragment, fragmentTag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
}
}
and use it like this whenever you need to go for nested fragment from one fragmnet
if (DetailsFragment.this.getActivity() != null
&& DetailsFragment.this.getActivity() instanceof HomeActivity)
((HomeActivity) DetailsFragment.this.getActivity())
.replaceFragment(cartfragment);
in the above suppose i am in details fragment and from here i want to go to cartfragment
then get the activitiy instance and cast it like the above

How to call change text size in all fragment of viewpager?

I want to change text in fragment of viewpager.
this is my FragmentActivity Code :
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
public class ScreenSlideActivity extends FragmentActivity {
private static final int NUM_PAGES = 1000;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.activity_screen_slide, menu);
menu.findItem(R.id.action_previous).setEnabled(
mPager.getCurrentItem() > 0);
MenuItem item = menu
.add(Menu.NONE,
R.id.action_next,
Menu.NONE,
(mPager.getCurrentItem() == mPagerAdapter.getCount() - 1) ? R.string.action_finish
: R.string.action_next);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
return true;
case R.id.action_previous:
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
return true;
case R.id.action_next:
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
return true;
case R.id.action_larger_text_size:
//call method in fragment setTextSizeLarger()
return true;
case R.id.action_smaller_text_size:
//call method in fragment setTextSizeSmaller()
return true;
}
return super.onOptionsItemSelected(item);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
and this my fragment :
public class ScreenSlidePageFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
private TextView tvText1;
public static ScreenSlidePageFragment create(int pageNumber) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlidePageFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_screen_slide_page, container, false);
tvText1 = (TextView) rootView.findViewById(android.R.id.text1);
tvText1.setText(
getString(R.string.title_template_step, mPageNumber + 1));
return rootView;
}
public void setTextSizeLarger()
{
tvText1.setTextSize((float)20);
}
public void setTextSizeSmaller()
{
tvText1.setTextSize((float)19);
}
public int getPageNumber() {
return mPageNumber;
}
}
what i want to achieve is the menu item larger text and smaller text can works.
if it click will setTextSize all TextView in all fragment.
and how to do it?
you can do it by using LocalBroadcastManager
in your fragments register receiver and when the menu item is clicked and the user select for example large icon send broadcast, something like below:
Intent intent = new Intent(BROADCAST_LARG_TEXT);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
and in your fragment when you receive broadcast see the action of it and change the text size.
BroadcastReceiver onTextSizeChanged = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
// Change the text size here
}
};
in this way you can also send text size.
another solution is making the fragment add items (text size) to your menu and when it changes, setting user text size. In order to do that you must add setHasOptionsMenu(true); to your fragments.
Update:
because you have not registered your receiver, you must register it at the onCreateView or onCreate and unregistere it at the onDestroy. this is the register part:
IntentFilter filter = new IntentFilter(BROADCAST_LARG_TEXT);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(onTextSizeChanged, filter);
and unregister is:
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(onTextSizeChanged);
So simpily logical steps:
1)when user select item send broadcast by using
Intent intent = new Intent(BROADCAST_LARG_TEXT);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
2) in your fragment declare onTextSizeChanged as a receiver and override onReceive.
3) register and unregister your receiver (onTextSizeChanged) at the mentioned lifecycle.

Android : refresh fragment with dynamic list using ViewPager and ActionbarTab

I'm quite new to Android. I'm stuck with this issue for a few days now.
I'm using the new google's Template of Action bar Tab with swipe (ViewPager).
Inside each fragment, I want to display a dynamic list of cards (with cardslib : https://github.com/gabrielemariotti/cardslib). The data for the cards come from an asyncTask which is fetching data from an url.
This is my problem : When I launch the app, the first View is display, but the ViewPager prepare the view for the second page, so the async task for the second screen is launch too. And the cards of the second screen are added to the first one.
Then if i swipe to the second screen, nothing is displayed, and the async task for the third screen is launch. So if swipe to the thrid screen, nothing... And then if i switch again to the second screen, the cards with data of the first screen are displayed... A real mess !
I think I have a probleme with the onCreate, onCreateView, or something like that. Maybe the code is sometimes not at the right place.. I don't know. I tried a lot of small modofication, but nothing worked. (but it worked like this with just action bar tab and without the ViewPager).
Thanks
Here is my code :
import java.util.Locale;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a {#link FragmentPagerAdapter}
* derivative, which will keep every loaded fragment in memory. If this
* becomes too memory intensive, it may be best to switch to a
* {#link android.support.v13.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mSectionsPagerAdapter.notifyDataSetChanged();
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
//mSectionsPagerAdapter.notifyDataSetChanged();
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
mSectionsPagerAdapter.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
if(tab.getPosition() != mViewPager.getCurrentItem()) {
mViewPager.setCurrentItem(tab.getPosition());
}
// mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mSectionsPagerAdapter.notifyDataSetChanged();
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mSectionsPagerAdapter.notifyDataSetChanged();
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
}
And here is the Fragment
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardArrayAdapter;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.view.CardListView;
import java.util.ArrayList;
import java.util.List;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.example.webanajones.asyncTask.GetItemTask;
import com.example.webanajones.asyncTask.IResultsListener;
import com.example.webanajones.asyncTaskResponse.ItemJsonResponse;
import com.example.webanajones.asyncTaskResponse.ItemJsonResponse.Item;
/**
* A placeholder fragment containing a simple view.
*/
public class PlaceholderFragment extends Fragment implements
IResultsListener {
/**
* The fragment argument representing the section number for this
* fragment.
*/
static final String ARG_SECTION_NUMBER = "section_number";
public static int limit = 0;
IResultsListener listener = this;
ArrayList<Card> cards = new ArrayList<Card>();
CardArrayAdapter mCardArrayAdapter;
int flag = 0;
boolean loadMore = true;
CardListView listView;
private int idx;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Bundle data = getArguments();
// idx = data.getInt("idx") +1;
limit = 0;
flag = 0;
loadMore = true;
List<String> params = new ArrayList();
params.add(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
params.add(String.valueOf(limit));
GetItemTask getItemTask = new GetItemTask();
getItemTask.setOnResultsListener(listener);
getItemTask.execute(params);
}
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#SuppressWarnings("unchecked")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
#Override
public void onGetItemTaskSucceeded(ItemJsonResponse itemJsonResponse) {
if (mCardArrayAdapter != null) {
mCardArrayAdapter.notifyDataSetChanged();
}
if (itemJsonResponse.getSuccess() != 0) {
loadMore = true;
final Item[] itemTable = itemJsonResponse.getItemArray();
for (int itemIndex = 0; itemIndex < itemTable.length; itemIndex++) {
if (itemTable[itemIndex].getTypeId() == 2) { // 2 = type GIF
final int index = itemIndex;
Card gifCard = new CardGif(getActivity()){
#Override
public void setupInnerViewElements(ViewGroup parent, View view) {
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
int width = (int) (metrics.widthPixels * 0.97);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.WRAP_CONTENT);
params.bottomMargin = 30;
LinearLayout LinearLayout = (LinearLayout) parent.findViewById(R.id.inner_frame);
GIFWebView gifView = new GIFWebView (getActivity(), itemTable[index].getObjectUrl());
LinearLayout.addView(gifView);
}
};
CardHeader header = new CardHeader(getActivity());
header.setTitle(itemTable[itemIndex].getTitle());
cards.add(gifCard);
}
if (itemTable[itemIndex].getTypeId() == 1) { // 1 = image
Card card = new Card(getActivity());
CardHeader header = new CardHeader(getActivity());
header.setTitle(itemTable[itemIndex].getTitle());
card.addCardHeader(header);
MyThumbnail thumb = new MyThumbnail(getActivity(), itemTable[itemIndex].getObjectUrl());
thumb.setExternalUsage(true);
card.addCardThumbnail(thumb);
final int index = itemIndex;
card.addPartialOnClickListener(Card.CLICK_LISTENER_THUMBNAIL_VIEW, new Card.OnCardClickListener() {
#Override
public void onClick(Card card, View view) {
Intent intent = new Intent(getActivity(), FullscreenActivity.class);
intent.putExtra("imageUrl", itemTable[index].getObjectUrl());
startActivity(intent);
}
});
cards.add(card);
}
}
if (flag == 0) {
mCardArrayAdapter = new CardArrayAdapter(getActivity(), cards);
listView = (CardListView) getActivity().findViewById(
R.id.carddemo_list);
listView.setAdapter(mCardArrayAdapter);
flag = 1;
}
}
else {
loadMore = false;
}
loadMoreCards(mCardArrayAdapter, loadMore);
}
public void loadMoreCards(CardArrayAdapter mCardArrayAdapter, boolean loadMore) {
if (listView != null) {
mCardArrayAdapter.notifyDataSetChanged();
if (loadMore) {
listView.setOnScrollListener(new EndlessScrollListener() {
#Override
public void onLoadMore(int page, int totalItemsCount) {
limit += 5;
List<String> params = new ArrayList();
params.add("1");
params.add(String.valueOf(limit));
GetItemTask getItemTask = new GetItemTask();
getItemTask.setOnResultsListener(listener);
getItemTask.execute(params);
}
});
}
}
}
//fin placeholderfrag
#Override
public void onGetImageTaskSucceeded(Bitmap bitmap) {
// TODO Auto-generated method stub
}
}
Thanks #GabrieleMariotti, it helped ! In fact I already tried to use different classes for the different fragments, but I forget to give diferent IDs to my cardsLists. It solved the problem. Btw, good job for your lib, it is awesome.
So for the next one : create differents classes for all your differents fragments. They have to use differents layout, and the cardsList have to use differents ID.

Android Swipe tabs Issue: Can't seem to update the UI and the result comes repeated n times

I'm using two classes. One for the background service where i'm doing several of gatherings of information, and the other I decided to use a swipe tab view to manage and organize my information/data. In the background service (like I said) i'm retrieving information (mostly string types) and sending them via intent to the Main Activity, this activity receives it via a broadcastreceiver (this part works fine). Now the problem i'm having is that when i try to distribute the information the the diferent tabs, it comes repeated and doesn't seem to refresh now my most crucial problem is the reapeating information part. Going to place my code and see if any one of you brilliant minds :D can help me. I hope you can.
import java.util.Locale;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ActivityMain extends FragmentActivity implements
ActionBar.TabListener {
int NUM_TAB = 3;
static TextView InfoView1;
static TextView InfoView2;
static TextView InfoView3;
static String infoData1 = "";
static String infoData2 = "";
static String infoData3 = "";
Fragment frag;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
Defining the intent
intent = new Intent(this, BroadcastService.class);
}
Starting the broadcastreceiver to call the Background service
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch (position)
{
case 0:
frag = new Information1();
Bundle arg0 = new Bundle();
arg0.putString(Information1.ARG_SECTION, infoData1);
frag.setArguments(arg0);
break;
case 1:
frag = new Information2();
Bundle arg1 = new Bundle();
arg1.putString(Information2.ARG_SECTION, infoData2);
frag.setArguments(arg1);
break;
case 2:
frag = new Information3();
Bundle arg2 = new Bundle();
arg2.putString(BatteryStatus.ARG_SECTION, infoData3);
frag.Information3(arg2);
break;
default:
frag = null;
break;
}
return frag;
}
#Override
public int getCount() {
// Show 3 total pages.
return NUM_TAB;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
private void updateUI(Intent intent) {
String stuff1 = intent.getStringExtra("stuff");
String stuff2 = intent.getStringExtra("stuff2");
String stuff3 = intent.getStringExtra("stuff3");
String stuff4 = intent.getStringExtra("stuff4");
String stuff5 = intent.getStringExtra("stuff5");
String stuff6 = intent.getStringExtra("stuff6");
so on...
And storing them in a string
infoData1 += ("Stuff1: " + stuff1 + "\n");
infoData1 += ("Stuff2: " + stuff2 + "\n");
infoData2 += ("Stuff3: " + Stuff3 + "\n");
infoData2 += ("Stuff4: " + Stuff4 + "\n");
infoData3 += ("Stuff5: " + Stuff5 + "\n");
infoData3 += ("Stuff6: " + Stuff6 + "\n");
}
public static class Information1 extends Fragment {
public static final String ARG_SECTION = "section_number";
public Information1(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View info1View = inflater.inflate(
R.layout.information1, container, false);
InfoView1 = (TextView) info1View
.findViewById(R.id.info1);
InfoView1.setText(getArguments().getString(ARG_SECTION));
Log.d("InfoView1", infoData1);
return info1View;
}
}
public static class Information2 extends Fragment {
public static final String ARG_SECTION = "section_number";
public Information2(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View info2View = inflater.inflate(
R.layout.Information2, container, false);
InfoView2 = (TextView) info2View
.findViewById(R.id.info2);
InfoView2.setText(getArguments().getString(ARG_SECTION));
Log.d("InfoView2", infoData2);
return info2View;
}
}
public static class Information3 extends Fragment {
public static final String ARG_SECTION = "section_number";
public Information3() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View info3View = inflater.inflate(
R.layout.Information3, container, false);
InfoView3 = (TextView) info3View
.findViewById(R.id.info3);
InfoView3.setText(getArguments().getString(ARG_SECTION));
Log.d("BatteryStatView", infoData3);
return info3View;
}
}
#Override
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter(BroadcastService.BROADCAST_ACTION));
}
#Override
public void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
stopService(intent);
}
}
Any ideas?! =D

Categories

Resources