user152356 user152356 - 2 months ago 24
Android Question

Xamarin match_parent to left?

I am trying to make an Android app with space usage displayed at the top. I want a

TextView
saying "Usage:" on the left, another
TextView
saying "x.x/x.x GB" on the right, and a horizontal
ProgressBar
in between. I am currently stuck at the
ProgressBar
part: I want it to fill the space between the 2
TextViews
, but not sure how.

If you can't tell, I am very new to Xamarin, but I'm pretty sure it's easy to do in Android Studio. (This is also my main complaint about Xamarin so far: programming is easier than Android Studio, but Layout design is much less intuitive)

Answer

Use a LinearLayout with horizontal orientation. The both TextViews have android:layout_width="wrap_content" and the ProgressBar needs android:layout_width="0dp" and android:layout_weight="1". This means: make the TextViews as wide as the Text is and stretch the ProgressView in between.

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Usage:" />
    <ProgressBar
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:max="100"
        android:progress="50"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="x.x/x.x GB" />
</LinearLayout>

enter image description here

Comments