Search code examples
c#androidandroid-linearlayoutandroid-alertdialogtimepicker

Xamarin C#: center AXML elements vertically inside horizontal LinearLayout


I have an AlertDialog created with DialogBuilder and I inflate the contents of an axml file as its view. I have a two level system of nested LinearLayouts with the outer vertically positioning two other ones with contents. One of these contents contains a TextView and a somewhat shrunk TimePicker. Here is the relevant part of the corresponding axml:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="Start"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:gravity="center_vertical"/>
        <TimePicker
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleX="0.80"
            android:scaleY="0.80"
            android:timePickerMode="spinner"
            android:id="@+id/endPicker" />
    </LinearLayout>

And this is how it looks like on my smartphone (AM/PM is in my mothertongue):

enter image description here

The text START refers to the start of a period of time and I want to align it vertically to the center of the time picker something like this (which is oc a manually modified pic):

enter image description here

For some reason, however, I cannot do that, no matter, how I align my texts. I've already tried a TableView similar to the one suggested HERE, but it didn't help. I also tried to play with the margins of both elements but event when the picker was so "padded" that it was not visible, the text still remained at its position dispite being set to center_vertical. I assume some hidden element belonging to the TimePicker is to be blame here, but I can't figure out what and how. What is the thing I'm not getting here?


Solution

  • Try changing the gravity field in the TextView from "center_vertical" to just "center"

    Edit: adding code

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/textView1"
            android:gravity="center" />
        <TimePicker
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/timePicker1" />
    </LinearLayout>