Friday 12 April 2013

Simple Image Viewer And Wallpaper Setter - Android

In this tutorial, we will see how to create an Image Viewer and to set Wallpaper for Android

Objectives:

1) Design the UI with one ImageView component and two Button Components
2) Button Next shows the next image and other button Set Wallpaper sets the current image as wallpaper
3) Errors are shown using Toast.makeToast() method

Steps:

1) Desgin the screen as follows:

2) Set the permission for using Wallpaper in AndroidManifest.xml file

<uses-permission android:name="android.permission.SET_WALLPAPER" >
</uses-permission>
3) Place the Image files under res\drawable-xhdpi folder

4) Implement the Next Button operation
int counter = 1;String currentImage = "";
public void showNextImage(View view) {
try {
ImageView iv = (ImageView) findViewById(R.id.
imageView1);
currentImage = "drawable/i" + counter;
iv.setImageDrawable(getResources().getDrawable(
getResources().getIdentifier(
currentImage, "drawable",
getPackageName())));
counter++;
if (counter > 6)
counter = 1;
}
catch (Exception e) {
Toast.makeText(
this, "Error Loading Image", Toast.LENGTH_SHORT)
.show();
}
}
5) Implement the set Wallpaper method

Handler
handler=new Handler();
public void setCurrentImageAsWallpaper(View view) {
Thread th =
new Thread() {
public void run() {
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(MainActivity.
this);
try {
wallpaperManager.setResource(
getResources().getIdentifier(
currentImage, "drawable",
getPackageName()));
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.
this, "Wallpaper set",
Toast.
LENGTH_SHORT).show();
}
});
}
catch (Exception e) {
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.
this,
"Error setting wallpaper",
Toast.
LENGTH_SHORT).show();
}
});
}
}
};
th.start();
}



Result:


Tuesday 9 April 2013

Creating Simple Maths Application for Android

This tutorial helps in understanding of

1) Designing Android application
2) Creating Input Forms
3) Handling events and updating the view

Steps:

1) Download the ADT (Android Developer Toolkit) from http://developer.android.com/sdk 
2) Unzip it to your favorite drive location
3) Start eclipse and follow the initials steps Described : http://developer.android.com/training/basics/firstapp/index.html 
4) Design the page as follows:
5) activity_main.xml file should look like below:

<
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp"
android:text="@string/first" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="19dp"
android:text="@string/second" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="14dp"
android:text="@string/addButton"
android:onClick="add"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/textView1"
android:text="@string/subtractButton"
android:onClick="sub" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="@string/ResultString" />
</RelativeLayout>
6) strings.xml should look as follows:
<?
xml version="1.0"
encoding="utf-8"?>
<
resources>
<string name="app_name">FirstApp</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="first">First Number</string>
<string name="second">Second Number</string>
<string name="addButton">Add</string>
<string name="subtractButton">Sub</string>
<string name="ResultString">Result</string>
</resources>
7) MainActivity.java

public
class MainActivity extends Activity {
private MathsService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main);
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.
main, menu);
return true;
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
builder.setMessage(
"Are you sure you want to exit?")
.setCancelable(
false)
.setPositiveButton(
"Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton(
"No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void add(View view){
EditText editText1 = (EditText)findViewById(R.id.
editText1);
EditText editText2 = (EditText)findViewById(R.id.
editText2);
int first = Integer.parseInt(editText1.getText().toString());
int second = Integer.parseInt(editText2.getText().toString());
service = new MathsService(new Add());
int result = service.performMathOperation(first, second);
TextView resultTxt = (TextView)findViewById(R.id.
textView3);
resultTxt.setText(
"Result: "+result);
}
public void sub(View view){
EditText editText1 = (EditText)findViewById(R.id.
editText1);
EditText editText2 = (EditText)findViewById(R.id.
editText2);
int first = Integer.parseInt(editText1.getText().toString());
int second = Integer.parseInt(editText2.getText().toString());
service = new MathsService(new Sub());
int result = service.performMathOperation(first, second);
TextView resultTxt = (TextView)findViewById(R.id.
textView3);
resultTxt.setText(
"Result: "+result);
}
}

The service layer can be implemented as per your choice.

Result: