user3381665
user3381665

Reputation: 1141

setting up multiple buttons in fragment

I have a fragment like so

    public class PagesFragment extends Fragment  {
    public PagesFragment(){}
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.section, container, false);
    return v;
}}

and I wanted to setup six buttons that open six corresponding xml layouts. If there is a way to do this with out setting up six different activities I would love to know.

The xml:

   <ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:background="#ffffff"
    android:clickable="true">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff">



        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/art"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/art"
            android:layout_gravity="center_horizontal|bottom"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"
             />


        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/ext"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/ext"
            android:layout_below="@+id/art"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/inter"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/inter"
            android:layout_below="@+id/ext"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"/>


        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/sport"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/sport1"
            android:layout_below="@+id/inter"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"/>
        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/opp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/opp"
            android:layout_below="@+id/sport"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"/>

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/muse"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/muse"
            android:layout_below="@+id/opp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"/>
    </RelativeLayout>
</ScrollView>

Thankyou in advanced.

Upvotes: 0

Views: 163

Answers (1)

cristiano2lopes
cristiano2lopes

Reputation: 3498

Fragments it's the way to go. Create an activity (ExampleActivity) with a layout that contains a FrameLayout (id=example_id) that will be replaced dynamically by a fragment.

   //inner xml of Example Activity Layout
   <FrameLayout
    android:id="@+id/example_id"
    android:layout_width="match_parent"/>

When launching the intent from the button to ExampleActivity pass info about the fragment you want to use (maybe a string to use in a factory so that you can instantiate the correct fragment).Then on the activity:

 Fragment fragment;
 //choose the fragment from intent data
 fragment = createFragmentFromIntent(intent);


 FragmentManager fragmentManager = getChildFragmentManager();
 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 fragmentTransaction.replace(example_id, fragment);
 fragmentTransaction.commitAllowingStateLoss();

Hope it helps.

Upvotes: 1

Related Questions