WEEK - 4
1. Create different View in Android Application with ex-
ample
Ans:. In Android, there are various types of views that can be used to cre-
ate different user interface elements in an application. Here are a few ex-
amples of different views in Android:
1. TextView: This view is used to display text on the screen. It can be
used for labels, headings, or any other text-based content.
Example:
<TextView
android:id="@+id/my_text_view"
android:layout_width=“match_parent”
android:layout_height=“wrap_content"
android:text="Hello, World!” />
Inside java or kotlin class files (i.e..for eg.MainActivity.java ) we need to
instantiate the view to use textview.
TextView tv = (TextView) findViewById(R.id.my_text_view);
2. EditText: This view is used to allow users to enter text input. It can be
used for login screens, search bars, or any other user input field.
Example:
<EditText
android:id=“@+id/my_edit_text"
android:layout_width=“match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name”/>
Inside java or kotlin class files (i.e..for eg.MainActivity.java ) we need to
instantiate the view to use edittext.
EditText et = (EditText) findViewById(R.id.my_edit_text);
3. Button: This view is used to create clickable buttons that perform ac-
tions or trigger events. It can be used for submitting forms, navigating
to other screens, or any other user interaction.
Example:
<Button
android:id=“@+id/my_button"
android:layout_width=“match_parent”
android:layout_height=“wrap_content"
android:text="Click me” />
Inside java or kotlin class files (i.e..for eg.MainActivity.java ) we need to
instantiate the view to use button.
Button btn = (Button) findViewById(R.id.my_button);
4. ImageView: This view is used to display images on the screen. It can
be used for logos, icons, or any other visual content.
Example:
<ImageView
android:id=“@+id/my_image_view"
android:layout_width=“match_parent"
android:layout_height=“wrap_content”
android:scale_type=“CenterCrop”
android:src=“@drawable/my_image" />
Inside java or kotlin class files (i.e..for eg.MainActivity.java ) we need to
instantiate the view to use button.
ImageView iv = (ImageView) findViewById(R.id.my_image_view);
5. ProgressBar: This view is used to display a progress bar that shows
the progress of an ongoing task or operation. It can be used for file up-
loads, downloads, or any other long-running process.
Example:
<ProgressBar
android:id=“@+id/my_progress_bar”
android:layout_width=“wrap_content"
android:layout_height="wrap_content" />
These are some few examples of the different types of views that can be
used in an Android application. By combining these views and using vari-
ous layout techniques, developers can create a wide range of different user
interfaces and user experiences in their applications.
A real time example of the above said views is given below.
activity_dummy_layout.xml
<?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:padding="@dimen/padding"
android:orientation="vertical">
<TextView
android:id="@+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding"
android:textSize="@dimen/txtSize"
android:text="Hello, World!" />
<EditText
android:id="@+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding"
android:textSize="@dimen/txtSize"
android:hint="Enter your name" />
<Button
android:id="@+id/my_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding"
android:textSize="@dimen/txtSize"
android:textColorHint="#ffffff"
android:hint="Click Me" />
<ImageView
android:id="@+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding"
android:scaleType="centerCrop"
android:background="@drawable/ic_launcher_background"
android:src="@drawable/ic_launcher_foreground" />
<ProgressBar
android:id="@+id/my_progress_bar"
android:layout_margin="@dimen/padding"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Output: