0

I want to create a dynamic horizontal layout which have 3 views which take equal space on screen. When 1 view is hidden then remaining views should fill up its space.

I am trying to achieve this using xml and not considering to write some code for it.

I am able to get 3 views to take equal space to fill the screen by using LinearLayout and weight but not able make it so that view should fill the space if some views are hidden.

My layout looks like this.

<LinearLayout
    android:layout_width="match_parent"
    android:weightSum="3"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="This is bob" />
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="This is bob" />
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="This is bob" />
</LinearLayout>

I thought it must be already asked by someone but i am not able to find any relevant question.

4
  • whats the error????????? Commented Nov 23, 2016 at 13:30
  • make visibility of First TextView as View.INVISIBLE Commented Nov 23, 2016 at 13:31
  • @Vijay: When i hide 1 TextView. Remaining 2 TextViews cover 66% and 33% space is empty. i want to Take 50% space by each textView so that 100% is filled by remaining 2 Commented Nov 23, 2016 at 13:32
  • You should remove weightsum. Check answer given by readyanderoid, if it works for you accept it. Commented Nov 23, 2016 at 13:36

2 Answers 2

2

Add

android:orientation="horizontal" 

and remove

android:weightSum="3" 

from main layout it will work.

Your layout should be like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="This is bob" />
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="This is bob" />
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="This is bob" />
</LinearLayout>
Sign up to request clarification or add additional context in comments.

1 Comment

It was in back of my head and I already had done this but at the very moment but was not able to remember. Thats perfect.
0

Use databinding for changing android:weightSum attribute in container when view's visibility is set to GONE. see docs: https://developer.android.com/topic/libraries/data-binding/index.html. You'll have to create one model object anyways.

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.