1

I'm new in android development and am facing issue with my test code . I want add buttons when click on the "+" button. Its getting added but position is not correct. I want to add the new button after edit text which is defined through XML.Am adding xml and my code. Thnks.

  <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:background="#EBE7E4" >

<RelativeLayout
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@layout/header_gradient"
    android:gravity="right"
    android:paddingBottom="5dip"
    android:paddingTop="5dip" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="false"
        android:layout_centerVertical="true"
        android:layout_marginLeft="18dp"
        android:src="@drawable/content_new" />
</RelativeLayout>

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/header"
    android:layout_marginLeft="36dp"
    android:ems="10"
    android:hint="Name of label" >

    <requestFocus />
</EditText>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_alignRight="@+id/editText1"
    android:layout_below="@+id/editText1" />

<LinearLayout
    android:id="@+id/lId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner1"
    android:layout_centerVertical="true"
    android:layout_marginLeft="33dp"
    android:text="Save &amp; Continue" />

private Spinner spinner;
ImageButton btnAddNew;
public static int MY_BUTTON = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_steps);        
    btnAddNew =(ImageButton)findViewById(R.id.imageButton1);        
    addItemsOnSpinner();

    btnAddNew.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
              Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + MY_BUTTON);
            LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
            Button newPass = new Button(getApplicationContext());
            newPass.setText("Add LABEL");
            newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            newPass.setWidth(418);
            newPass.setId(MY_BUTTON);
            //newPass.
            newPass.setOnClickListener(this);
            rAlign.addView(newPass);
            MY_BUTTON ++;

        }
    });
}   
3
  • for that you need to create your layout dynamically Commented Sep 19, 2012 at 5:56
  • You are adding your button to rAlign (Linear layout) so you have to first align this layout below/right of edit text Commented Sep 19, 2012 at 6:03
  • @ JaiSoni hmm ok let me try :) thnx Commented Sep 19, 2012 at 6:07

2 Answers 2

2

Here i solved by changing some lines in your XML

Here is a XML

<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:background="#EBE7E4" >

<RelativeLayout
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:gravity="right"
    android:paddingBottom="5dip"
    android:paddingTop="5dip" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="false"
        android:layout_centerVertical="true"
        android:layout_marginLeft="18dp"/>
</RelativeLayout>

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/header"
    android:layout_marginLeft="36dp"
    android:ems="10"
    android:hint="Name of label" >

    <requestFocus />
</EditText>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_alignRight="@+id/editText1"
    android:layout_below="@+id/editText1" />

<LinearLayout
    android:id="@+id/lId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner1"
    android:layout_below="@+id/spinner1"
    android:orientation="vertical" >
</LinearLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner1"
    android:layout_below="@+id/lId"
    android:layout_centerVertical="true"
    android:layout_marginLeft="33dp"
    android:text="Save &amp; Continue" />
</RelativeLayout>

Nothing changed in onCreate function

Sign up to request clarification or add additional context in comments.

1 Comment

There is a Problem in your OnCreate Function if you click that AddLabel button it will generate new Button by solve that problem you need to remove newPass.setOnClickListener(this); or if you need to do action create new listener and do what ever you want
0

- Though this solution will not work for N nos of Button, but will work if you know how many buttons to appear in hand

- Use TableRow after EditText, and then after dynamically creating the buttons add them in the TableRow.

- Take care that the buttons don't pass the right side of the screen when the buttons gets more than screen can show in emulator or phone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.