Android Lab Exercises
Android Lab Exercises
Android Lab Exercises
MainActivity.java
package com.saya.seekbar;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
Output:
ListView
MainActivity.java
package com.e.listview;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
et1=(EditText) findViewById(R.id.editText1);
et2=(EditText) findViewById(R.id.editText2);
et3=(EditText) findViewById(R.id.editText3);
rg=(RadioGroup) findViewById(R.id.radioGroup1);
rb0=(RadioButton) findViewById(R.id.radio0);
rb1=(RadioButton) findViewById(R.id.radio1);
rb2=(RadioButton) findViewById(R.id.radio2);
rb3=(RadioButton) findViewById(R.id.radio3);
rg.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int arg1) {
// TODO Auto-generated method stub
float n1, n2, n3=0;
n1=Float.parseFloat(et1.getText().toString());
n2=Float.parseFloat(et2.getText().toString());
if(arg1 == rb0.getId())
n3=n1+n2;
else if(arg1 == rb1.getId())
n3=n1-n2;
else if(arg1 == rb2.getId())
n3=n1*n2;
else if(arg1 == rb3.getId())
n3=n1/n2;
et3.setText(n3+"");
}
}
File Read and Write
MainActivity.java
package com.ic.filereadwrite;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
et=(EditText)findViewById(R.id.editText1);
btnsave=(Button)findViewById(R.id.btnsave);
btnload=(Button)findViewById(R.id.btnload);
btnclear=(Button)findViewById(R.id.btnclear);
btnclose=(Button)findViewById(R.id.btnclose);
btnsave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
FileOutputStream fos=openFileOutput("myfile", MODE_PRIVATE);
fos.write(et.getText().toString().getBytes());
fos.close();
Toast.makeText(getApplicationContext(), "File Saved", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
btnclear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et.setText("");
}
});
btnload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
BufferedReader reader = new BufferedReader(new
InputStreamReader(openFileInput("myfile")));
String textline;
StringBuffer strbuffer=new StringBuffer();
while( (textline=reader.readLine()) != null){
strbuffer.append(textline+"\n");
}
et.setText(strbuffer.toString());
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace(); }
}
});
btnclose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
}
}
IntentActivity
MainActivity.java
package com.saya.intentapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
SecondActivity.java
package com.saya.intentapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
SecondActivity.java
package com.saya.splashscreen;
import android.os.Bundle;
import android.app.Activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Alert Dialog
MainActivity.java
package com.ic.alertdialogs;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn3=(Button)findViewById(R.id.button3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1:
showdialog1();
break;
case R.id.button2:
showdialog2();
break;
case R.id.button3:
showdialog3();
break;
}
}
private void showdialog1() {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Confirm Delete");
builder.setMessage("Are You sure to Delete?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Yes is Selected",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "No is Selected",
Toast.LENGTH_SHORT).show();
}
});
AlertDialog mydialog = builder.create();
mydialog.show();
}
private void showdialog2() {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
final String[] colors={"Red","Green","Blue","Cyan","Yellow","Pink"};
builder.setTitle("Select a Color ");
builder.setItems(colors, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), colors[which],
Toast.LENGTH_SHORT).show();
}
});
AlertDialog mydialog = builder.create();
mydialog.show();
}
private void showdialog3() {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
final String[] colors={"Red","Green","Blue","Cyan","Yellow","Pink"};
builder.setTitle("Select a Color ");
builder.setSingleChoiceItems(colors, -1, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), colors[which],
Toast.LENGTH_SHORT).show();
}
});
AlertDialog mydialog = builder.create();
mydialog.show();
}
}
Tab Widget Talking Clock
MainActivity.java
package com.ic.talkingclock;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import java.util.Calendar;
import android.app.Activity;
import android.app.TabActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AnalogClock;
import android.widget.TabHost;
import android.widget.Toast;
mytabHost.addTab(mytabHost.newTabSpec("tab1").setIndicator("AnalogClock").setContent(
R.id.tab1));
mytabHost.addTab(mytabHost.newTabSpec("tab2").setIndicator("DigitalClock").setContent
(R.id.tab2));
ac=(AnalogClock)findViewById(R.id.analogClock1);
tts=new TextToSpeech(this, this);
ac.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
h=c.get(Calendar.HOUR);
m=c.get(Calendar.MINUTE);
s=c.get(Calendar.SECOND);
String t="The time is "+h+" Hours"+m+" Minutes"+s+" Seconds";
Toast.makeText(getApplicationContext(), t, Toast.LENGTH_LONG).show();
tts.speak(t, TextToSpeech.QUEUE_FLUSH, null);
}
});
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
}
Multi Language List
MainActivity.java
package com.saya.multi;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
lv=(ListView)findViewById(R.id.listView1);
final ArrayAdapter<CharSequence>
myadapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.days ,android.R.layout.simple_list_item_1);
lv.setAdapter(myadapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
myadapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
}
}
WebView
MainActivity.java
package com.saya.webview;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.NumberPicker;
String[] myarray={"Mypage","Google","Yahoo","Bing"};
np=(NumberPicker) findViewById(R.id.numberPicker1);
np.setDisplayedValues(myarray);
np.setMinValue(0);
np.setMaxValue(myarray.length-1);
wv=(WebView) findViewById(R.id.webView1);
btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int choice=np.getValue();
if(choice==0)
wv.loadUrl("file:///android_asset/mypage.html");
else if(choice==1){
wv.setWebViewClient(new WebViewClient());
wv.loadUrl("https://www.google.co.in");
}
else if(choice==2)
wv.loadUrl("https://www.yahoo.com");
else if(choice==3)
wv.loadUrl("https://www.bing.com");
}
});
}
}
Tween Animation
MainActivity.java
package com.pa.tween;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
Anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="4000"
android:startOffset="2000"/>
</set>
Tab Widget Talking Clock
MainActivity.java
package com.pa.talkingclock;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AnalogClock;
import android.widget.TabHost;
import android.widget.Toast;
myhost.addTab(myhost.newTabSpec("tab1").setIndicator("AnalogueClock").setContent(R.id
.tab1));
myhost.addTab(myhost.newTabSpec("tab2").setIndicator("DigitalClock").setContent(R.id.
tab2));
}
@Override
public void onClick(View v) {
Calendar c=Calendar.getInstance();
h=c.get(Calendar.HOUR);
m=c.get(Calendar.MINUTE);
s=c.get(Calendar.SECOND);
String st="The time is "+String.valueOf(h)+"Hours"+String.valueOf(m)
+"minutes"+String.valueOf(s)+"seconds";
Toast.makeText(getApplicationContext(),st,Toast.LENGTH_SHORT).show();
tts.speak(st, TextToSpeech.QUEUE_FLUSH, null);
}
}
Shared Preference
MainActivity.java
package com.ic.sharedpreference;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.widget.EditText;
et1=(EditText) findViewById(R.id.editText1);
et2=(EditText) findViewById(R.id.editText2);
SharedPreferences settings = getSharedPreferences("MYPREFS", 0);
et1.setText(settings.getString("name", ""));
et2.setText(settings.getString("email", ""));
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences settings = getSharedPreferences("MYPREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("name", et1.getText().toString());
editor.putString("email",
et2.getText().toString());
editor.commit();
}
}
GridView
MainActivity.java
package com.example.exgridview;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MainActivity extends ActionBarActivity {
GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv=(GridView)findViewById(R.id.gridView1);
gv.setAdapter(new myadapter(this));
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent i=new Intent(getApplicationContext(),FullScreenActivity.class);
i.putExtra("id", arg2);
startActivity(i);
}
});
}
}
myadapter.java
package com.example.exgridview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
public class myadapter extends BaseAdapter implements ListAdapter {
private Context context;
public Integer[]
myimages={R.drawable.pic_1,R.drawable.pic_2,R.drawable.pic_3};
public myadapter(Context c) {
context=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myimages.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv=new ImageView(context);
iv.setImageResource(myimages[arg0]);
iv.setLayoutParams(new GridView.LayoutParams(200,200));
return iv;
}
}
Fullscreenactivity.java
package com.example.exgridview;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new
PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.full_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_full_screen,
container, false);
return rootView;
}
}