user1223035
user1223035

Reputation: 241

How to create a dynamic sliding screens in Android and show given below layout

I have a layout which has a dynamically generated radiobuttons and a submit button which gets the value of selected radiobuttons on SUBMIT button. Now I want to create a dynamically generated sliding view for (n) pages where i can show same layout for different pages. Below is my approach and a working code. Thanks in advance

MainActivity.java

public class MainActivity extends Activity {

    private String[] application = { "ABC", "DEF", "GHI", "HIJ" };                     
    private String[] device = { "ABC", "DEF", "GHI", "HIJ" }; 
    private RadioGroup radioGroup1;
    private RadioGroup radioGroup2;
    private RadioButton btn;
    private RadioButton btn2;
    private String text1;
    private String text2;
    RadioButton button1;
    RadioButton button2;
    Button selectall;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkButtonClick();
        drawRadiobuttons();

    }

    private void checkButtonClick() {

        Button myButton = (Button) findViewById(R.id.findSelected);
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                StringBuffer responseText = new StringBuffer();
                responseText.append("");

                // Get selected radiobuttons
                if (radioGroup1.getCheckedRadioButtonId() != -1) {
                    text1 = btn.getText().toString();
                    Log.d("Button", "Text 1 : " + text1);
                }

                if (radioGroup2.getCheckedRadioButtonId() != -1) {
                    text2 = btn2.getText().toString();
                    Log.d("Button", "Text 2 : " + text2);
                }


                Toast.makeText(
                        getApplicationContext(),
                        "Data Posting : APPLICATION : "
                                + text1 + " \nDEVICE : " + text2,
                        Toast.LENGTH_LONG).show();


            }
        });

    }

    private void drawRadiobuttons(){

        radioGroup1 = (RadioGroup) findViewById(R.id.radio1);
        radioGroup2 = (RadioGroup) findViewById(R.id.radio2);

        ViewGroup hourButtonLayout = (ViewGroup) findViewById(R.id.radio1);
        for (int i = 0; i < application.length; i++) {
            button1 = new RadioButton(this);
            button1.setId(i);
            button1.setText(application[i]);
            hourButtonLayout.addView(button1);

            radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup mRadioGroup2,
                                int checkedId2) {
                            for (int i = 0; i < mRadioGroup2.getChildCount(); i++) {
                                btn = (RadioButton) mRadioGroup2.getChildAt(i);
                                int t = mRadioGroup2.getId();
                                System.out.println(t);

                                if (btn.getId() == checkedId2) {
                                    text1 = btn.getText().toString();
                                    Toast.makeText(getApplicationContext(),
                                            "You selected : " + text1,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }

        ViewGroup hourButtonLayout2 = (ViewGroup) findViewById(R.id.radio2); 
        for (int i = 0; i < device.length; i++) {
            button2 = new RadioButton(this);
            button2.setId(i);
            button2.setText(device[i]);
            hourButtonLayout2.addView(button2);

            radioGroup2
                    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup mRadioGroup,
                                int checkedId) {
                            for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
                                btn2 = (RadioButton) mRadioGroup.getChildAt(i);
                                int t = mRadioGroup.getId();
                                System.out.println(t);

                                if (btn2.getId() == checkedId) {
                                    text2 = btn2.getText().toString();
                                    Toast.makeText(getApplicationContext(),
                                            "You selected : " + text2,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }

    }

}

activity_main.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:orientation="vertical"
    android:background="#fff">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#000"
        android:text="Select Question1"
        android:textSize="18sp"/>

    <Button
        android:id="@+id/findSelected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"/>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="200dip"
        android:orientation="vertical"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="200dip"
            android:orientation="vertical">

            <RadioGroup
                    android:id="@+id/radio1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dip"
                    android:layout_marginTop="5dip"
                    android:background="#fff"
                    android:checkedButton="@+id/sound" >
                </RadioGroup>
        </LinearLayout>
    </ScrollView>

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_marginTop="5dip"
        android:layout_marginBottom="5dip"
        android:text="Select Question2"
        android:textColor="#000"
        android:textSize="18sp"/>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="360dip"
        android:orientation="vertical"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="560dip"
            android:orientation="vertical">

            <RadioGroup
                    android:id="@+id/radio2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dip"
                    android:layout_marginTop="5dip"
                    android:background="#fff"
                    android:checkedButton="@+id/sound">
                </RadioGroup>

        </LinearLayout>
    </ScrollView>

</LinearLayout>

Can anyone help ?

Upvotes: 1

Views: 438

Answers (1)

agamov
agamov

Reputation: 4427

If I'm getting you right, by sliding views you mean ViewPager. You can parametrize ViewPager's adapter, i.e. pass the number of pages to your Adapter's constructor. Have a look at FragmentPagerAdapter or FragmentStatePagerAdapter.

  1. In your button click listener calculate your number of pages needed (n).

  2. Pass it in Intent to new Activity.

  3. Implement your own FragmentViewPager and pass your n value to its constructor.

  4. ...

  5. PROFIT!

Sample here.

Upvotes: 1

Related Questions