Wednesday, August 3, 2011

First Step to android!

ok! to get start with android you have the following things on your windows machine!
1. android SDK with certain library installed in it! that you can download from developer.android.com
and procedure for downloading lib. i will explain it later!

2. Eclipse basic version!

3.ADT plugin ! get a latest plugin

4. internet connection is must during first few stages of installation only!

Installation procedure
1. Extract the content of SDK , u will find SDK manager.exe (mind it u should have internet connection during this following phase), then let the program fetch certain information from the internet..... wait a while! after a certain moment you will get this




check android SDK platform tools and
SDK platform android 2.2
thats our only requirement currently! rest you can download like sample examples tat contain source codes of google sample application like bluetooth chat, games ,etc!

after this !

Note: during following process remove your internet cable or connection or u can say disable internet! otherwise it can cause certain errors!
open the Eclipse Go to Help-->Click on Install new software -->click on ADD --> name any thing like "android plugin"
the select archive and locate ADT plugin.zip


ok! now after your plugin get installed in your eclipse restart the Eclipse program!
after restart our next step is let the eclipse know where is the android sdk, so for tat Select Preference in window main menu of the Eclipse. click on android at left corner, locate the android SDK there!



now next step is we have to create android emulator ! so tat we can run and debug our programs!
select windows in eclipse main menu and click on android SDK and AVD manager there open virtual device and click on create AVD manager create emulator the way u want!

Installation process completed!







Sunday, July 31, 2011

How to persist the Dialog Box even after the Screen Orientation Change Android

I have read many articles about serious problem with dialog box that get disappear after screen orientation changes ! Actually the reason for the disappearance , when the screen orientation changes the Android sys. refresh the activity causing restart of activity class! But I have a solution for that hope u enjoy the matter!
Save state concept is used here, with android activity's override methods called
  1. Override the onRetainNonConfigurationInstance() method to return the Object you would like to retain.
  2. When your Activity is created again, call getLastNonConfigurationInstance() to recover your Object.
thats
public Object onRetainNonConfigurationInstance() {
final MyDataObject data = collectMyLoadedData();
return data;
}
This following lines should be declared in the on create method to get the state of the activity.  final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
data
= loadMyData();
}
I am posting the code Hope U understand more clear......
code having two layout file main.xml contain only one button  and second Layout file name contain two EDITTEXT and one button  http://developer.android.com/guide/topics/resources/runtime-changes.html      this is from where i got the info.... if u want to know more about this issue....  

package in.ultraneo.savestate;

import java.security.PublicKey;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SavestateinandroidActivity extends Activity {
/** Called when the activity is first created. */
Button btn1;
Button btn2;
EditText edit1,edit2;
String DIALOGSTATUS="0";
String restore = "";
CustomizeDialog custo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.button1);
MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
data = loadMyData();
new Toast(this).makeText(this, "there is no data",Toast.LENGTH_SHORT).show();
custo = new CustomizeDialog(this, R.layout.dialogstate);
DIALOGSTATUS="0";
edit1 = (EditText)custo.findViewById(R.id.editText1);
edit1.setText("");
edit2 = (EditText)custo.findViewById(R.id.editText2);
edit2.setText("");
btn2 = (Button)custo.findViewById(R.id.button1);
} else {
if(data.string3=="1")
{
DIALOGSTATUS="1";
restore = "1";
custo = new CustomizeDialog(this, R.layout.dialogstate);
edit1 = (EditText)custo.findViewById(R.id.editText1);
edit2 = (EditText)custo.findViewById(R.id.editText2);
edit1.setText(data.string1);
edit2.setText(data.string2);
custo.show();
}
}
custo.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
DIALOGSTATUS="1";
new Toast(getBaseContext()).makeText(getBaseContext(),"Show", 1000).show();
}
});
custo.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
DIALOGSTATUS="0";
edit1.setText("");
edit2.setText("");
restore ="0";
new Toast(getBaseContext()).makeText(getBaseContext(),"Dism", 1000).show();
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
restore = "1";
custo.show();
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
custo.dismiss();
}
});
}
});
}
private MyDataObject loadMyData() {
MyDataObject da = new MyDataObject();
return da;
}

public Object onRetainNonConfigurationInstance() {
final MyDataObject data = collectMyLoadedData();
return data;
}

private MyDataObject collectMyLoadedData() {
if(restore.equals("1")){
MyDataObject da = new MyDataObject();
da.string1 = edit1.getText().toString();
da.string2 = edit2.getText().toString();
da.string3 = DIALOGSTATUS;
return da;
}else
{
return null;
}
}

}






********************
package in.ultraneo.savestate;

public class MyDataObject {
String string1;
String string2;
String string3;
public MyDataObject loadMyData()
{
return this;
}

}