August 2013

Taking text input in Android via EditText

Objectives :

  • How to take input in Android?
  • How to take text input in Android?
  • How to use EditText in Android?
  • How to take multiline input in Android?

If you wish to take text input in Android, you can use <EditText> to take the text input in Android.

Here is the small example showing different uses of EditText. EditText can be also used to take multiline input as that of textarea.

[sourcecode language=”xml”]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First Name" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:id="@+id/EditTextFirstName"
android:hint="First Name" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cell No" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Cell No." />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email ID" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Email Name" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Message" />

<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5" />

</LinearLayout>

[/sourcecode]

Multiline Input in Android

If you need, you can use the same <EditText> to take multiline input as per the code snippet given below :

[sourcecode language=”xml”]
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5" />
[/sourcecode]

Here, android:inputType=”textMultiLine” will convert the single line text input into multi line input and android:lines=”5″ will give 5 (five) visible lines input.

Seminar on Android

[youtube:http://www.youtube.com/watch?v=aecJw6iPlno&feature=youtu.be]

Conducted 2 hour Seminar on Android at Shree Datta Meghe Polytechnic, Nagpur on 6th Aug. 2013 at 12 noon.

It was again great experience during the interaction with the students of SDMP, shared experiences and had a day long discussion with the students. Awesome experience.

Working with Layouts in Android

Objectives :

  • Which layouts does Android have?
  • How to implement a layout in Android?
  • How to use different layouts in Android?
  • How to use Linear Layout in Android?
  • How to use Relative Layout in Android?
  • How to use Absolute Layout in Android?
  • How to use Frame Layout in Android?
  • How to use Table Layout in Android?
  • How to use nested layouts in Android?
  • How to use a layout inside another layout in Android?

In Android, there are main five (5) layouts which are as follows;

  1. Linear Layout
  2. Relative Layout
  3. Absolute Layout
  4. Table Layout
  5. Frame Layout

Here I have used the same form controls/components in form to show how it looks in different layouts except Frame Layout.

Linear Layout in Android

Here I have used nested linear layout.

XML Code for Linear Layout :

[sourcecode language=”xml”]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:text="User Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

</LinearLayout>

[/sourcecode]

Here android:orientation=”vertical” will render controls in vertical manner and android:orientation=”horizontal” will render controls in horizontal manner.

Relative Layout in Android

Here in Relative layout we don’t need to nest layouts generally.

XML Code for Relative Layout :

[sourcecode language=”xml”]

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:id="@+id/userName"
android:text="First Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/editUserName"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/userName"
android:layout_below="@id/userName"/>

<EditText
android:id="@+id/editpassword"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editUserName"
android:layout_alignLeft="@id/editUserName"/>

<TextView
android:id="@+id/password"
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/editpassword"
android:layout_below="@id/editUserName" />

</RelativeLayout>

[/sourcecode]

Here we need to specify android:layout_toLeftOf=”@id/controlId”, android:layout_toRightOf=”@id/controlId”, android:layout_below=”@id/controlId”, android:layout_alignLeft=”@id/controlId”, etc. parameters so as to render the control at particular place.

Absolute Layout in Android

Here in Absolute Layout we need to specify exact pixel location or (x,y) coordinate where we need to render our controls.

XML Code for Absolute Layout :

[sourcecode language=”xml”]
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:layout_x="10px"
android:layout_y="110px"
android:text="User Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:layout_x="150px"
android:layout_y="100px"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_x="10px"
android:layout_y="160px"
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:layout_x="150px"
android:layout_y="150px"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</AbsoluteLayout>

[/sourcecode]

Table Layout in Android

Whenever you need to manage data in tabular manner, you can use Table Layout. Table Layout will help you to show data in rows and columns.
Here is the sample code showing how to use Table Layout :

[sourcecode language=”xml”]
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TableRow>

<TextView
android:text="User Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />

<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</TableRow>

<TableRow>

<TextView
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />

<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</TableRow>

</TableLayout>
[/sourcecode]

Here in Table Layout, we use <TableRow> tag to add contents to a row same as that of HTML.

Frame Layout in Android

Frame Layout can be used to display various contents such as text, images, videos, image galleries, etc.
Sample code for Frame Layout is as follows :

[sourcecode language=”xml”]<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:text="http://aatul.me"
android:textSize="24sp"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>

</FrameLayout>
[/sourcecode]

New Google Nexus 7 Giveaway

Title:  Enter Brand New Google Nexus 7 16GB Giveaway

This is your chance to enter to win one of the brand new Google Nexus 7 (16GB) tablets that was just announced on July 24th and they began taking orders on July 30th.  This tablet which has the best specs currently on the tablet market includes a 7.02” screen which has a 1920×1200 HD resolution which is an incredible 323 PPI.

Dual cameras now with a 1.2MP front facing and 5MP rear facing camera, the new Google Nexus 7 boasts up to 9 hours of active use battery life with it’s 3950mAh battery. It also has Qi compatible wireless charging built in.

Audio on the new Google Nexus 7 includes stereo speakers with surround sound, powered by Fraunhofer. The dual band Wi-Fi supports Wireless a/b/g/n but I would have loved to see Wireless a/c support which exists in the newest routers but client devices are still slow to support.

You also get:

  • microUSB
  • SlimPort™ enabled
  • 3.5mm audio
  • Microphone
  • Power and Volume buttons
    • GPS
    • Gyroscope
    • Accelerometer
    • Compass
    • Ambient Light

The New Google Nexus 7 in spec is better than the iPad mini in nearly every way including double the pixel resolution, equal in camera quality, 4x as much built in memory and a vastly superior processor. At a price that is still $100 cheaper than the iPad mini.

Enter Now for your Chance to Win a Google Nexus 7

Thanks to the following sponsors Dragon BloggerSBABZY, TechWalls, CravingTech, Techetron and The Game Technician we are able to co-host this giveaway and one lucky entrant will get a brand new Google Nexus 7 Tablet 16GB.

Win the Brand New Google Nexus 7 16GB
You need to enable javascript to enter this campaign !
Powered by PromoSimple.

You can increase your odds of winning by sharing your custom link in the PromoSimple giveaway and convincing others to sign up with your referral link, up to 250 bonus entries can be gained this way. Also it is setup so you can tweet from the widget 1x per day so you can get 30 extra entries if you come back and tweet everyday from the widget (bookmark this site). Remember to do all of the entries, thank our sponsors and if you do write a blog post and become a co-host remember to leave the post URL as your entry validation. As an additional benefit if you choose to become a co-host you can win $50 cash for hosting the winning entry even if you don’t win (or that in addition if you do win).

This giveaway is open to anyone, anywhere over the age of 18 where rules and restrictions apply. See contest rules for details, but if you live outside the U.S. you have a few options, one you can receive $229 cash via Paypal instead of the Google Nexus 7 due to the costs in shipping and limitations in shipping Internationally directly from Google’s site, you could gift it to someone else in the U.S. or if you are willing to pay for shipping I will order the Nexus 7 delivered to me and then ship the device Internationally to you as long as you are covering shipping costs. This will add time to the delivery as it has to ship to me and then ship overseas to you however.