Vous êtes sur la page 1sur 19

1.

ANDROID TEXT VIEW


Text View is used to display text in android layout

Android Simple Text View Example.


Android project name DemoText View.(For this go to file, then Android Project and give project name and set target of api level) Source Code:Main.xml is
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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="@string/hello" /> <TextView android:id="@+id/main_txtview1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="This is Example of Text View" android:textColor="#36ff00" /> </LinearLayout>

DemoTextViewActivity.java is
package com.android.demotextview; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class DemoTextViewActivity extends Activity { protected TextView _tv1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView _tv1=(TextView)findViewById(R.id.main_txtview1); } }

The output will be:-

2.ANDROID EDIT TEXT


Android Edit Text is simply like a text field, text area or a text input field in online forms.It is an editable TextView. EditText has following features like : EditText XML Layout Codes with Sample Input and Soft Keyboard Screen-shorts How Get Value from an EditText Programmatically How Assign values to an EditText How to Clear Value of an EditText. Setting MaxLength/Maximum Number of Characters that can be Entered to an EditText Limit digit/numeric characters that can be Entered to an EditText

Android Simple EditText Example.


Source code : Main.xml is
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"

android:orientation="vertical" android:background="@android:color/background_light"> <EditText android:id="@+id/main_edit1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16dp" /> <Button android:id="@+id/main_bt1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Display" /> </LinearLayout>

DemoEditText.java is package com.android.demoedittext; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class DemoEditText extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button _bt=(Button)findViewById(R.id.main_bt1); _bt.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub EditText _et=(EditText)findViewById(R.id.main_edit1); String s=_et.getText().toString(); Toast.makeText(getApplicationContext(), s+" hello", Toast.LENGTH_LONG).show(); } }); } }

The output will be:-

3.ANDROID IMAGE VIEW


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, and provides various display options such as scaling and tinting Source Code:Main.xml is ::<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@android:color/background_light" > <ImageView android:id="@+id/main_imgview" android:src="@drawable/sunset" android:layout_height="fill_parent" android:layout_width="fill_parent" android:scaleType="center" android:layout_gravity="center" /> </LinearLayout>

The output will be:-

4.ANDROID BUTTON
Source Code :Main.xml is ::<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="100px" android:layout_height="wrap_content" android:text="Button 1" /> <Button android:id="@+id/button2" android:layout_width="100px" android:layout_height="wrap_content" android:text="Button 2" /> </LinearLayout>

DemoButton.java is package com.android.demobutton;

package com.ButtonExample; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ButtonExample extends Activity { Button b1,b2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b1 = (Button) findViewById(R.id.button1); b2 = (Button) findViewById(R.id.button2); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast msg = Toast.makeText(getBaseContext(), "You have clicked Button 1", Toast.LENGTH_LONG); msg.show(); } }); b2.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast msg = Toast.makeText(getBaseContext(), "You have clicked Button 2", Toast.LENGTH_LONG); msg.show(); } }); } }

The output will be:-

5.ANDROID IMAGE BUTTON


In Android ,You can use android.widget.imagebutton to display a Button with a customize background image.Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button, with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src attribute in the XML element or by the setImageResource method. Source Code :Main.xml is ::<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@android:color/background_light"> <ImageButton android:id="@+id/imageButton1" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout>

DemoImageButtomActivity.java is :
package com.android.imageButton; import import import import import import android.app.Activity; android.os.Bundle; android.view.View; android.view.View.OnClickListener; android.widget.ImageButton; android.widget.Toast;

public class DemoImageButtomActivity extends Activity { /** Called when the activity is first created. */ ImageButton imageButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addListenerOnButton(); } public void addListenerOnButton() { imageButton = (ImageButton) findViewById(R.id.imageButton1); imageButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Toast.makeText(DemoImageButtomActivity.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show(); } }); } }

The output will be:-

6.ANDROID TOGGLE BUTTON


Source Code : Main.xml is ::<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/background_light" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ToggleButton android:id="@+id/togglebutton" android:layout_width="150px" android:layout_height="50px" android:textOn="ON" android:textOff="OFF" /> </LinearLayout>

DemoToggleButtonActivity.java is :
package com.android.Toggle; import import import import import import android.app.Activity; android.os.Bundle; android.view.View; android.view.View.OnClickListener; android.widget.Toast; android.widget.ToggleButton;

public class DemoToggleButtonActivity extends Activity { /** Called when the activity is first created. */ ToggleButton tb; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tb = (ToggleButton) findViewById(R.id.togglebutton); tb.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getBaseContext(), "Button is "+tb.getText().toString(), Toast.LENGTH_LONG).show(); } }); } }

The output will be:-

7.ANDROID RADIO BUTTON


Radio buttons are one of the common UI elements in android apps. Radio buttons allow the user to select an option from a set. There are two key classes related to radio buttons. RadioButton : The RadioButton has two states: either checked or unchecked. Once a RadioButton is checked, it cannot be unchecked. RadioGroup : A RadioGroup is used to group together one or more RadioButton views, thereby allowing only one RadioButton to be checked within the RadioGroup.

Source Code :Main.xml is ::<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@android:color/background_light"> <RadioGroup android:id="@+id/radioSex" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radioMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio_male" android:checked="true" />

<RadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio_female" /> </RadioGroup> <Button android:id="@+id/btnDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_display" /> </LinearLayout> DemoRadioButtonActivity.java is :package com.android.radio; import import import import import import import import android.app.Activity; android.os.Bundle; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.RadioButton; android.widget.RadioGroup; android.widget.Toast;

public class DemoRadioButtonActivity extends Activity { private RadioGroup radioSexGroup; private RadioButton radioSexButton; private Button btnDisplay; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addListenerOnButton(); } public void addListenerOnButton() { radioSexGroup = (RadioGroup) findViewById(R.id.radioSex); btnDisplay = (Button) findViewById(R.id.btnDisplay); btnDisplay.setOnClickListener(new OnClickListener() { public void onClick(View v) { // get selected radio button from radioGroup int selectedId = radioSexGroup.getCheckedRadioButtonId(); // find the radiobutton by returned id

radioSexButton = (RadioButton) findViewById(selectedId); Toast.makeText(getApplicationContext(), radioSexButton.getText(), Toast.LENGTH_SHORT).show(); } }); }}

The output will be:-

8.ANDROID CHECK BOX


Android Checkbox is used to select more than one option at a time. Example: In an admission from we may need to select both diploma & BE in graduation option.For this we can use Checkbox which will enable us to select multiple option.

Source Code :Main.xml is ::<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >

<CheckBox android:id="@+id/main_ch1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Diploma" /> <CheckBox android:id="@+id/main_ch2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="BE" /> <Button android:id="@+id/main_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Display" /> </LinearLayout>

DemoCheckBoxActivity.java is :package com.android.CheckBox; import import import import import import import android.app.Activity; android.os.Bundle; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.CheckBox; android.widget.Toast;

public class DemoCheckBoxActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button _btn=(Button)findViewById(R.id.main_btn); _btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub CheckBox _ch1=(CheckBox)findViewById(R.id.main_ch1); CheckBox _ch2=(CheckBox)findViewById(R.id.main_ch2); if (_ch1.isChecked()==true && _ch2.isChecked()==false) { Toast.makeText(getApplicationContext(), "Diploma", Toast.LENGTH_LONG).show(); } if (_ch1.isChecked()==false && _ch2.isChecked()==true) { Toast.makeText(getApplicationContext(), "BE", Toast.LENGTH_LONG).show();

} if (_ch1.isChecked()==true && _ch2.isChecked()==true) { Toast.makeText(getApplicationContext(), "Both Diploma and BE", Toast.LENGTH_LONG).show(); } if (_ch1.isChecked()==false && _ch2.isChecked()==false) { Toast.makeText(getApplicationContext(), "Nothing", Toast.LENGTH_LONG).show(); } } }); } }

The output will be:-

9.ANDROID SPINNER
This sample android program shows you how to use Spinner in Android. In this program a list is shown as a dropdown box. When you click on the list, the selected item is shown on the text view. You can use this ArrayAdapter widget and the Spinner object together with the onListItemClick() method to determine the selected index and process accordingly. The Project describes how to implement spinner(drop-down list) for your application.

Source Code :Main.xml is ::<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Spinner android:id="@+id/spin" android:layout_width="150px" android:layout_height="wrap_content" android:layout_gravity="center"> </Spinner> </LinearLayout>

DemoSpinnerActivity.java is :package com.android.DemoSpinner; import import import import android.app.Activity; android.os.Bundle; android.widget.ArrayAdapter; android.widget.Spinner;

public class DemoSpinnerActivity extends Activity { Spinner sp; ArrayAdapter<String> adapter; String numbers[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN" }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sp = (Spinner) findViewById(R.id.spin); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, numbers); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item ); sp.setAdapter(adapter); } }

The output will be:-

10.ANDROID PROGRESS BAR


Actions such as loading resources, downloading content, or sending messages can take time. When that action is measurable, you can make the user aware of how far along the operation is in the process. A moving progress bar also reassures users that the app is still operating properly. Even if the progress bar cant be tied to anything but a timer that updates the bar smoothly, the user will have something to look at while they are waiting. Well cover the vertical progress bar here, but there are several styles for the progress bar. For example, the indeterminate state for a progress widget can show that something is still happening.

Source Code :Main.xml is ::<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnStartProgress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Download File" /> </LinearLayout>

DemoProgressActivity.java is :package com.android.DemoProgerss; import import import import import import import android.app.Activity; android.app.ProgressDialog; android.os.Bundle; android.os.Handler; android.view.View; android.view.View.OnClickListener; android.widget.Button;

public class DemoProgressActivity extends Activity { Button btnStartProgress; ProgressDialog progressBar; private int progressBarStatus = 0; private Handler progressBarHandler = new Handler(); private long fileSize = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addListenerOnButton(); } private void addListenerOnButton() { // TODO Auto-generated method stub btnStartProgress = (Button) findViewById(R.id.btnStartProgress); btnStartProgress.setOnClickListener( new OnClickListener() { public void onClick(View v) { // prepare for a progress bar dialog progressBar = new ProgressDialog(v.getContext()); progressBar.setCancelable(true); progressBar.setMessage("File downloading ..."); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.show(); //reset progress bar status progressBarStatus = 0; //reset filesize fileSize = 0; new Thread(new Runnable() { public void run() { while (progressBarStatus < 100) { // process some tasks progressBarStatus = doSomeTasks();

// your computer is too fast, sleep 1 second try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // Update the progress bar progressBarHandler.post(new Runnable() { public void run() { progressBar.setProgress(progressBarStatus); } }); } // ok, file is downloaded, if (progressBarStatus >= 100) { // sleep 2 seconds, so that you can see the 100% try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } // close the progress bar dialog progressBar.dismiss(); } } }).start(); } }); } // file download simulator... a really simple public int doSomeTasks() { while (fileSize <= 1000000) { fileSize++; if (fileSize == 100000) { return 10; } else if (fileSize == 200000) return 20; } else if (fileSize == 300000) return 30; } else if(fileSize == 400000) { return 40; } else if (fileSize == 500000) return 50; } else if (fileSize == 600000)

{ {

{ {

return 60; } else if(fileSize == 700000) { return 70; } else if (fileSize == 800000) { return 80; } else if (fileSize == 900000) { return 90; } } return 100; }

The output will be:-

Vous aimerez peut-être aussi