Unit Iv
Unit Iv
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 1
UNIT-IV Designing User Interface with View
- This ‘view’ however comes along with a few properties bundled to them. Some of the important
and are often used to build up a complete meaningful screen design
1. id
2. width
3. height
4. margin
5. padding
“Id”
- This is basically the name of the particular view and will be used to refer that particular view
through out the project. It has to be unique(multiple views referencing to same id will confuse
the compiler).
- As the name of these properties suggest, these are the dimensions of that particular view. Now
these dimensions can be set using hard-coded values and it will adopt to them in most layouts,
but its not a very good design as the content inside them might get cropped or will have
unwanted spaces.
- Android provides two pre-defined options to set these dimensions — “match_parent” and
“wrap_content”.
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 2
UNIT-IV Designing User Interface with View
- Setting the dimensions to “match_parent” will make them equal to those of its parent’s
dimensions. If there is no parent to that particular view, it merely sets to the screen’s
dimensions (parent of a view is the U.I element in which it is housed in). And setting it to
“wrap_content” will force the view to adopt its dimensions according to its content.
“margin”
- This is the minimum distance that a view has to maintain from its neighbouring views. That’s it.
Since there are four sides to any view, the four margins corresponding to them are
“margin_left”, “margin_right”, “margin_top” and “margin_bottom”. If the same margin is
needed to be set on all sides, it can be set directly through “margin” property.
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 3
UNIT-IV Designing User Interface with View
“padding”
- The distance between the view’s outline and its content. Again similar to the “margin” property,
“padding” too has “padding_left”, “padding_right”, “padding_top”, “padding_bottom” and the
common padding to all sides can be set through “padding” property.
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 4
UNIT-IV Designing User Interface with View
- In Android, TextView displays text to the user and optionally allows them to edit it
programmatically. TextView is a complete text editor, however basic class is configured to not
allow editing but we can edit it.
- View is the parent class of TextView. Being a subclass of view the text view component can be
used in your app’s GUI inside a ViewGroup, or as the content view of an activity. We can create
a TextView instance by declaring it inside a layout(XML file) or by instantiating it
programmatically(Java Class).
Attributes of TextView:
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 5
UNIT-IV Designing User Interface with View
6. textStyle: textStyle attribute is used to set the text style of a text view. The possible text styles
are bold, italic and normal. If we need to use two or more styles for a text view then “|”
operator is used for that.
7. background: background attribute is used to set the background of a text view. We can set a
color or a drawable in the background of a text view.
8. padding: padding attribute is used to set the padding from left, right, top or bottom. In above
example code of background we also set the 10dp padding from all the side’s of text view
Example:
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 6
UNIT-IV Designing User Interface with View
EditText in Android
- In Android development, the EditText view is a UI element used for user input of text.
- We often use EditText in our applications in order to provide an input or text field, especially in
forms. The most simple example of EditText is Login or Sign-in form.
Attributes of EditText:
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 7
UNIT-IV Designing User Interface with View
Example:
<EditText
android:id = "@+id/editText"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:inputType = "text" />
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 8
UNIT-IV Designing User Interface with View
- There are different types of buttons used in android such as CompoundButton, ToggleButton,
RadioButton.
- AndroidButton is a subclass of TextView class and compound button is the subclass of Button
class. On a button we can perform different actions or events like click event, pressed event,
touch event etc.
1. id: id is an attribute used to uniquely identify a text Button. Below is the example code in which
we set the id of a Button.
2. gravity: The gravity attribute is an optional attribute which is used to control the alignment of
the text like left, right, center, top, bottom, center_vertical, center_horizontal etc.
3. text: text attribute is used to set the text in a Button. We can set the text in xml as well as in the
java class.
4. textColor: textColor attribute is used to set the text color of a Button. Color value is in the form
of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”.
5. textSize: textSize attribute is used to set the size of the text on Button. We can set the text size
in sp(scale independent pixel) or dp(density pixel).
6. textStyle: textStyle attribute is used to set the text style of a Button. The possible text styles are
bold, italic and normal. If we need to use two or more styles for a Button then “|” operator is
used for that.
7. background: background attribute is used to set the background of a Button. We can set a color
or a drawable in the background of a Button.
8. padding: padding attribute is used to set the padding from left, right, top or bottom. In above
example code of background, we also set the 10dp padding from all the sides of button.
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 9
UNIT-IV Designing User Interface with View
Example:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4CAF50"
android:text="Submit"
android:textColor="#fff "
android:textSize="24sp"/>
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 10
UNIT-IV Designing User Interface with View
android:inputType="textPassword"/>
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/et2"
android:layout_marginTop="40dp"
android:textSize="20sp"
android:text="Log In"
android:background="#5CCC6E"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot Password"
android:layout_below="@+id/b1"
android:layout_marginTop="20dp"
android:textColor="#0EE447"
android:textSize="16sp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
OUTPUT:
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 11
UNIT-IV Designing User Interface with View
- In Android, ImageButton is used to display a normal button with a custom image in a button.
- In simple words we can say, ImageButton is a button with an image that can be pressed or
clicked by the users.
- By default, it looks like a normal button with the standard button background that changes the
color during different button states.
- An image on the surface of a button is defined within a xml (i.e. layout ) by using src attribute or
within java class by using setImageResource() method. We can also set an image or custom
drawable in the background of the image button.
Attributes of ImageButton:
Example:
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc"/>
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 12
UNIT-IV Designing User Interface with View
- A toggle button in Android is a UI element that has two states: on and off.
- It's similar to a checkbox, but it has a different appearance and behavior.
- When a toggle button is on, it displays a different color or icon than when it's off, and it typically
triggers an action when the user toggles it.
- The most simple example of ToggleButton is doing on/off in sound, Bluetooth, wifi, hotspot etc.
It is a subclass of compoundButt
- To check current state of a toggle button programmatically we use isChecked() method. This
method returns a Boolean value either true or false. If a toggle button is checked then it returns
true otherwise it returns false
Attributes of ToggleButton:
1. checked: checked is an attribute of toggle button used to set the current state of a toggle
button. The value should be true or false where true shows the checked state and false shows
unchecked state of a toggle button. The default value of checked attribute is false.
2. textOn And textOff: textOn attribute is used to set the text when toggle button is in checked/on
state. We can set the textOn in XML textOn attribute is used to set the text when toggle button
is in checked/on state. We can set the textOn in XML as well as in the java class.
3. textColor: textColor attribute is used to set the text color of a toggle button. Color value is in the
form of “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb”
4. textSize: textSize attribute set the size of the text of a toggle button. We can set the text size in
sp(scale independent pixel) or dp(density pixel).
5. textStyle: textStyle attribute is used to set the text style of the text of a Toggle button. You can
set bold, italic and normal. If you need to use two or more styles for a text view then “|”
operator is used for that.
6. background: background attribute is used to set the background of a toggle button. We can set
a color or a drawable in the background of a toggle button.
7. padding: padding attribute is used to set the padding from left, right, top or bottom.
8. drawableBottom, drawableTop, drawableRight And drawableLeft: These attribute draw the
drawable below, top, right and left of the text of ToggleButton
Example:
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 13
UNIT-IV Designing User Interface with View
<ToggleButton
android:id="@+id/toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="On"
android:textOff="Off" />
RadioButton & RadioGroup In Android Studio
Attributes:
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 14
UNIT-IV Designing User Interface with View
7. textStyle: textStyle attribute is used to set the text style of the text of a Toggle button. You can
set bold, italic and normal. If you need to use two or more styles for a text view then “|”
operator is used for that.
8. background: background attribute is used to set the background of a toggle button. We can set
a color or a drawable in the background of a toggle button.
9. padding: padding attribute is used to set the padding from left, right, top or bottom.
10. drawableBottom, drawableTop, drawableRight And drawableLeft: These attribute draw the
drawable below, top, right and left of the text of ToggleButton
Example:
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 15
UNIT-IV Designing User Interface with View
- In Android, CheckBoxIn Android, CheckBox is a type of two state button either unchecked or
checked in Android
- It is a type of on/off switch that can be toggled by the users.
- You should use checkbox when presenting a group of selectable options to users that are not
mutually exclusive.
- CompoundButton is the parent class of CheckBox class.
Attributes:
Example:
<CheckBox
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple CheckBox"/>
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 16
UNIT-IV Designing User Interface with View
- ProgressBar is used to display the status of work being done like analyzing status of work or
downloading a file etc.
- In Android, by default a progress bar will be displayed as a spinning wheel but If we want it to be
displayed as a horizontal bar then we need to use style attribute as horizontal.
- It mainly use the “android.widget.ProgressBar” class.
- To add a progress bar to a layout (xmlTo add a progress bar to a layout (xml) file, you can use
the element.
- By default, a progress bar is a spinning wheel (an indeterminate indicator). To change to a
horizontal progress bar, apply the progress bar’s horizontal style.
Attributes:
- getMax() – returns the maximum value of progress bar We can get the maximum value of the
progress bar in java class. This method returns a integer value.
- getProgress() – returns current progress value We can get the current progress value from a
progress bar in java class. This method also returns a integer value.
Example:
<ProgressBar
android:id="@+id/simpleProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 17
UNIT-IV Designing User Interface with View
android:max="100"
style="@style/Widget.AppCompat.ProgressBar.Ho
rizontal"
android:progress="50"/>
Attributes:
Example:
String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 18
UNIT-IV Designing User Interface with View
android.R.layout.simple_list_item_1, items);
// Create a ListView object in the XML layout file and set its adapter to the ArrayAdapter
listView.setAdapter(adapter);
- In android GridView is a view group that display items in two dimensional scrolling grid (rows
and columns), the grid items are not necessarily predetermined but they are automatically
inserted to the layout using a ListAdapter.
- Users can then select any grid item by clicking on it.
- GridView is default scrollable so we don’t need to use ScrollView is default scrollable so we
don’t need to use ScrollView or anything else with GridVie
- GridView is widely used in android applications. An example of GridView is your default Gallery,
where you have number of images displayed using grid.
- Adapter Is Used To Fill Data In Gridview: To fill the data in a GridView we simply use adapter To
fill the data in a GridView we simply use adapter and grid items are automatically inserted to a
GridView using an Adapter To fill the data in a GridView we simply use adapter and grid items
are automatically inserted to a GridView using an Adapter which pulls the content from a source
such as an arraylist, array or database
- Important Note: numColumns property has to be specified otherwise GridView behaves like a
ListView with just singleChoice.
Attributes:
Example:
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 19
UNIT-IV Designing User Interface with View
<GridView
android:id="@+id/simpleGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:columnWidth="80dp"
android:listSelector="#0f0"/>
1. id: id is an attribute used to uniquely identify a image view id is an attribute used to uniquely
identify a image view in android.
2. src: src is an attribute used to set a source file or you can say image in your imageview to make
your layout attractive.
3. background: background attribute is used to set the background of a ImageView. We can set a
color or a drawable in the background of a ImageView.
4. padding: padding attribute is used to set the padding from left, right, top or bottom of the
Imageview.
- paddingRight: set the padding from the right side of the image view.
- paddingLeft: set the padding from the left side of the image view.
- paddingTop: set the padding from the top side of the image view.
- paddingBottom: set the padding from the bottom side of the image view.
- padding: set the padding from the all sides of the image
5. scaleType: scaleType is an attribute used to control how the image should be re-sized or moved
to match the size of this image view. The value for scale type attribute can be fit_xy,
center_crop, fitStart etc.
Example:
<ImageView
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion />
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 20
UNIT-IV Designing User Interface with View
- Horizontal ScrollView: In android, You can scroll the elements or views in both vertical and
horizontal directions. To scroll in Vertical we simply use ScrollView as we shown in the previous
code and to scroll in horizontal direction we need to use HorizontalScrollview.
- Below is HorizontalScrollView syntax in Android is:
-
- In Android, ImageView class is used to display an image file in application.
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 21
UNIT-IV Designing User Interface with View
Attributes:
ScrollView and HorizontalScrollView has same attributes, the only difference is scrollView scroll the child
items in vertical direction while horizontal scroll view scroll the child items in horizontal direction.
Important Method:
1. makeText(Context context, CharSequence text, int duration): This method is used to initiate
the Toast. This method take three parameters First is for the application Context, Second is text
message and last one is duration for the Toast.
Constants of Toast: Below is the constants of Toast that are used for setting the duration for the
Toast.
- LENGTH_LONG: It is used to display the Toast for a long period of time. When we set this
duration the Toast will be displayed for a long duration.
- LENGTH_SHORT: It is used to display the Toast for short period of time. When we set this
duration the Toast will be displayed for short duration.
2. show(): This method is used to display the Toast on the screen. This method is display the text
which we create using makeText() method of Toast.
3. setText(CharSequence s): This method is used to set the text for the Toast. If we use makeText()
method and then we want to change the text value for the Toast then we use this method.
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 22
UNIT-IV Designing User Interface with View
4. setDuration(int duration): This method is used to set the duration for the Toast. If we use
makeText() method and then we want to change the duration for the Toast then we use this
method.
Example:
<ImageView
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion />
- In Android, TimePicker is a widget used for selecting the time of the day in either AM/PM mode
or 24 hours mode.
- The displayed time consist of hours, minutes and clock format. If we need to show this view as a
Dialog then we have to use a TimePickerDialog class.
Important Method:
1. setCurrentHour(Integer currentHour): This method is used to set the current hours in a time
picker.
2. setCurrentMinute(Integer currentMinute): This method is used to set the current minutes in a
time picker.
3. getCurrentHour(): This method is used to get the current hours from a time picker.
4. getCurrentMinute(): This method is used to get the current minutes from a time picker.
5. setIs24HourView(Boolean is24HourView): This method is used to set the mode of the Time
picker either 24 hour mode or AM/PM mode. In this method we set a Boolean value either true
or false. True value indicate 24 hour mode and false value indicate AM/PM mode.
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 23
UNIT-IV Designing User Interface with View
6. is24HourView():This method is used to check the current mode of the time picker. This method
returns true if its 24 hour mode or false if AM/PM mode is set.
Attributes:
Example:
- In your layout XML file, add the following code to include the TimePicker widget:
- You can set an initial time for the TimePicker using the setCurrentHour and setCurrentMinute
methods:
- To get the selected time from the TimePicker, you can use the getHour and getMinute methods:
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 24
UNIT-IV Designing User Interface with View
- In Android, DatePicker is a widget used to select a date. It allows to select date by day, month
and year in your custom UI (user interface).
- If we need to show this view as a dialog then we have to use a DatePickerDialog class. For
selecting time Android also provides timepicker to select time.
Important Method:
1. setSpinnersShown(boolean shown): This method is used to set whether the spinner of the date
picker in shown or not. In this method you have to set a Boolean value either true or false. True
indicates spinner is shown, false value indicates spinner is not shown. Default value for this
function is true.
2. getDayOfMonth(): This method is used to get the selected day of the month from a date picker.
This method returns an integer value.
3. getMonth(): This method is used to get the selected month from a date picker. This method
returns an integer value.
4. getYear(): This method is used to get the selected year from a date picker. This method returns
an integer value.
5. getFirstDayOfWeek(): This method is used to get the first day of the week. This method returns
an integer value.
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 25
UNIT-IV Designing User Interface with View
Attributes:
Example:
Android Language by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-7743909870) 26