I have 3 fragments in my activity, managed by a custom class which extends FragmentPagerAdapter.
My Main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.v4.view.ViewPager
android:id="#+id/tabviewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
My activity :
public class RankingFragmentActivity extends FragmentActivity{
....
List fragments = new Vector();
fragments.add(Fragment.instantiate(this,Tab1Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Tab2Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Tab3Fragment.class.getName()));
//adapter
MyPagerAdapter mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) super.findViewById(R.id.tabviewpager);
pager.setAdapter(this.mPagerAdapter);
}
I can perfectly switch from fragments from letf to right (and right to left).
I 'd like to set buttons as listeners (in order to change displayed fragment) but I don't know how to do.
ex:
click on button1 : Fragment 1 is displayed
click on button2 : Fragment 2 is displayed
click on button3 : Fragment 3 is displayed
Thank you !
The answer is right in the documentation for FragmentPagerAdapter
// Watch for button clicks.
Button button = (Button)findViewById(R.id.goto_first);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
Related
I have two layouts as shown in the below image. In the second layout I have two buttons. When user clicks on the button1 layout 2 should occupy whole screen and layout1 should be invisible.When button 2 is pressed again it should show the normal view of both layouts.
Initial views :
When button 1 is pressed :
When button2 is pressed original view should be displayed again.
You can use Fragments to accomplish this (or not). Use a FrameLayout in order to contain the layout to be "kicked out" when you press Button 1.
To do that, simply obtain the reference to the FrameLayout (give it an id and then reference it in the onCreate() method), and set in the Button1 onClickListener() setVisibility(View.GONE); for the FrameLayout.
That will get rid of the view.
When you press on Button2, re-instate the FrameLayout by setting in the onClickListener() setVisibility(View.VISIBLE);
PS. A FrameLayout is a great "container" for a single Fragment.
Here's the code to do it:
Layout file: (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="com.yourDomain.yourApplicationName.MainActivity">
<FrameLayout
android:id="#+id/layout_1"
android:background="#android:color/holo_purple"
android:layout_width="match_parent"
android:layout_height="150dp">
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity: (MainActivity.java)
package com.yourDomain.yourApplicationName;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button1;
Button button2;
View frameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = findViewById(R.id.layout_1);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
setButtonBehavior();
}
private void setButtonBehavior() {
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
frameLayout.setVisibility(View.GONE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
frameLayout.setVisibility(View.VISIBLE);
}
});
}
}
You can use 2 fragments in one layout and then you can use visibilty property of fragments. Fragment is a partical layout that you can put buttons and other tools inside. Here is an explanation of fragments.
http://www.vogella.com/tutorials/AndroidFragments/article.html
We always have heard using multiple fragments with one activity. Is opposite possible? I am curious about this. Can we use same fragment for multiple activities. Please give ONE EXAMPLE.
How to reuse one Fragment in multiple Activities
The green background with two buttons is a single fragment that is reused among multiple activities.
1. Make your fragment class and layout
MyFragment.java
import android.support.v4.app.Fragment;
public class MyFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myLayout = inflater.inflate(R.layout.my_fragment_layout, container, false);
// add click listeners to the buttons in the fragment
Button buttonOne = myLayout.findViewById(R.id.button_1);
Button buttonTwo = myLayout.findViewById(R.id.button_2);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
return myLayout;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_1:
Toast.makeText(getContext(), "Button One", Toast.LENGTH_SHORT).show();
break;
case R.id.button_2:
Toast.makeText(getContext(), "Button Two", Toast.LENGTH_SHORT).show();
break;
}
}
}
my_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_dark"
android:orientation="vertical">
<Button
android:id="#+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="#+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>
2. Add the fragment to your activities
activity_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToRedActivityButtonClick"
android:text="Go to red activity"/>
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
activity_red.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff3636"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToYellowActivityButtonClick"
android:text="Go to yellow activity"/>
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
activity_yellow.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f9f478"
android:orientation="vertical">
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Notes
For simplicity we added the fragment directly to the xml. You can also load fragments dynamically in code. See the documentation for help with that.
Yes, it is possible to have one fragment with multiple activities.
But you will need to program the layout with java using LayoutParams and embed them in every fragment instance.
On every activity, you need to call this fragment
Create your UI Components in Java, add them to the layout dynamically from Java Class i.e. Your Activities.
I would suggest this approach will not be easy to maintain, if you are not super comfortable with Java exclusively. You will need to forget XML for this approach as nothing will be there in it at all, everything will be done with Java classes only.
generic_error_msg_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/White" >
<TextView
android:id="#+id/error_message_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/font_size_16sp" />
<Button
android:id="#+id/error_button_handler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="left|center_vertical"
android:textSize="#dimen/font_size_16sp"
/>
</RelativeLayout>
GenericErrorFragment.Java
public class GenericErrorFragment extends Fragment{
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View genericView = inflater.inflate(R.layout.generic_error_msg_fragment, null);
TextView errorText = (TextView) genericView.findViewById(R.id.error_message_textview);
errorText.setText("error msg" );
Button errorbutton = (Button) genericView.findViewById(R.id.error_button_handler);
errorbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// your logic launch some other activity
}
});
return genericView;
}
}
you can load this fragment in any activity and can define your custom text for error and button handler
I will not write whole code but I can give you exact example you are looking for
Think of an application in which a person can register either as admin or as user
Now while working on it you make 3 fragments in admin registration activity asking
1.) personal information
2.) academic information
3.) admin details
Now for user registration activity say you make 2 fragments to get the following information
1.) personal information
2.) user details
here you used personal information fragment 2 times for 2 activities
code for this is child's play main thing is the concept
I am making an activity which has a section for comments based on addresses inputed by the user.
The user will add a new comments section by pressing a button. This is a non fixed number of sections, so I have initially added this section as a fragment in the xml.
I have an onClick function that adds another fragment to the linearlayout.
My problem is that the new fragments always add to the top of the linearlayout , i.e. above the existing fragment/fragments (i.e. the new fragment will push all the other fragments down).
How can I add the new fragment so that it displays below the existing fragment?
Code in .java file:
public class SpecialReportAdden extends ActionBarActivity {
int numOfFragments;
LinearLayout addenHolder;
TextView report;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_special_report_adden);
numOfFragments=1;
Button addenSave = (Button)findViewById(R.id.btnAddendumSave);
Button addenAddComment = (Button)findViewById(R.id.btnAddendumAddComment);
addenHolder =(LinearLayout)findViewById(R.id.AddenLinLayHolder);
addenSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//this should save all comments including those added in dynamically produced fragments
//save database
Intent i = new Intent (SpecialReportAdden.this, MenuPage.class);
startActivity(i);
}
});
addenAddComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("number of Fragments at start of OnClick", Integer.toString(numOfFragments));
Fragment newAddenFrag = addNewfragment(numOfFragments);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.AddenLinLayInnerHolder, newAddenFrag);
ft.addToBackStack(null);
ft.commit();
numOfFragments++;
Log.i("number of Fragments updated", Integer.toString(numOfFragments));
}
});
}
public Fragment addNewfragment(int number) {
AddendumLinInputFragment adden = new AddendumLinInputFragment();
TextView tx = (TextView) findViewById(R.id.txtVAddendumFragReportNum);
tx.setText("Report "+number+" :");
tx.setTextColor(Color.BLACK);
TextView address = (TextView)findViewById(R.id.txtVAddendumFragAddress);
address.setText("A new address");
address.setTextColor(Color.BLUE);
return adden;
}
xml file for the .java activity
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.danielt.pestcontrol.SpecialReportAdden"
android:id="#+id/RellayAddenComments">
<TextView
android:id="#+id/txtVAddendumTitle" android:text="Service Report Addendum" android:textStyle="bold" android:layout_centerHorizontal="true" android:elegantTextHeight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>
<TextView
android:id="#+id/txtVAddendumTechName"
android:layout_below="#+id/txtVAddendumTitle"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Technician Name: "
android:textSize="18sp"
/>
<TextView
android:id="#+id/txtVAddendumDate"
android:layout_below="#+id/txtVAddendumTechName"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date: "
android:textSize="18sp"
/>
<ScrollView
android:id="#+id/scrlVAddendum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtVAddendumDate">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/AddenLinLayHolder"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/AddenLinLayInnerHolder"
android:orientation="vertical">
<fragment
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.example.danielt.pestcontrol.AddendumLinInputFragment"
android:id="#+id/addendum1CommentsFragment"
android:layout_below="#+id/txtVAddendum1Date"
android:layout_marginTop="24dp"
tools:layout="#layout/fragment_addendum_lin_input" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add another Addendum Comment"
android:id="#+id/btnAddendumAddComment"
android:layout_below="#+id/scrlVAddendum"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save / Update Comments"
android:id="#+id/btnAddendumSave"
android:layout_below="#+id/btnAddendumAddComment"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
</LinearLayout>
</ScrollView>
I havent added the fragment code but I will if someone wants to see it.
Thanks for any help.
You don't need fragment to hold only comment view. Just inflate new comment view and add it to the parent view.
LayoutInflater inflater = LayoutInflater.from(context);
View inflatedLayout= inflater.inflate(R.layout.yourLayout, container, false);
container.addView(inflatedLayout);
Where container is a view that hold all comments.
Regarding adding fragments, according to Fragments documentation:
If you're adding multiple fragments to the same container, then the
order in which you add them determines the order they appear in the
view hierarchy
Therefore if it's behave different, it could be related to OS version or bug :)
I have a main activity that starts up when the app opens. Once the activity is started, it then opens a GridView fragment from the main activity onCreate(Also, the main activity and fragment share the same XML layout).
The problem I am having is that whenever I try to add a onClick event to my Button, nothing happens unless I remove the code that opens up the GridView's fragment from my main activity.
NOTE: I am using fragments for my GridView because I am displaying lots of images at the same time, so I've set up Fragment classes to handle them efficiently without it effecting performance.
Would there be any way around this?, cheers in advance.
Main activity:
public class ImageGridActivity extends FragmentActivity {
private static final String TAG = "ImageGridActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.image_grid_fragment);
if (BuildConfig.DEBUG) {
Utils.enableStrictMode();
//Whenever I remove this code here:
}
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
final FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(android.R.id.content, new ImageGridFragment(), TAG);
ft.commit();
//To here, it works
Button B1 = (Button) findViewById(R.id.button1);
B1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.w("myApp", "no network");
}
});
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="#+id/gridView"
style="#style/PhotoGridLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="#dimen/image_thumbnail_size"
android:horizontalSpacing="#dimen/image_thumbnail_spacing"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="#dimen/image_thumbnail_spacing" >
</GridView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
I'm trying to set the Click event from the main layout as you can see
here. What could I do to fix this problem I have?
You'll never interact with the Button from the activity layout because you add the Fragment directly on the FrameLayout which holds the content, so the fragment will cover the previously set content of the Activity. You could modify the activity layout like this:
R.layout.act_ImageGridActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frag_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
followed by using the above FrameLayout when doing the fragment transaction:
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.frag_container, new ImageGridFragment(), TAG);
ft.commit();
I am currently using ViewFlipper for my main activity with two different layouts. I want to use a third layout, but I can only find the showNext() and showPrevious() commands. Can someone show me how to implement a third layout using ViewFlipper?
Made an example for you that shows howto display different views in a ViewFlipper.
The layout of the example is made up of the following parts. There are three radio buttons. A ViewFlipper is placed below the radio buttons. This flipper holds three different simple views with different texts.
The radio buttons are then hooked up to a listener in the java code that will change the view displayed by the ViewFlipper depending on which radio button that currently is chosen.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<RadioGroup android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:layout_height="wrap_content"
android:id="#+id/radio0" android:layout_width="wrap_content"
android:text="Show View 1" android:checked="true"></RadioButton>
<RadioButton android:layout_height="wrap_content"
android:id="#+id/radio1" android:layout_width="wrap_content"
android:text="Show view 2"></RadioButton>
<RadioButton android:layout_height="wrap_content"
android:id="#+id/radio2" android:layout_width="wrap_content"
android:text="Show View 3"></RadioButton>
</RadioGroup>
<ViewFlipper android:id="#+id/ViewFlipper01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--adding views to ViewFlipper-->
<TextView android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First view is now displayed"></TextView>
<TextView android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second view is now displayed"></TextView>
<TextView android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third view is now displayed"></TextView>
</ViewFlipper>
</LinearLayout>
JAVA
package com.test.threeviews;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.ViewFlipper;
public class ThreeViewsinaFlipperActivity extends Activity {
RadioButton RB0;
RadioButton RB1;
RadioButton RB2;
ViewFlipper VF;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
* Find the views declared in main.xml.
*/
RB0 = (RadioButton) findViewById(R.id.radio0);
RB1 = (RadioButton) findViewById(R.id.radio1);
RB2 = (RadioButton) findViewById(R.id.radio2);
VF = (ViewFlipper) findViewById(R.id.ViewFlipper01);
/*
* Set a listener that will listen for clicks on the radio buttons and
* perform suitable actions.
*/
RB0.setOnClickListener(radio_listener);
RB1.setOnClickListener(radio_listener);
RB2.setOnClickListener(radio_listener);
}
/*
* Define a OnClickListener that will change which view that is displayed by
* the ViewFlipper
*/
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.radio0:
VF.setDisplayedChild(0);
break;
case R.id.radio1:
VF.setDisplayedChild(1);
break;
case R.id.radio2:
VF.setDisplayedChild(2);
break;
}
}
};
}
See this simple use of android.widget.ViewFlipper. With it you can create different layout from xml and then switch among them with simple method like this:
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);
// you can switch between next and previous layout and display it
viewFlipper.showNext();
viewFlipper.showPrevious();
// or you can switch selecting the layout that you want to display
viewFlipper.setDisplayedChild(1);
viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.secondLayout)
Xml example with tree layouts:
<ViewFlipper
android:id="#+id/myViewFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
<LinearLayout
android:id="#+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
<LinearLayout
android:id="#+id/thirdLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
</ViewFlipper>
You can use it in this way also. I have attached the java code and xml file where a button is used to change the flipper view.
package com.nikhil.play.add_subtract;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ViewFlipper;
public class Flipper extends Activity implements OnClickListener {
ViewFlipper flippy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flipper);
flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
flippy.setOnClickListener(this);
flippy.setFlipInterval(10000);
flippy.startFlipping();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
flippy.showNext();
}
}
XML CODE-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/viewFlipper1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flipper 2" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flipper 3" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flipper 4" />
</ViewFlipper>
</LinearLayout>
xml
<ViewFlipper
android:id="#+id/viewflip"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_weight="1"
/>
Java
public class BlankFragment extends Fragment{
ViewFlipper viewFlipper;
FragmentManager fragmentManager;
int gallery_grid_Images[]={drawable.image1, drawable.image2, drawable.image3,
drawable.image1, drawable.image2, drawable.image3, drawable.image1,
drawable.image2, drawable.image3, drawable.image1
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(fragment_blank, container, false);
viewFlipper =(ViewFlipper)rootView.findViewById(R.id.viewflip);
for(int i=0;i<gallery_grid_Images.length;i++)
{
// This will create dynamic image view and add them to ViewFlipper
setFlipperImage(gallery_grid_Images[i]);
}
return rootView;
}
private void setFlipperImage(int res) {
Log.i("Set Filpper Called", res+"");
ImageView image = new ImageView(getContext());
image.setBackgroundResource(res);
viewFlipper.addView(image);
viewFlipper.setFlipInterval(1000);
viewFlipper.setAutoStart(true);
}