Introduction to Android Views



Introduction to Android Views:-

The View class is a superclass for all GUI components in Android. Android contains the following commonly used View subclasses:

Most commonly used Android View classes

Here we have some of the most commonly used android View classes:
TextView in Android:-
TextView is the most widely used view used to show pre-defined text on display screen.
Following are some of the main attributes that are most commonly used:

Attribute
Description
android:text
Used to specify the text to be displayed in the TextView
android:textSize
Using this attribute we can control the size of the text.
android:textColor
Using this attribute we can specify the color of our text.
android:textAllCaps
If set True, this will make the text appear in upper case.
android:letterSpacing
Using this attribute we can set the spacing between letters of the text.
android:hint
This attribute is used to show a default text, if no text is set in the TextView.

EditText View in Android:-
EditText is a TextView which is editable. It has almost similar properties as a TextView. EditText is used when you want to have a text field in your application where user can enter any text. It can be either single line or multi-line.
Following are some of the attributes that are most commonly used:
Attribute
Description
android:inputType
Used to specify what the text entered should be like and for what purpose it will be used. Text…
  • textAutoComplete - This provides with suggestions as user is typing in text.
  • textAutoCorrect - This will enable auto correct on user input text.
  • textPassword - Display the entered text in form of dots or stars.
  • textEmailAddress
  • phone - This will present only the numeric keyboard to users.

RadioButton View and CheckBox View in Android:--

CheckBox View in Android:-

Checkbox is used when you have to show multiple options to the user and the user is allowed to choose as many options as they want, by tapping on them. You can set its default check status as true or false and other properties are same as TextView.
<android:checked="true"/>

RadioButton View in Android:-

RadioButton is used when you have to allow selection of only one option among the list of multiple options -:

<RadioGroup
    android:id="@+id/rg_gender"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="20dp">

    <RadioButton
        android:id="@+id/rb_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male"
        android:textColor="@android:color/black"/>

    <RadioButton
        android:id="@+id/rb_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="Female"/>

</RadioGroup>

Button View in Android

Button, as understood by its name, is a component which can be pressed or clicked by the user to perform an action.

<Button
    android:id="@+id/btn_submit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:textColor="@android:color/holo_blue_dark"/>

Note: If you do not know about the Activity class as of now, do not worry. We will explain it very soon.


public class MainActivity extends Activity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        // creating instance of button
        Button b = (Button) findViewById(R.id.btn_submit);
        // setting on click event listener
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
             
                // Perform action on click
                 
            }
        });
    }
}


ImageView and ImageButton in Android:-


ImageView in Android:-

Image View is used to show any picture on the user interface.
Following are some of the main attributes that are most commonly used:
Attribute
Description
android:maxHeight
Used to specify a maximum height for this view.
android:maxWidth
Used to specify a maximum width for this view.
android:src
Sets a drawable as the content for this ImageView.
android:scaleType
Controls how the image should be resized or moved to match the size of the ImageView.
android:tint
Tints the color of the image in the ImageView.

Using an Image as Button

ImageButton has the same property as ImageView. Only one feature is extra, which is, images set through ImageButton are clickable, and actions can be attached with them upon clicking.

<ImageButton
    android:id="@+id/imgButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    android:src="@drawable/img_nature"/>


Just like a button, the setOnClickListener() can be used to set an event listener for click event on this.


imageButton = (ImageButton)findViewById(R.id.imgButton);
imageButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        
        // do anything here
        
    }
});



Post a Comment

0 Comments