I am having trouble with Material Design layout.
Here is what I have:
and the code to represent that
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@color/backgroundColor"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.mycompany.inventory.MainActivity">
<ImageView
android:id="@+id/logoView"
android:layout_width="225dp"
android:layout_height="90dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/mycompanylogo"
android:layout_marginBottom="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="USERNAME"
android:id="@+id/txtUsername"
android:layout_below="@+id/logoView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:layout_below="@id/txtUsername"
android:id="@+id/txtScannedCount"/>
<TextView
android:text="Scanned Today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_below="@+id/txtScannedCount"
android:layout_alignLeft="@+id/txtScannedCount"
android:layout_alignStart="@+id/txtScannedCount" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/txtInventoryCount"
android:layout_below="@id/txtUsername"
android:layout_alignRight="@+id/btnSearch"
android:layout_alignEnd="@+id/btnSearch" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Inventory"
android:id="@+id/textView4"
android:layout_above="@+id/btnSearch"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_marginLeft="153dp"
android:layout_marginStart="153dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_searchvehicle"
android:id="@+id/btnSearch"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp"
android:layout_marginBottom="15dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@color/colorPrimary"
android:backgroundTint="@color/colorPrimaryDark"
android:textColor="@color/buttontext"
android:onClick="NavSearchVechiclesPressed"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_scanbarcode"
android:id="@+id/btnScanner"
android:layout_below="@+id/btnSearch"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/btnSearch"
android:layout_alignEnd="@+id/btnSearch"
android:background="@color/colorPrimary"
android:backgroundTint="@color/colorPrimaryDark"
android:textColor="@color/buttontext"
android:onClick="NavScanBarcodePressed"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/txtVersion"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="@color/divider"/>
</RelativeLayout>
And through the magic of Photoshop here is what I want.
How can I obtain he lay out with the “Scanned Today” and “Inventory” text views as well as the large centered numbers?
4
Answers
I think what you want is for the TextView text to be center justified horizontally, in which case add
to your textViews
You can (and probably you should) use other layouts inside your parent relativeLayout. This way you simplify a lot the design and you avoid that all the design is dependant of one item (on your layout, if you remove one of the textViews most of the layout is repositioned, as they were relative to that textView)
A quick example of this with your code could be something like this:
(I removed the text, colors and images, but it looks similar to what you want)
Try playing around with different sub-layouts and it will make your life easier 🙂
use layout as
Try this one