ammu
ammu

Reputation: 117

vertical scroll view for dynamically created buttons

I am having a problems with adding scroll view in my dynamically created buttons.here is my xml code.

<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="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="27dp"
        android:background="@drawable/custom_border"
        android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="243dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/linearLayout1"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:hint="@string/hinttextitem" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="26dp"
        android:background="@drawable/button_shape"
        android:onClick="newonClick"
        android:text="click" />
  </LinearLayout>


     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/linear1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:id="@+id/dynamic"
        android:background="@drawable/dynamic_border"
        android:orientation="vertical" >
      </LinearLayout>

</RelativeLayout>

here is my java code

public class MainActivity extends Activity {

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

public void newonClick(View V){
    LinearLayout layout=(LinearLayout)findViewById(R.id.linear1);
    LinearLayout dynamiclayout=(LinearLayout)findViewById(R.id.dynamic);
    dynamiclayout.removeAllViews();
    EditText edittext = (EditText) findViewById(R.id.editText1); 
    int x=Integer.valueOf(edittext.getText().toString());
    for(int i=1;i<=x;i++)
    {
      Button addButton =new Button(this);
      addButton.setText("Button"+i);
      addButton.setId(i);
      dynamiclayout.addView(addButton);
      addButton.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             Toast.makeText(getApplicationContext(),
                  "button is clicked"+v.getId(), 8000).show();
         }
      });
    }

if am adding scroll view in dynamic layout, it will disappear.so how can i add vertical scroll view only for dynamically created buttons.can anyone solve my problem?

Upvotes: 2

Views: 2579

Answers (3)

No_Rulz
No_Rulz

Reputation: 2719

Try to use, In XML,

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

<LinearLayout
        android:id="@+id/dynamic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:orientation="vertical" >
</LinearLayout>
</ScrollView>

In Java:

this.scrollView = (ScrollView) findViewById(R.id.scrollView1);
this.linearLayout = (LinearLayout) findViewById(R.id.dynamic);
this.linearLayout.setOrientation(LinearLayout.VERTICAL);
Button[] btn = new Button[10];
for (int i = 0; i < 10; i++) {
    btn[i] = new Button(this);
    btn[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    btn[i].setText("This is the button" + i);
   this.linearLayout.addView(btn[i]);
}

Upvotes: 1

balaji koduri
balaji koduri

Reputation: 1321

try this: place scroll view for LinearLayout only.

 <ScrollView
    android:layout_width="wrap_content"
    android:layout_below="@+id/linear1"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/dynamic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:background="@drawable/dynamic_border"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

Upvotes: 4

dipali
dipali

Reputation: 11188

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/linear1" >

        <LinearLayout
            android:id="@+id/dynamic"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="80dp"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

please replace this code:

Upvotes: 0

Related Questions