Showing posts with label Button events. Show all posts
Showing posts with label Button events. Show all posts

Friday, 6 September 2013

Set Wallpaper in Android

Easy Way to Android Set Wallpaper


There is a easy way to set the wallpaper from the resources folder of your application to the android smartphone.You can set it through program.


1. Add the permissions in the Android Manifest file.

<uses-permission android:name="android.permission.SET_WALLPAPER" />

2. MainActivity.java

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.app.WallpaperManager;
import android.view.Menu;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  //android wallpaper manager
  WallpaperManager myWallpaperManager = WallpaperManager
    .getInstance(getApplicationContext());
  try {
   //set wallpaper picture from resource here
   myWallpaperManager.setResource(R.drawable.android_wallpaper);
   Toast.makeText(getApplicationContext(),"Success set as wallpaper",Toast.LENGTH_SHORT).show();
  } catch (IOException e) {
   Toast.makeText(getApplicationContext(),"Error on setting as wallpaper",Toast.LENGTH_SHORT).show();
  }
 }

}   

3. Now run your Application.(Dont Forget to put an image in res/drawable folder named android_wallpaper)

Thank you.

Thursday, 5 September 2013

Multiple Button Click Listener

Multiple Button Listener

Now we'll learn how to manage Multiple Button click Event Listener.
As you have learn the single button click event in previous tutorial.
Now we'll Move to the Multiple Buttons Tutorial.

1.activity_main.xml

<LinearLayout 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" 
    android:orientation="vertical">

    <Button

        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test1" />
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test2" />
    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test3" />
    <Button
        android:id="@+id/btn4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test4" />


</LinearLayout>


2.MainActivity.java

package com.sri.buttontest;

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 MainActivity extends Activity implements OnClickListener{

Button btn1,btn2,btn3,btn4;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
}

@Override

public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn1:
Toast.makeText(getApplicationContext(), "test1 is clicked", Toast.LENGTH_LONG).show();
break;
case R.id.btn2:
Toast.makeText(getApplicationContext(), "test2 is clicked", Toast.LENGTH_LONG).show();
break;
case R.id.btn3:
Toast.makeText(getApplicationContext(), "test3 is clicked", Toast.LENGTH_LONG).show();
break;
case R.id.btn4:
Toast.makeText(getApplicationContext(), "test4 is clicked", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}

}
3. That's all.Run the Project Now.
Comments and doubts are encouraged.
Thank you.

Simple Button Listener

Button Event Listener


New for Android, then go step by step.
First we will learn the button click event.

First write the xml file in new project.

1.activity_main.xml

<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" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="test" />

</RelativeLayout>



MainActivity.java

package com.sri.buttontest;

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 MainActivity extends Activity {
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Test is clicked",
Toast.LENGTH_LONG).show();
}
});
}

}


3.Now this is how the button listener works.
Thank you.