+ All Categories
Home > Documents > (Standard Widgets) a

(Standard Widgets) a

Date post: 24-Nov-2015
Category:
Upload: cshashank1992
View: 19 times
Download: 0 times
Share this document with a friend
Description:
android
Popular Tags:
42
Android development Standard Android Controls (TextView,EditText and ImageView)
Transcript

PowerPoint Presentation

Android development

Standard Android Controls(TextView,EditText and ImageView)

Displaying Text to Users with TextViewOne of the most basic user interface elements, or controls, in the Android SDK is the TextView control. We use it simply, to display fixed text strings or labels.As with most of the user interface elements, it is derived from the class View and is within the android.widget package.

General Properties Of TextView1. The text PropertyTo display text in TextView we use its android:text attribute in XML and there are 2 ways to set it:As raw text

General Properties Of TextViewb. Via strings.xml

Creating Contextual Links in TextView

Creating Contextual Links in TextViewFollowing are the values for the autoLink attribute: none: Disables all linking. web: Enables linking of URLs to web pages. email: Enables linking of email addresses to the mail client with the recipient filled. phone: Enables linking of phone numbers to the dialer application with the phone number filled out, ready to be dialed. map: Enables linking of street addresses to the map application to show the location. all: Enables all types of linking.

Creating Contextual Links in TextViewTo set the autoLink property programmatically we call the setAutoLinkMask() method on the TextView object. Eg:tv.setAutoLinkMask(Linkify.PHONE_NUMBERS|Linkify.WEB_URLS);This method accepts the integer argument which are the static members of the class LinkifyIts possible values are PHONE_NUMBERS,WEB_URLS,MAP_ADDRESSES,EMAIL_ADDRESSES,ALLRetrieving Data from Users with EditTextThe Android SDK provides a number of controls for retrieving data from users. The Android SDK provides a convenient control called EditText to handle text input from a user. The EditText class is derived from TextView. In fact, most of its functionality is contained within TextView but is enabled when created as an EditText. Creating An EditText ControlTo define an EditText control in an XML layout file we can use the following code:

Creating An EditText ControlIn the previous code a property worth noting is: android:hint This property puts some text in the edit box that goes away when the user starts entering text .Allowing User to Type Multiple LinesTo let the user type multiple lines of input we need to set the inputType attribute of EditText

Restricting User Input To DigitsTo restrict the user input to digits only we can use the android:digits property. If this is not set, the entry field allows the user to enter text as well. However, setting a size allows the user to input numbers only

Accepting Passwords In EditTextTo provide a password interface to EditText we can set its android:inputType property. The value given will be textPassword

Accepting Passwords In EditTextTo set it via java code we need to call the setInputType( ) method on EditText object

ed.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);This method receives an integer argument which is framed by btiwise ORing members of InputType classCapitalizing User InputIn order to capitalize what user types in an EditText we can use its capitalize property

Other possible values are none,words,sentences

Two Useful Methods Of EditTextThe EditText control provides us 2 very useful methods using which we can get/set text in EditText.These are :public Editable getText()(for obtaining contents of EditText)public void setText(CharSequence)(for setting contents of EditText)

The Toast ClassA toast is a view containing a quick little message for the user. The toast class helps us create and show those.When the view is shown to the user,it appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two examples are the volume control, and the brief message saying that your settings have been saved.

How To Create Toast ?To create a new Toast, we call a static method on theToastclass named makeText. There are two differentmakeTextmethods. One of them takes in a String that can be from any source you want. The second expects a String resource ID.

public static Toast makeText (Context , CharSequence , int )To make a standard toast that just contains a text view with the text as a constant String.Parameter Desc:1.Reference to current Context2.Message To Show3.Duration

1.First argument can be this or getApplicationContext( )2.Second argument can be any message3. Third argument are any of following 2 constants:public static final int LENGTH_LONG : Show the view or text notification for a long period of time.public static final int LENGTH_SHORT :Show the view or text notification for a short period of time.

The Code Toast t;T=Toast.makeText(this,Hello,Toast.LENGTH_LONG);How To Show Toast ?To show Toast Messages we need to call its method show()Its prototype is public void show ()The Code:Toast t;t=Toast.makeText(this,Hello,Toast.LENGTH_LONG);t.show( );

Other Methodspublic void setText (CharSequence) To update the text in a Toast that was previously created using one of the makeText() methods.public void setDuration (int duration) To set how long to show the message for.

Displaying ImagesWhen it comes to displaying images, Android supports three common image formats: PNG JPG GIF. The images for the Android application are stored in the directory res/drawableDisplaying ImagesWhile creating a new application, ADT creates several folders: drawable-ldpi drawable-mdpi drawable-hdpi drawable-xhdpi and drawable-xxhdpiEach directory is meant for storing images of different screen resolutions. Displaying ImagesTo support devices of different densities, we can store the images of low, medium, high, and extra high resolutions in these folders.

The images with resolutions of 480dpi,320dpi, 240dpi, 160dpi, and 120dpi and sizes 144*144px, 96x96px, 72x72px, 48x48px, and 36x36px are usually stored in the res/drawable-xxhdpi, res/drawable-xhdpi res/drawable-hdpi, res/drawable-mdpi, and res/drawable-ldpi folders, respectively

Adding ImagesCopy images to any of resource folders. All image filenames should be lowercase and contain only letters, numbers, and underscores. After we add images to the res/drawable folders, the gen folder is regenerated where the R.java file resides. The R.java file includes a reference to the newly added image and hence can be used in the layout file or other Java code. The syntax for referencing the image in the layout file is @drawable/image_filenameIn Java code, the image can be referenced using the following syntax: R.drawable.image_filename

The ImageView ControlThe class used to display images is android.widget.ImageViewThe 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 managerSteps Reqd To display Image

1. Store the image in respective folder.2. Add the ImageView widget to XML file3.Set the src attribute of ImageView to the image to be displayed. Its syntax is:android:src=@drawable/For eg:android:src=@drawable/smileySteps Reqd To display Image4. To programmatically set the image we call the setImageResource() method of ImageView whose protoype is:

public void setImageResource (int resId)


Recommended