Android Simple Text View Example
Android Simple Text View Example
Android Simple Text View Example
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); } }
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(); } }); } }
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>
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(); } }); } }
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(); } }); } }
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(); } }); } }
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
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(); } } }); } }
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); } }
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; }