How to Create a User Interface (UI) Using XML : Widgets

Written by:  • Edited by: Simon Hill
Updated Jul 5, 2011
• Related Guides: Android | Google | HTML

Now it's time to see the widgets we have available in the Android platform. In the last part of this series of articles, we saw the "layouts", containers for these widgets.

Once we have learned what layouts are, it's time to work with the widgets that are going to be inside the layouts. If you have worked with HTML this will be easy, if not...It will be easy anyway. We call a widget to every single element in the UI, as Buttons, Texts, Images, EditText.

We are going to work in XML, but we have to take into account that all we are going to do now, can be done using Java classes. Let's start.

Widgets

There are lots of widgets to use in our applications, now we are going to have a look to the most significant (in my opinion).

Button

This element is a …. button. The most simple Button has the following structure:

<Button

android:id="@+id/button01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button">

</Button>

Where android:id is the unique identifier of the element, and android:layout_width/layout_height are, as we could see in the last article http://www.brighthub.com/mobile/google-android/articles/22948.aspx is the size of the element. In the android:text attribute we set the text that its inside the Button.

We can have more attributes to configure our Button.

  • android:clickable → we set if the button reacts to click events
  • android:soundEffectsEnabled → We can set if this button have sounds effects when its clicked or touched

These are examples of attributes for Button widget. We can get more from this page:

http://code.google.com/intl/es-ES/android/reference/android/widget/Button.html

Most of the widget's attributes are shared because they have inherited them from more complex elements (Views).

TextViews

These are simple labels that hold text inside. I have created a more complex TextView element to see more than the basic attributes of it.

<TextView

android:id="@+id/TextView01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/black"

android:text="Here you can put whatever you want"

android:textSize="12sp"

android:typeface="sans"

android:textStyle="italic"

android:textAlign="center"

>

</TextView>

As we can see, we can configure the TextView to fit to our needs. We can change text size with android:textSize (hint! Its important to add the units to the values of the attributes, if we are specifying an element size, we have to put “px” (pixels) or whatever unit we are using). On the other hand if we want to change the face of the fonts inside the text, we can use android:typeface to change to “bold”, “italic” or both. We can align the text with textAlign or we can change the background of the widget and put an image (drawable) or a color with android:background.

EditText

This is just the same as TextView, but with the difference that the text that is inside the widget can be edited by the application user.

ImageView

With this widget we can add images to our applications

<ImageView

android:id="@+id/widget31"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

</ImageView>

The structure is, as you can see, the same as the other widgets. The image must be a drawable (image in the drawable folder).

Want to know more?

You can visit the Google widgets page to learn more about this. In the next articles we will see how to use an external application to create our user interface.


Comments

Showing all 18 comments
 
Faizan Jan 23, 2011 12:59 AM
Custom Shapes Buttons
Thanks Jbeerdev.

The photoshop part is already done.
So there is no custom shaped buttons or area defined button in android?

If this is no possible i will try to use transparent image buttons

something like this
http://images.crackberry.com/files/u7860/hexagon2.jpg
Jbeerdev Jan 22, 2011 2:23 PM
RE: How to Create a User Interface (UI) Using XML : Widgets
Hi Faizan.

The way I work with this kind of complex image-background is the following:

Splitting the image into the differente pieces you want to interact with the user. (for example, cutting the differente image buttons). Place the buttons (with it's own background) in the positions you consider, so the total composition is the one you want.

This is a "photoshop" or "gimp" work, more than android one... About putting a mask on the image, as far as I know, It's not possible.
Faizan Jan 22, 2011 2:14 PM
Custom Shapes Buttons
is this possible to use bacground image like this
http://media02.hongkiat.com/practical_web_interface_design/Volkswagen-Inspired-Navigation.jpg

and create buttons by making some mask on image??
barry Nov 29, 2010 6:52 PM
android:soundEffectsEnabled
how would you put in the android:soundEffectsEnabled , would it need a sound file inthe res >raw ?


and the http://code.google.com/intl/es-ES/android/reference/android/widget/Button.html

link does not work

thanks
Jbeerdev Feb 23, 2010 3:49 AM
RE: How to Create a User Interface (UI) Using XML : Widgets
Hi manoj

The "UI" elements are called "Activities". Every Activity has a UI interface (in xml) "attached" to it. And to go from Activity to Activity you have to use Intents:

Intent intent = new Intent(FromActivity, ToActivity.class);
startActivity(intent);
manoj Feb 23, 2010 3:39 AM
how to go from one UI to another UI
hey i am trying to develop an application for android. now my problem is to create a link from one UI to another UI. ie like if we click on button like CONTINUE it will go to next page...so in similar i need to do in my project.......so i need help from u....
brijesh Feb 11, 2010 11:22 PM
Autocomplete Text view
how to use Autocomplete Text view?
Jbeerdev Dec 27, 2009 1:25 PM
RE: How to Create a User Interface (UI) Using XML : Widgets
Hi Ajay

I have written something about the manifest in this article, but it is very basic. I have to update it with new content:

http://www.brighthub.com/mobile/google-android/articles/45445.aspx
Ajay Dec 24, 2009 12:20 AM
Detail
Hey can u plz explore more on manifest.xml .. it has got so many features and i m not getting how to use them.
Pals Nov 7, 2009 1:17 PM
Android
Gr8 work
sara Oct 28, 2009 3:50 PM
Android User Interface Design

Simple way to design,

<b>Android User Interface Design</b>
Jbeerdev Sep 14, 2009 10:44 AM
How to create UI with Java code
Here it is, just exit from the oven.

http://www.brighthub.com/mobile/google-android/articles/48845.aspx

Hope it helps!
Jbeerdev Sep 14, 2009 4:13 AM
Java and XML
I have just written an article about how to use Java code instead of XML to create UI. Soon it will be pubished.

About the string.xml file? Ok! Its very easy. I will write about this next.
Sridhar Sep 8, 2009 7:17 AM
RE: How to Create a User Interface (UI) Using XML : Widgets
Thanks a lot :)


You answered many questions that I had regarding

when to use xml and java in android

could u also write about the use of string.xml file
Pierre Aug 27, 2009 2:44 AM
Thanks for the articles so far!
Just wanted to say thanks for the articles so far. They are very clear and easy to follow. Hope you finish them to the point where you've created a finish application in this step by step way.
Jbeerdev Aug 21, 2009 1:07 PM
Soon
I will write about this in few days. :)
Björn Aug 21, 2009 12:20 PM
Next article?
When will you post the next article, im really enjoying this.
 
blog comments powered by Disqus
Email to a friend