Snake
Snake

Reputation: 14648

Visible and Invisible Layouts

I need your advice regarding my design and if you have a better idea (or you agree with me) that it is good. for some reason I have a feeling it is a "stone age programming style" but lets see.

Basically I have my xml relative layout. In the center (vertical and horizenatlly). I want to display "either" 3 buttons OR 3 texts depending on some user input. So what I did is the following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clipChildren="false"
    android:clipToPadding="false" >

    <RelativeLayout android:id="@+id/Buttons"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
        <Button1 .../>
        <Button2 .../>
        <Button3 .../>
    </RelativeLayout>

    <RelativeLayout android:id="@+id/Texts"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
       <TextView1 .../>
       <TextView2.../>
       <TextView3.../>
    </RelativeLayout>

</RelativeLayout>

Depending on the user input in the code I set visibility to either Visible or Invisible

Is this alright? and if not what do you suggest?

Upvotes: 0

Views: 2096

Answers (3)

Shubhayu
Shubhayu

Reputation: 13552

In my opinion, what you have done may be primitive, but it is simple, which is good :) But if you still want some options and make life complicated, then

  1. Put each of the blocks in a separate xml and use include.
  2. Make 2 views and then use ViewFlipper to flip them based on user requirements.

But for the simple requirement that you have, I think u r doing fine.

The include option would work something like this,

layout_1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Buttons"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
        <Button1 .../>
        <Button2 .../>
        <Button3 .../>
</RelativeLayout>

layout_2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Texts"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
       <TextView1 .../>
       <TextView2.../>
       <TextView3.../>
</RelativeLayout>

your main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clipChildren="false"
    android:clipToPadding="false" >

    <include layout="@layout/layout_1" />
    <include layout="@layout/layout_2" />   

</RelativeLayout>

The include helps you in making reusable layouts and also helps in keeping your xml files grow out of proportions, specially when you have complicated UIs.

Upvotes: 2

Pavandroid
Pavandroid

Reputation: 1586

First option is:

You can add all the three buttons or textviews in Linearlayout and center it in parent by using android:layout_centerInParent.

Second option is:

You can center the middle button out of all the three buttons and adjust the other two buttons with respective to the middle button. Same way we should also repeat this for textviews. In this option, we should make all the three views visibility to View.GONE explicitly.

Upvotes: 0

Raunak
Raunak

Reputation: 6507

What you need is View.GONE, and View.VISIBLE.

With View.GONE - the layout doesn't occupy any space (almost as if it didn't exist at all). If you use View.INVISIBLE, to the user, the view (buttons, or text in your case) will not be visible, but they will still be there on the screen, thus shifting the other view (buttons, or text) up or down (the view won't be in dead center).

TIP: You can use 'android:layout_centerInParent' instead of 'android:layout_centerHorizontal' and 'android:layout_centerVertical'.

Upvotes: 3

Related Questions