Android Tutorials : Basic widgets


Published on

Tutorial 1 - Learning About Basic Widgets In Android

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Android Tutorials : Basic widgets

  1. 1. Android.Widgets Tutorial 1 : Empowering Android WidgetsBy: Mr.PrajyotMainkarMS Software Systems( BITS-Pilani)BE(Hons.) Computer Engineering , PMP( IIT Delhi) S
  2. 2. TextView – The Andro-Label The simplest widget is the label, referred to in Android as a TextView . Typically, these are used like labels in Android UICreate a new android project with following contents in main.xml and run this project on emulator <?xml version="1.0" encoding="utf-8"?> <TextViewxmlns:android="" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is sample demo of TextView" />
  3. 3. TextView – The Andro-LabelThe following output is obtained when you run the code via emulator.
  4. 4. Images – Fleeting with imagesAndroid has two widgets to help embed images in your activities: ImageViewand ImageButton. As the names suggest, they are image-based analogues to TextView and Button , respectively. <?xml version="1.0" encoding="utf-8"?> <ImageViewxmlns:android="" android:id="@+id/icon" android:layout_width="fill_parent" android:layout_height="fill_parent" android:adjustViewBounds="true" android:src="@drawable/logo" /> Note that, android:src takes path of image from the drawable folder(@ is the reference), so drag „n drop /paste any logo.png image to drawable folder.
  5. 5. Images – Fleeting with images The following output is obtained when you run the code via emulator.
  6. 6. EditText– Learning EditTextEditText is a thin veneer over TextView that configures itself to be editable. In the javafile (Src folder) import the file “import android.widget.EditText;”. Find the xml code below <?xml version="1.0" encoding="utf-8"?> <EditTextxmlns:android="" android:id="@+id/editfield" android:layout_width="fill_parent" android:layout_height="fill_parent" android:singleLine="false" />
  7. 7. EditText – Learning EditTextpackage com.EditTextDemo;import;import android.os.Bundle;import android.widget.EditText;public class EditTextDemoActivity extends Activity { /** Called when the activity is first created. */@OverrideEditText E; public void onCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);E.setText("This is sample edit text demo"); }}
  8. 8. CheckBox– Lets *Check*•isChecked(): Determines if the check box has been checked.•setChecked(): Forces the check box into a checked or unchecked•state.•toggle(): Toggles the check box as if the user checked it. <?xml version="1.0" encoding="utf-8"?> <CheckBoxxmlns:android="" android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This checkbox is unchecked" />
  9. 9. CheckBox – Lets *Check*package com.CheckBoxDemo;import;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;public class CheckBoxDemoActivity extends Activity implements CompoundButton.OnCheckedChangeListener {CheckBox checkbox;public void onCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main); checkbox=(CheckBox)findViewById(;checkbox.setOnCheckedChangeListener(this); }public void onCheckedChanged(CompoundButtonbuttonView,booleanisChecked) {if (isChecked) {checkbox.setText("This checkbox is: checked");}else {checkbox.setText("This checkbox is: unchecked"); } } }
  10. 10. CheckBox – Lets *Check*The following output is obtained when you run the code via emulator.
  11. 11. RadioButton – Tune Up •check(): Checks a specific radio button via its ID •clearCheck(): Clears all radio buttons, so none in the group are checked.•getCheckedRadioButtonId(): Gets the ID of the currently checked radio button (or -1 if none are checked). Most times RadioButton widgets are placed inside a RadioGroup . The RadioGroup indicates a set of radio buttons whose state is tied, meaning only one button in that group can be selected at any time. If you assign an android:id to yourRadioGroup in your XML layout, you can access the group from your Java code and
  12. 12. RadioButton – Tune Up•The XML Code is below. Make no changes to Java File : <?xml version="1.0" encoding="utf-8"?> <RadioGroup xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Goa" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Maharashtra" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Kerala" /> </RadioGroup>
  13. 13. Thank you..GET IN TOUCH– Tune Up RadioButton Phone: +91-9822987513 Email: