Build your First App Part 2

android_learners_hub


                  Build your First App Part 2

 For part1 click this link Build your First App Part 1 (onlyjavaforall.blogspot.com)

View and View Group:-


View:-Whenever you want to apply some action on Button, Textfield and other UI components (that we will do in coming posts) you will notice that you have to import View class by using following import statement:


import android.view.View;


All these components which respond to user action are called Views. For example, if you have to set visibility of the buttons as invisible then you have to use View.INVISIBLE as parameter of setVisibility method as shown below.


button.setVisibility(View.INVISIBLE);


View Group: - A View Group holds View in it means on which the Views appear is called a View Group. A View Group is invisible.

Example: - Layouts (Linear, Relative and Constraint etc.)



Today we will cover Linear Layout (View Group) consisting Button and Text View (Views).


Linear Layout:- 

Linear Layout display the data into linear order whether it is in Horizontal (in single column and multiple rows) or/and in Vertical (that is in multiple rows and single columns). It can be displayed in nested way also.


To use Linear Layout just write the same code as shown below or you can also drag and drop. (for that go to design<<Layout<<Linear Vertical)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

</LinearLayout>



In Linear Layout we will cover TextView and Button. TextView is used to view some text on the screen and Button is used to display the button. Both have same concept as we read in java.

Following is the syntax to use Button and TextView in XML:-

<Button attributes />
<TextView  attributes />

Beside of button and TextView we can use any component from palette in it.

Example of Linear Layout (vertical):-

In the attribute of Linear Layout set orientation to “vertical”. Then, to create Text Views and Button write the following code in between the <LinearLayout></LinearLayout>:-

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView 1"
    android:textSize="50dp" />

<TextView
    android:id="@+id/text2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView 2"
    android:textSize="50dp" />

<TextView
    android:id="@+id/text3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView 3"
    android:textSize="50dp" />

<TextView
    android:id="@+id/text4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView 4"
    android:textSize="50dp" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Click me"
    android:textSize="20dp" />


Output:

Example of Linear Layout (Horizontal):-

In the attribute of Linear Layout set orientation to “Horizontal”. Then, to create TextView and Button write the following code in between the <LinearLayout>
</LinearLayout>.

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView 1"
    android:textSize="50dp" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Click me"
    android:textSize="20dp"/>

Output:


This was an introduction to view groups and views we will discuss rest Views and Viewgroups in coming posts. For any query feel free to comment.

   ---------------------------------------------------------------------------------------
   ---------------------------------------------------------------------------------------

Comments

Post a Comment