4
\$\begingroup\$

I am trying to make a nice set of items, which are equally spaced from each other. On example below, I set buttons between empty textview. Every button and text view has height = 0dip and weight=1. And it looks pretty nice. However, is there a way to clean up the layout file?

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:text="" />

            <Button
                android:id="@+id/btn_distance_walked"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:clickable="false"
                android:text="Distance walked = " />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:text="" />

            <Button
                android:id="@+id/btn_exersise_time"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:clickable="false"
                android:text="Exercise time = " />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:text="" />

            <Button
                android:id="@+id/btn_average_speed"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:clickable="false"
                android:text="Average speed = " />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:text="" />

            <Button
                android:id="@+id/btn_calories_burned"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:clickable="false"
                android:text="Callories burned = " />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:clickable="false"
                android:text="" />

            <Button
                android:id="@+id/btn_back"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:text="Back" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:text="" />
        </LinearLayout>
\$\endgroup\$

closed as unclear what you're asking by Simon Forsberg, Pimgd, Vogel612, Yann, Heslacher Oct 31 '14 at 15:43

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    \$\begingroup\$ Can you add some screenshot of how this looks for you? \$\endgroup\$ – Simon Forsberg Jun 17 '14 at 13:32
  • \$\begingroup\$ If you need both buttons and TextViews then this seems good! \$\endgroup\$ – Jabbar_Jigariyo Oct 10 '14 at 21:53
  • \$\begingroup\$ It will look best if you use ListView with some style instead of TextView and set the property for divider in ListView as per requirement and it will look great. \$\endgroup\$ – Aditya Vyas-Lakhan Oct 30 '14 at 13:44
  • 2
    \$\begingroup\$ How do you make a GUI description more efficient? I don't get it. \$\endgroup\$ – Pimgd Oct 30 '14 at 14:50
  • 1
    \$\begingroup\$ I'm not familiar with Android's process, but could you use theming or styles to prevent duplication of height, weight and wrap values? \$\endgroup\$ – Nick Udell Oct 30 '14 at 17:23
8
\$\begingroup\$

I don't know what the standards are for Layout Naming on Android, but I can tell you that these names are not very good.

  • textView2
  • btn_distance_walked
  • textView3

none of these accurately describes what they do.

you should probably look for naming schemes on Google.

btn_distance_walked doesn't tell us what the button is going to do. What is it going to do with the distance walked? It should be something like calculate_Distance or calculateDistance (my preferred naming scheme camelCasing)

Update:

I recently answered a question similar to this one and came up with a name scheme for buttons, action_ObjectActedUpon this may be more specific to the naming issue in your code as well.


Your indentation seems to be off as well, it should look like this

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:text="" />

    <Button
        android:id="@+id/btn_distance_walked"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:clickable="false"
        android:text="Distance walked = " />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:text="" />

    <Button
        android:id="@+id/btn_exersise_time"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:clickable="false"
        android:text="Exercise time = " />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:text="" />

    <Button
        android:id="@+id/btn_average_speed"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:clickable="false"
        android:text="Average speed = " />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:text="" />

    <Button
        android:id="@+id/btn_calories_burned"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:clickable="false"
        android:text="Callories burned = " />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:clickable="false"
        android:text="" />

    <Button
        android:id="@+id/btn_back"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:text="Back" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:text="" />
</LinearLayout>

I just answered a similar question and I think that I should add a little bit of that answer to this one..

Another way that you can reduce clutter in the layout is to define some styles by creating an xml file in the res/values/ folder of your project and do this

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyStyle1">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">0dip</item>
        <item name="android:layout_weight">1</item>
        <!-- etc. -->
    </style>
</resources>

and then in your Layout XML you add the style to the layout element like this

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical" >

    <TextView
        style="@style/MyStyle1"
        android:id="@+id/textView2"
        android:text="" />

    <Button
        style="@style/MyStyle1"
        android:id="@+id/btn_distance_walked"
        android:clickable="false"
        android:text="Distance walked = " />

    <TextView
        style="@style/MyStyle1"
        android:id="@+id/textView3"
        android:text="" />

    <Button
        style="@style/MyStyle1"
        android:id="@+id/btn_exersise_time"
        android:clickable="false"
        android:text="Exercise time = " />

    <TextView
        style="@style/MyStyle1"
        android:id="@+id/textView4"
        android:text="" />

    <Button
        style="@style/MyStyle1"
        android:id="@+id/btn_average_speed"
        android:clickable="false"
        android:text="Average speed = " />

    <TextView
        style="@style/MyStyle1"
        android:id="@+id/textView5"
        android:text="" />

    <Button
        style="@style/MyStyle1"
        android:id="@+id/btn_calories_burned"
        android:clickable="false"
        android:text="Callories burned = " />

    <TextView
        style="@style/MyStyle1"
        android:id="@+id/textView6"
        android:clickable="false"
        android:text="" />

    <Button
        style="@style/MyStyle1"
        android:id="@+id/btn_back"
        android:text="Back" />

    <TextView
        style="@style/MyStyle1"
        android:id="@+id/textView1"
        android:text="" />
</LinearLayout>

note that I am not a professional, but I found Styles and Themes from doing a quick search for styles.xml android example

Don't forget about Developer.Android.com it can be very helpful.

\$\endgroup\$

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