You can have more than one layout in your activity. You can
have a relative layout and a linear layout in the same activity, that is why we
call Nested View Group.
In this exercise, we will be making use of only linear
layouts.
I will also show you how to use linear layout with
horizontal orientation and vertical orientation.
Example 1.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Donald" android:textSize="30sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Soo Yii" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tunde" android:textSize="30sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ben" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Alice" android:textSize="30sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Donald" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Soo Yii" android:textSize="20sp" android:layout_marginLeft="5dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tunde" android:textSize="20sp" android:layout_marginLeft="5dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ben" android:textSize="20sp" android:layout_marginLeft="5dp" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Alice" android:textSize="20sp" android:layout_marginLeft="7dp" /> </LinearLayout> </LinearLayout>
Your App will look like this when you run it on your Android
Phone
Example 2.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#87CEEB" android:orientation="vertical" android:padding="16dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name: " android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="James John" android:textSize="30sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Job: " android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="40dp" android:text="Politician" android:textSize="30sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hobbies: " android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="Leading, Sleeping, Talking." android:textSize="30sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Marital Status: " android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="Engaged" android:textSize="30sp" /> </LinearLayout> </LinearLayout>
Your App will look like this when you run it on your Android
Phone
To preview your Activity as you modify it on Android Studio,
click Preview at the right side of your Android Studio.
Check the image below
You can add other views like ImageView and button to your
activity.
Working with colors
You can use any HTML color you want. In the second example,
I used HTML color by simply writing #87CEEEB.
Go on and check this link https://htmlcolorcodes.com/ for more colors that you can use.
Working with background
You can use colors for the background of your activity and
you can use images likewise.
In the previous article I showed you how to display an image
in your App. To set the background with an image having pasted the image(abc) in the drawable folder of your project
android:background="@drawable/abc".
To apply color as your activity background.
android: background=" #800080"
No comments:
Post a Comment