Thursday 27 February 2014


How to Create simple Alert Dialog 





       public class Home extends Activity {

Button btnAlert, btnDate, btnTime, btnCustom;

final int D_ALERT = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

initialize();

btnAlert.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

showDialog(D_ALERT);

}
});

btnDate.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
DatePickerDialog date = new DatePickerDialog(Home.this,
new DateListener(), 2012, 7, 1);
date.show();
}
});

btnTime.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
TimePickerDialog time = new TimePickerDialog(Home.this,
new TimeListener(), 17, 45, false);
time.show();
}
});

btnCustom.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(Home.this);
dialog.setContentView(R.layout.cus_lay);
dialog.setTitle("BitCode");

final EditText edtUsername = (EditText) dialog
.findViewById(R.id.edtUsername);
Button btnLogin = (Button) dialog.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (edtUsername.getText().toString().equals("bitcode")) {
mt("login success");
dialog.dismiss();
} else {
mt("something wrong");
}

}
});
dialog.show();
}
});
}

class TimeListener implements OnTimeSetListener {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
btnTime.setText("hh: " + hourOfDay + " mi: " + minute);
}

}

class DateListener implements OnDateSetListener {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
btnDate.setText(dayOfMonth + " - " + (monthOfYear + 1) + " - "
+ year);

}

}

@Override
protected Dialog onCreateDialog(int id) {

mt("onCreateDialog called...");

if (id == D_ALERT) {
AlertDialog aDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(Home.this);
builder.setTitle("BitCode");
builder.setMessage("Are you sure?");
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("Yes", new YesListener());
builder.setNegativeButton("No", new YesListener());
builder.setCancelable(false);

aDialog = builder.create();
return aDialog;
}
return super.onCreateDialog(id);
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
mt("onPrepareDialog dialog called for id " + id);
if (id == D_ALERT) {
((AlertDialog) dialog).setMessage("this is a change message");
}
}

class YesListener implements DialogInterface.OnClickListener {

@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE)
mt("you said yes");
if (which == DialogInterface.BUTTON_NEGATIVE)
mt("you said no");

dialog.dismiss();
}

}

private void mt(String text) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}

private void initialize() {
btnAlert = (Button) findViewById(R.id.btnAlert);
btnDate = (Button) findViewById(R.id.btnDate);
btnTime = (Button) findViewById(R.id.btnTime);
btnCustom = (Button) findViewById(R.id.btnCustom);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}

}

activity_home xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

  
    <Button
        android:id="@+id/btnAlert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Alert Dialog" />

    <Button
        android:id="@+id/btnDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pick Date" />

    
    <Button
        android:id="@+id/btnTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pick Time" />
    
    <Button
        android:id="@+id/btnCustom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Dialog" />
    
    
</LinearLayout>

cus_lay xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <ImageView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/ic_launcher"/>
    
    <EditText android:layout_width="100px"
        android:layout_height="wrap_content"
        android:id="@+id/edtUsername"
        android:hint="username"/>
    
    <EditText android:layout_width="100px"
        android:layout_height="wrap_content"
        android:id="@+id/edtPassword"
        android:hint="password" android:password="true"/>
    
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:id="@+id/btnLogin" android:text="Login"/>
    </LinearLayout>






How to get Json Response by hitting Url




public class MainActivity extends Activity {

String url = "http://api.androidhive.info/contacts/";
String jsonResponse = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new Response().execute();

}

class Response extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {

try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpresponse = httpClient.execute(httpPost);
HttpEntity entity = httpresponse.getEntity();
jsonResponse = EntityUtils.toString(entity);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

}

}



Note:



  • Network operations should not be in userthread so that you have to use either async task or thread handler structure
  • Internet permission should be mentioned in android manifest file