0

How do I dynamically add image buttons on the click of a button? I need to add the image buttons in 2x3 format and it should be horizontally scrollable when it goes out of the screen.

package com.example.dynamic;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    LinearLayout linearLayout1;
    LinearLayout linearLayout2;
    int flag=0;


    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main);
        linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
        linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2);

    }

    public void onClick(View v){

        ImageView image = new ImageView(MainActivity.this);
                image.setBackgroundResource(R.drawable.ic_launcher);
                if(flag==0){
                linearLayout1.addView(image);
                flag=1;
                }
                else{
                 linearLayout2.addView(image);
                 flag=0;
                }

      }



}

and this is the layout I used:

<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=".MainActivity" >

            <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="Button" />


<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
    android:id="@+id/linearLayout1"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</ScrollView>

 <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
    android:id="@+id/linearLayout2"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
  </ScrollView>      



</LinearLayout>

The problem with this is that, when the number of image button exceeds the screen width, it is not scrollable. Another potential problem is that it might be possible to scroll the 2 rows separately, and not together. and that is not desirable. Please help me, I'm a beginner and pardon me for any mistakes while asking this.

mkr231
  • 68
  • 2
  • 16

1 Answers1

0

Сurrently there is no horizontal scrollview class in android. You can try some custom scroll views here, here or here. When you will choose which one class to use, you should create adapter, or add items directly into this scroll view. You dont need any layouts inside scroll view.

Community
  • 1
  • 1
user2729200
  • 171
  • 5
  • Thank you so much.That is exactly what I wanted. But I still have a few doubts. If I'm using [this](http://www.dev-smart.com/archives/34) as a horizontal scroll view, how would I be able to implement in 2 rows and dynamically add columns on click of a button? – mkr231 Sep 26 '13 at 18:39
  • You should search for horizontal gridview, or your item of list view should have two layouts. – user2729200 Sep 26 '13 at 18:59
  • I found [this](https://github.com/jess-anders/two-way-gridview) and I believe that would do the job. – mkr231 Sep 26 '13 at 19:06