Android UI Lecture Basic Widgets

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Mobile Application Development in

Android

Bangladesh-Korea Information Access Center (IAC)


Department of Computer Science and Engineering (CSE)
Bangladesh University of Engineering and Technology (BUET)
Basic Widgets

USER INTERFACES
TextView

TextViews are
typically used to
display a caption.

TextViews are not


editable, therefore
they take no input.
TextView Example

<TextView
android:id="@+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000ff"
android:padding="3dp"
android:text="Enter User Name"
android:textSize="16sp"
android:textStyle="bold"
android:gravity="center">
</TextView>
ImageView

Displays an arbitrary image, such as an icon.

The ImageView class can load images from


various sources (such as resources or content
providers),

Takes care of computing its measurement from


the image so that it can be used in any layout
manager

Provides various display options such as scaling


and tinting.
ImageView Example

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android"
/>
Button and ImageButton

Represents a push-button widget.

Push-buttons can be pressed, or clicked, by


the user to perform an action.

A button consists of text or an icon (or both


text and an icon) that communicates what
action occurs when the user touches it.
Button (Creating only with Text)

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
... />
Button (Creating only with Image)

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_icon"
... />
Button (Creating text Image)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:drawableLeft="@drawable/button_icon"
... />

• For positioning the image you can use


– android:drawableRight
– android:drawableLeft
– android:drawableTop
– android:drawableBottom
ToggleButton

Displays checked/unchecked states as a button with a "light" indicator


and by default accompanied with the text "ON" or "OFF".
ToggleButton Example

<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Activated"
android:textOff="Disabled"/>
EditText

Allows the user to type text into your app.

It can be either single line or multi-line. T

Touching a text field places the cursor and automatically displays the
keyboard.

In addition to typing, text fields allow for a variety of other activities,


such as text selection (cut, copy, paste) and data look-up via auto-
completion.
EditText Example

<EditText
android:id="@+id/txtUserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:hint="Name">
</EditText>

• Sp (Scale-independent Pixels)
– Used for specifying font size
– Like the dp unit, but it is also scaled by the
user's font size preference.
– It is used so that the font size ca be adjusted
for both the screen density and user's
preference.
EditText- Specifying a Keyboard Type

• EditText can have different input types, such as


number, date, password, or email address.
• The type determines what kind of characters are
allowed inside the field, and may prompt the virtual
keyboard to optimize its layout for frequently used
characters.
• You can specify the type of keyboard you want for
your EditText object with the android:inputType
attribute.
– For example, if you want the user to input an email address, you
should use the textEmailAddress input type
EditText: Keyboard Example

<EditText
android:id="@+id/email_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/email_hint"
android:inputType="textEmailAddress" />

The default text input type. The textEmailAddress


input type. The phone input type.
EditText Controlling Behavior with
inputType
• The android:inputType attribute allows bitwise
combinations so you can specify both a keyboard
layout and one or more behaviors at once.
Values Usage
none There is no content type. The text is not editable.
text Just plain old text.
textCapCharacters Can be combined with text and its variations to request
capitalization of all characters
textCapWords Can be combined with text and its variations to request
capitalization of the first character of every word.
textCapSentences Can be combined with text and its variations to request
capitalization of the first character of every sentence.
textMultiLine Can be combined with text and its variations to allow
multiple lines of text in the field. If this flag is not set,
the text field will be constrained to a single line.
EditText Controlling Behavior with
inputType
Values Usage
textUri Text that will be used as a URI.
textEmailAddress Text that will be used as an e-mail address.
textPostalAddress Text that is being supplied as a postal mailing address
textPassword Text that is a password
number A numeric only field
numberSigned Can be combined with number and its other options to
allow a signed number.
phone For entering a phone number
datetime For entering a date and time
date For entering a date
time For entering a time

You might also like