I have successfully set up the a FragmentActivity with FragementPagerAdapter associated with ViewPager to implement two tabbed application .
One of the Tabs namely "Wave" has a text view and a button . All I want is call textview.setText method via the onClick method of button described by its xml attribute .
I do not know where should I initialize my TextView or Button , how can I get the context of Wave tab and where should I write onclick method-
public class InformationShow extends FragmentActivity {
XMLdata dataObject;
ViewPager viewPager;
PagerAdapter adpt;
Fragment temp;
TextView tv;
Button bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
viewPager=(ViewPager)findViewById(R.id.pager);
adpt = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adpt);
// temp=adpt.fg.findFragmentById((int)adpt.getItemId(1));
tv=(TextView)findViewById(R.id.graphWaveTextView);
bt = (Button)findViewById(R.id.button1);
}
public void changeText(View v){
tv.setText("It worked ");
}
Adapter Class
public class PagerAdapter extends FragmentPagerAdapter {
int count = 2;
CharSequence namme[] = {"Temperature","Wave"};
XMLdata data;
FragmentManager fg;
public PagerAdapter(FragmentManager fragmentManager ){
super(fragmentManager);
this.fg = fragmentManager;
}
Context context;
#Override
public Fragment getItem(int arg0) {
switch (arg0){
case 0:{
TemperatureGraphFrag temp = new TemperatureGraphFrag();
return temp;
}
case 1:{WaveHeightGraphFrag wave = new WaveHeightGraphFrag();
return wave;
}
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
return namme[position];
}
}
Fragments Class
public class TemperatureGraphFrag extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.graph_t, container, false);
return view;
}
}
public class WaveHeightGraphFrag extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.graph_sig_wave_height, container, false);
return view;
}
}
fragment_main XML implemented by FragmentActicity
<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" >
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:textColor="#65C2C9"
android:scrollbarSize="5dp"/>
</android.support.v4.view.ViewPager>
Tab 2 Fragment XML graph_sig_wave_height
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/graphWaveTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_gravity="center"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center"
android:onClick="changeText"/>
</LinearLayout>
Tab 1 fragment layout XML graph_t
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linearTemp"
>
<TextView
android:id="#+id/graphTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_gravity="center"
/>
</LinearLayout>
Add the following method to your WaveHeightGraphFrag class:
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
final TextView t = (TextView) view.findViewById(R.id.graphWaveTextView);
Button b = (Button) view.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
t.setText("It worked ");
}
});
}
This is what you want.
Related
i have a tabbed layout with fragments on each page. on the first page there is a user input box and button that when inputted and clicked gives a toast. i would like to store the string of data in my mysql database and populate the list view on another fragment with data stored in the table from mysql. Was wondering how to go about this exactly?
main activity class
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the viewpager in activity_main.xml
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
// Set the ViewPagerAdapter into ViewPager
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
}
public void btnShout(View v) {
//allows for label to be changed to shouted once button is pressed
EditText txtInput = (EditText) findViewById(R.id.txtInput);
TextView lblShout = (TextView) findViewById(R.id.lblShout);
lblShout.setText("Shouted! ");
//allows for toast to be displayed once button is clicked
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
toast.makeText(MainActivity.this, txtInput.getText() + " Has Been Shouted.", toast.LENGTH_SHORT).show();
}
here is my activity main xml
<?xml version="1.0" encoding="utf-8"?>
<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" >
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:textColor="#000000" />
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</android.support.v4.view.ViewPager>
viewpageradapter class
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
final int PAGE_COUNT = 3;
// Tab Titles
private String tabtitles[] = new String[] {"Home","Shouts","Shouts" };
Context context;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return PAGE_COUNT;
}
public Fragment getItem(int position) {
switch (position) {
// Open Fragment home.java
case 0:
FragmentHome fragmenthome = new FragmentHome();
return fragmenthome;
// Open Fragment shouters.java
case 1:
FragmentShouts fragmentshouts = new FragmentShouts();
return fragmentshouts;
case 2:
FragmentShouts fragmentShouts = new FragmentShouts();
return fragmentShouts;
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
return tabtitles[position];
}
here is my tab with the user input (home fragment class)
public class FragmentHome extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragment home.xml
View view = inflater.inflate(R.layout.fragmenthome, container, false);
return view;
}
here is fragment home xml
<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="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="New Shout"
android:id="#+id/lblShout"
android:layout_marginBottom="134dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtInput"
android:layout_alignTop="#+id/lblShout"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:layout_alignParentEnd="true"
android:hint="Enter New Shout..." />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOUT"
android:id="#+id/button"
android:layout_below="#+id/txtInput"
android:layout_centerHorizontal="true"
android:onClick="btnShout" />
</RelativeLayout>
here is the fragment with the listview (shouts fragment class)
public class FragmentShouts extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragment shouts.xml
View view = inflater.inflate(R.layout.fragmentshouts, container, false);
return view;
}
here is the fragment shouts xml
<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:clickable="true"
android:contextClickable="true">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
this is image while useing keypad
and this image after keypad was hidden
I use EditText but below it there is ListView. After typing text inputs in EditText and then press done or click back to hide keypad. Then the part of ListView behind the keypad disappeared and replaced by white area (cleared), why ?
NOTE: this fragment is under tablayout and viewpager and the fragment which contain the edit text and listview is launched from the main fragment
this is main fragment
public class FriendsFragment extends Fragment {
public FriendsFragment() {
// Required empty public constructor
}
private FragmentActivity myContext;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview=inflater.inflate(R.layout.friends_fragment, container, false);
viewPager = (ViewPager) rootview.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootview.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
if (tab.getPosition() == 1) {
FindFriendsFragment.myfriends_list.invalidate(FindFriendsFragment.myfriends_list.getLeft(), FindFriendsFragment.myfriends_list.getTop(), FindFriendsFragment.myfriends_list.getRight(), FindFriendsFragment.myfriends_list.getBottom());
FindFriendsFragment.adapter.notifyDataSetChanged();
FindFriendsFragment.myfriends_list.clearFocus();
FindFriendsFragment.myfriends_list.postInvalidate();
}
}
});
TextView friends = (TextView) rootview.findViewById(R.id.search);
Typeface Exo_thin = Typeface.createFromAsset(myContext.getAssets(), "fonts/Exo2.0-Thin.otf");
friends.setTypeface(Exo_thin);
return rootview;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new MyFriendsFragment(), "My Friends");
adapter.addFragment(new FindFriendsFragment(), "Find Friends");
adapter.addFragment(new TwoFragment(), "Friend Requests");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
this is xml of main fragment
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/rel"
android:layout_alignParentTop="true"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/search"
android:text="Friends"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:textSize="25sp"
android:textColor="#color/colorVeryDarkBlue"
/>
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
app:tabSelectedTextColor="#color/colorLightGreen"
app:tabTextColor="#color/colorDarkGreen"
app:tabMode="scrollable"
android:background="#color/colorDarkBlue"
app:tabIndicatorColor="#color/colorDarkBlue"
app:tabGravity="center"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
the fragment which contain listview and edit text
public class FindFriendsFragment extends Fragment {
public FindFriendsFragment()
{
// Required empty public constructor
}
ListView myfriends_list;
FindFriendsAdapter adapter;
ArrayList<FindFriends> arraylist ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview= inflater.inflate(R.layout.find_friends_fragment, container, false);
EditText search=(EditText) rootview.findViewById(R.id.search);
Typeface Exo_Regular =
Typeface.createFromAsset(getActivity().getAssets(), "fonts/Exo2.0-
Regular.otf");
search.setTypeface(Exo_Regular);
arraylist = new ArrayList<FindFriends>();
arraylist.add(new FindFriends("mina fared", "hello
guys",1,"sdsdsdsds",true )) ;
adapter = new FindFriendsAdapter(getActivity(), arraylist);
myfriends_list.setAdapter(adapter);
adapter.notifyDataSetChanged();
return rootview;
}
}
And the related xml file is of the fragment which contain listview and edittext:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#ffffff"
>
<LinearLayout
android:layout_height="60dp"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_marginTop="111dp"
android:id="#+id/refl"
android:background="#color/colorLightGrey"
android:layout_alignParentTop="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/search"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:background="#ffffff"
android:singleLine="true"
android:hint="Search"
android:ellipsize="start"
android:imeOptions="actionDone"
android:gravity="center_vertical|center_horizontal"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:textColorHint="#color/colormyhintfindfiernds"
android:textColor="#color/colorDarkBlue"
/>
</LinearLayout>
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_marginTop="1dp"
android:layout_below="#+id/refl"
android:divider="#ffffff"
android:dividerHeight="1.5dp"
/>
</RelativeLayout>
this is the parent fragment which contains all fragments
public class ProfileFragment extends Fragment {
public ProfileFragment()
{
// Required empty public constructor
}
RelativeLayout rl1;
DrawView drawView ;
DrawView drawView2;
TextView myprofile,username,notification_txt,colloection_txt,friends_txt,setting_txt,public_profile_txt;
ImageView public_profile_btn,friends;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootview=inflater.inflate(R.layout.myprofile_fragment, container,
false);
username= (TextView) rootview.findViewById(R.id.user_name_txt);
myprofile= (TextView) rootview.findViewById(R.id.myprofile);
notification_txt= (TextView) rootview.findViewById(R.id.notification_txt);
colloection_txt= (TextView) rootview.findViewById(R.id.colloection_txt);
friends_txt= (TextView) rootview.findViewById(R.id.friends_txt);
setting_txt= (TextView) rootview.findViewById(R.id.setting_txt);
public_profile_txt= (TextView) rootview.findViewById(R.id.public_profile_txt);
Typeface Exo_thin = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Exo2.0-Thin.otf");
Typeface Exo_SemiBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Exo2.0-SemiBold.otf");
myprofile.setTypeface(Exo_thin);
username.setTypeface(Exo_SemiBold);
notification_txt.setTypeface(Exo_SemiBold);
colloection_txt.setTypeface(Exo_SemiBold);
friends_txt.setTypeface(Exo_SemiBold);
setting_txt.setTypeface(Exo_SemiBold);
public_profile_txt.setTypeface(Exo_SemiBold);
ImageView x=(ImageView)rootview.findViewById(R.id.colloection);
ImageView y=(ImageView)rootview.findViewById(R.id.friends);
ImageView x1=(ImageView)rootview.findViewById(R.id.setting);
rl1 =(RelativeLayout)rootview.findViewById(R.id.rel2);
final View fragmentContainer = rootview.findViewById(R.id.container);
friends =(ImageView)rootview.findViewById(R.id.friends);
public_profile_btn=(ImageView)rootview.findViewById(R.id.public_profile);
public_profile_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Fragment newFragment = new MyProfileFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(fragmentContainer.getId(), newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
friends.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Fragment newFragment = new FriendsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(fragmentContainer.getId(), newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return rootview;
}
}
from manifest.xml search for your activity tag like this
<activity
android:name=".YourActivity"
android:label="#string/title_activity"
...>
</activity>
then add android:configChanges="keyboard|keyboardHidden" like that
<activity
android:name=".YourActivity"
android:configChanges="keyboard|keyboardHidden"
android:label="#string/title_activity"
...>
</activity>
EDIT
i found that my answer not completely right you may add
android:windowSoftInputMode="adjustNothing"
sorry about that
I'm working on an Application which contains a PageViewer inside a Layout.
But if i start it, the PageViewer isn't visible. (looks leight layout_height=0)
I never used a PageViewer inside other layouts, so maybe it isn't designed to be part of something?
Layout file (shortened,activity_tricks.xml):
<linearLayout>
<TableRow>
....
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrapt_content">
<android.support.v4.view.ViewPager
android:id="#+id/Activity_Trick_viewPager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="6dip" />
</TableRow>
<TableRow>
.
.
.
Trick_Fragment_layout.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"
android:orientation="vertical" >
<TextView
android:id="#+id/trick_fragment_textView_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Activity:
public class TrickActivity extends FragmentActivity {
private ViewPager mViewPager;
private TrickActivityPageAdapter mPageAdapter;
public static final int FRAMES = 3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tricks);
mPageAdapter= new TrickActivityPageAdapter(getSupportFragmentManager(),frames);
mViewPager=(ViewPager)findViewById(R.id.Activity_Trick_viewPager);
mViewPager.setAdapter(mPageAdapter);
//... do other stuff.. //
}
}
public class TrickActivityPageAdapter extends FragmentPagerAdapter {
private int frames;
public TrickActivityPageAdapter(FragmentManager fm, int frames) {
super(fm);
this.frames = frames;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TrickFragment.init("F 1");
case 1:
return TrickFragment
.init("F 2");
case 2:
return TrickFragment.init("f3");
default:
return TrickFragment.init("default");
}
}
#Override
public int getCount() {
return frames;
}
}
public class TrickFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static Fragment init(String string) {
TrickFragment f = new TrickFragment();
Bundle bdl = new Bundle();
bdl.putString(EXTRA_MESSAGE, string);
f.setArguments(bdl);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.trick_fragment_layout, container, false);
TextView tv = (TextView)v.findViewById(R.id.trick_fragment_textView_1);
tv.setText(message);
return v;
}
I am making use of PagerTabStrip in my android app like the ones used in the new Google play store app. I went through a few tutorials and was succesfull in creating three tabs. They are Information, Electronic Configuration and Facts
Information
Electronic Configuration
Facts
Here is the xml layout
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:textSize="30dp"
android:background="#000000"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#ffffff" />
</android.support.v4.view.ViewPager>
Here is the Java file
public class Tabs extends FragmentActivity
{
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.act2aluminium);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
public SectionsPagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount()
{
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return "Information";
case 1:
return "Electronic Configuration";
case 2:
return "Facts";
}
return null;
}
}
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return textView;
}
}
}
Now, my question is How do I attach layouts or pages to the tabs instead of the tiny 1, 2, 3 textViews?
I've searched a lot but coudn't find a good explanation of how it's done. Please help me out with the code. Thanks in advance!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:textSize="30dp"
android:background="#000000"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#ffffff" />
</android.support.v4.view.ViewPager>
page1.xml
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click here" />
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/button1"
android:text="I am Page one" />
page2.xml
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click here" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/button2"
android:text="I am Page two" />
page3.xml
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click here" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/button3"
android:text="I am Page three" />
MainActivity.java
public class MainActivity extends FragmentActivity
{
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
public SectionsPagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
switch (position) {
case 0:
// Top Rated fragment activity
return new Information();
case 1:
// Games fragment activity
return new ElectonicConfiguration();
case 2:
// Movies fragment activity
return new Fact();
}
return null;
}
#Override
public int getCount()
{
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return "Information";
case 1:
return "Electronic Configuration";
case 2:
return "Facts";
}
return null;
}
}
//Page 1 Fragment
public static class Information extends Fragment {
public Information()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page1, null);
TextView textView = (TextView)view.findViewById(R.id.text1);
Button button=(Button)view.findViewById(R.id.button1);
return view ;
}
}
//Page 2 Fragment
public static class ElectonicConfiguration extends Fragment {
public ElectonicConfiguration()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page2, null);
TextView textView = (TextView)view.findViewById(R.id.text2);
Button button=(Button)view.findViewById(R.id.button2);
return view ;
}
}
//Page 3 Fragment
public static class Fact extends Fragment {
public Fact()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page3, null);
TextView textView = (TextView)view.findViewById(R.id.text3);
Button button=(Button)view.findViewById(R.id.button3);
return view ;
}
}
}
page1.xml, page2.xml,page3.xml are the layout file for the first,second and third page respectively.And There are 3 different fragments declared in the MainActivity.java for the 3 different pages. getItem() of SectionsPagerAdapter class manages all the 3 fragment pages. Make changes in the xml file as per your wish.I think the code is pretty self explanatory.If you have any doubt don't hesitate to ask.
Hope it helps. Cheers!
Halo guys, I followed the android developer steps:http://developer.android.com/training/animation/screen-slide.html
How can I call get the "btnRight" & "btnLeft" in MainJava to create a onClick function?
i have no idea about that, (???).findViewByID ?
btw, is there an another better coding design to create this effect?
i wanna create 3 page that can slide or maybe click to change screen, did i use this viewpager incorrectly? Is it probably add some new view instants of using the pagenumber trick?
please help me a bit, thanks!!
MainJava.java:
public class MainJava extends FragmentActivity {
private static final int NUM_PAGES = 3;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.hide();
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(1);
}
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;
}
}
}
ScreenSlidePageFragment.java:
public class ScreenSlidePageFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
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) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.scroll_view_content, container, false);
//rootView.addView(inflater.inflate(R.layout.scroll_view_content, null));
View pageLeft = (View) rootView.findViewById(R.id.pageLeft);
View pageRight = (View) rootView.findViewById(R.id.pageRight);
View pageMain = (View) rootView.findViewById(R.id.pageMain);
if (mPageNumber==0){
pageLeft.setVisibility(View.VISIBLE);
pageMain.setVisibility(View.GONE);
pageRight.setVisibility(View.GONE);
}else if (mPageNumber==1){
pageLeft.setVisibility(View.GONE);
pageMain.setVisibility(View.VISIBLE);
pageRight.setVisibility(View.GONE);
}else if (mPageNumber==2){
pageLeft.setVisibility(View.GONE);
pageMain.setVisibility(View.GONE);
pageRight.setVisibility(View.VISIBLE);
}
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
scroll_view_content.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Dummy content. -->
<LinearLayout
android:id="#+id/pageLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
style="?android:textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:lineSpacingMultiplier="1.2"
android:text="AAA" />
</LinearLayout>
<LinearLayout
android:id="#+id/pageRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
style="?android:textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:lineSpacingMultiplier="1.2"
android:text="CCC"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/pageMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#123456"
android:orientation="vertical" >
<TextView
android:id="#+id/textB"
style="?android:textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:background="#654321"
android:text="BBB"/>
<LinearLayout
android:layout_below="#+id/textB"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id="#+id/btnLeft"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00ffff"
android:text="A"/>
<Button
android:id="#+id/btnRight"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffff00"
android:text="B"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Thats simple really. You have to use the setCurrentItem(int) of the view pager class. The algorithm would be:
int currPage=1 //Points to second page in the viewpager
if left button clicked //listener for the button
2a. currPage--
2b. mPager.setCurrentItem(currPage) //pager is object of your viewpager class
if right button clicked //listener for the button
3a. currPage++
3b. mPpager.setCurrentItem(currPage)
Check out this link for for info.