Friday, February 18, 2011

How to set action to the button ?


1) Create new Android Project

2) Click main.xml and add new button to layer


3) If you want to change button variable name right click on that button and select Edit ID and then set variable name

4) If you want to add new text to button right click on that button and select Edit Text. After click that you can see below window.

select string -> new String

Click Ok and use new sting to your button

5) Then add below code to your java class as below shown

package lk.cipher.tt;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class TT extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button b=(Button)findViewById(R.id.btnTest);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast("Button Clicked");
}
});
}


//show the selected radio channel
private void showToast(String text){
Toast.makeText(TT.this, "" + text, Toast.LENGTH_LONG).show();
}

}

When you click button it display your message "Button Clicked ".

sameeraa4ever

Tuesday, February 15, 2011

How to create Loading dialog box


If you want to display progress bar with the dialog box you can use ProgressDialog.

ex-

package com.load;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;

public class loading_box extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProgressDialog pdialog=new ProgressDialog(this);
pdialog.setCancelable(true);
pdialog.setMessage("Loading ....");
pdialog.show();
setContentView(R.layout.main);
}
}

Sameeraa4ever

How to create Yes, No Alert boxes


If you want to confirmation alert, you can use setPositiveButton() and setNagativeButton().

ex-

package com.yesno;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class yesnoalert extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder alert_box=new AlertDialog.Builder(this);
alert_box.setIcon(R.drawable.icon);
alert_box.setMessage("This is Yes no alert box");
alert_box.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Yes Button Clicked",Toast.LENGTH_LONG).show();
}
});
alert_box.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "No Button Clicked", Toast.LENGTH_LONG).show();
}
});
alert_box.show();
setContentView(R.layout.main);
}
}

If You want to add Title to that alert box add below line

alert_box.setTitle("Confirm Alert");


Sameera Nadirani Handapangoda

How to create Alert boxes


There are four type of alert boxes are available. Here I describe how to create simple alert box.

package com.alt; // package name

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class alertBox extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

AlertDialog.Builder altDialog= new AlertDialog.Builder(this);
altDialog.setMessage("Alert Dialog Message"); // here add your message
altDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Ok button Clicked ", Toast.LENGTH_LONG).show();
}
});
altDialog.show();

}
}


After Ok button Press it will display below screen.



Sameeraa4ever

Monday, February 14, 2011

Remove Auto focus from Edit Text



When start Android application, it always auto focus EditText box. Here we give explain how to remove auto focus from EditText.


Add your main.xml file to below additional code.




After add above code, you can remove auto focus from EditText.



Sameeraa4ever

Creating First Android Project


Go to Project File >> New >> Project
Sselect Android Project from Android
Now you can see below window and then enter below details


Project Name: AndroidHelloWorld

Package Name: com.android

Activity Name: HelloWorld

Application Name: My Hello World App

select target android version ex : android 2.3.1 API Level 9

Now click finish button

Then Run your application clicking Run button


Android with Eclipse


Download and Setup New Eclipse Classic 3.5.2
Then install android Development Tools(ADT). Open eclipse and go to Help>Install New Software..

It will appear below window , then enter https://dl-ssl.google.com/android/eclipse/ and click on Next to start installation. After installation the Eclipse IDE would require a restart.

Installing google android SDK

Download Android SDK
Go to eclipse Window > Preferences
Click android link and browse android SKD location from your directory

Add Android Platform

Install one or more Android Platforms like 2.3,2.2, 2.1 or some old.
Go to Window > Android SDK and AVD Manager
Go to Available Packages and select at least oneAndroid Platform
Click on Install Selected button:


Create New Android Virtual Device

Go to Window > Android SDK and AVD Manager
Sselect Virtual Devices
Click on New to create a Virtual Device, give it some Name
Select Target Android Platform

Sameeraa4ever