0

I have two linear layouts ,main layout consist of two textviews and a inner layout which contains a button and textview... I want to change the position of my inner layout button which i want to go below after last textivew in main linearlayout.. Here is my xml file ....

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Personal Information"
            android:textSize="30sp" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        <TextView
                 android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Email" />

        <Button
            android:id="@+id/btnAddEmail"
            android:layout_width="93dp"
            android:layout_height="wrap_content"
            android:text="Add" />   
    </LinearLayout>
    <TextView 
        android:id="@+id/tvEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />
    </LinearLayout>

</ScrollView>
0

3 Answers 3

0

One solution will be to have another invisible button in the other position and when you want to "move" the button you make the current one "invisible" and switch the other.

3
  • But i have a click listner on that button .. if i change the visibility so how could i do the same work on other which i want to visible @alfasin Feb 15, 2014 at 8:06
  • I dont understand how?? Feb 15, 2014 at 8:20
  • Use your_btn.performClick() in click event of your below button.
    – AndiM
    Feb 15, 2014 at 11:51
0

Like alfasin said have two buttons in your layout. Set the visibility of one to GONE or INVISIBLE depending on your requirements. INVISIBLE will just hide the button from view however the space required to show the button will be preserved. GONE will hide the button and the space it takes up in the layout will also be removed.

btn.setVisibilty(View.GONE)
btn.setVisibilty(View.INVISIBLE)
btn.setVisibilty(View.VISIBLE)

If you want to have the same action triggered by both then register the same click listener for both buttons. Have an additional check to toggle the visibility based on the button clicked.

0

Add a second button below your last TextView and set its visibility to "gone".

XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Personal Information"
        android:textSize="30sp" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Email" />

        <Button
            android:id="@+id/btnAddEmail"
            android:layout_width="93dp"
            android:layout_height="wrap_content"
            android:text="Add" />   
    </LinearLayout>
    <TextView 
        android:id="@+id/tvEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />
    <Button
        android:id="@+id/btnNew"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:visibility="gone" />   
</LinearLayout>

Get your Buttons, initialize your OnClickListener and do your Logic. Then switch the visibility states of your two buttons and you are done:

private Button mAddMail;
private Button mNewButton;

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

    mAddMail = (Button) findViewById(R.id.btnAddEmail);
    mNewButton = (Button) findViewById(R.id.btnNew);

    mAddMail.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Do your logic
            // ....
            mAddMail.setVisibility(View.GONE);
            mNewButton.setVisibility(View.VISIBLE);
        }
    });
}

With this approach you don't directly change your buttons position. It's more like a little workaround. You always have two buttons and you only change their visibility.

1
  • But i have so many buttons in my project .. i think it will go to a very lengthy process ..... Thanx for the response Feb 15, 2014 at 9:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.