basickarl
basickarl

Reputation: 40464

Android XML Layout - Better Solution?

I am currently using the XML:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="4"
    android:orientation="horizontal" >
</LinearLayout>

<ImageView
    android:id="@+id/iv_dot_loading1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:src="@drawable/shape_dot_loading"
    android:layout_gravity="center_vertical" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:orientation="horizontal" >
</LinearLayout>

<ImageView
    android:id="@+id/iv_dot_loading2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:src="@drawable/shape_dot_loading"
    android:layout_gravity="center_vertical" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:orientation="horizontal" >
</LinearLayout>

<ImageView
    android:id="@+id/iv_dot_loading3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:src="@drawable/shape_dot_loading"
    android:layout_gravity="center_vertical" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="4"
    android:orientation="horizontal" >
</LinearLayout>

shape_dot_loading.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <gradient android:startColor="#E8E8E8" android:endColor="#E8E8E8"
        android:angle="270"/>
</shape>

It does the job, however I feel that there should be a more simple solution to solve this?

Upvotes: 1

Views: 92

Answers (3)

Ben Pearson
Ben Pearson

Reputation: 7752

  1. Use a View (View) instead of a ViewGroup (LinearLayout) for your 'filler' views.
  2. It's not clear here because you have stripped out the surrounding XML, but when including it else where, I would define this in it's own file and use an <include /> to pull it in.

Based on your naming, it looks like you're trying to implement a progress (loading) bar? Depending on what you're trying to achieve, there might be a better solution or alternative approaches to building the XML.

Upvotes: 0

JJ86
JJ86

Reputation: 5113

You can use <include/> to avoid repetition: you can define in an xml file the ImageView and add it in your xml.

Also there is an Android official tutorial.

Upvotes: 0

Alexander
Alexander

Reputation: 48262

To avoid repetition you can define the repeating xml parameters as a style, then apply that same style to all your ImageViews in XML layout

Upvotes: 1

Related Questions