Thursday, February 7, 2013

Custom Alert with extending Dialog in android


                 Create a your own class For alert in android Dynamically and save memory and learn java


----------------------------------------------
Code
----------------------------------------------

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;



/**
 *
 * @author Sudhi.S
 *
 */
public class AlertTwoButton extends Dialog implements OnClickListener {
Context mcontext = null;

public AlertTwoButton(Context context) {
super(context, android.R.style.Theme_Panel);
mcontext = context;
initialization();
}

boolean multi = false;
boolean edit = false;
String msg10=null;
public AlertTwoButton(Context context, boolean edit,boolean multi,String msg) {
super(context, android.R.style.Theme_Panel);
mcontext = context;
this.edit=edit;
this.multi = multi;
msg10=msg;
initialization();
}

RelativeLayout mreal = null;

protected void initialization() {
mreal = new RelativeLayout(mcontext);
RelativeLayout.LayoutParams mrealp = new RelativeLayout.LayoutParams(
-1, -2);
mreal.setBackgroundResource(R.drawable.abs__dialog_full_holo_light);
this.addContentView(mreal, mrealp);
}

public void showDoubleButtonAlert(String mtitle, String mmsg, Integer mres,
String button1, String button2) {
if (mtitle != null && mmsg != null && mres != null) {

RelativeLayout.LayoutParams mrealp12 = new RelativeLayout.LayoutParams(
-1, -1);
mrealp12.setMargins(10, 10, 10, 10);
LinearLayout ll0 = new LinearLayout(mcontext);
ll0.setOrientation(LinearLayout.HORIZONTAL);
ll0.setLayoutParams(mrealp12);
ll0.setPadding(5, 5, 5, 5);
ImageView img = new ImageView(mcontext);
img.setLayoutParams(new LinearLayout.LayoutParams(-2, -1));
img.setImageResource(mres);
ll0.addView(img);
TextView txt = new TextView(mcontext);
txt.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
txt.setText(mtitle);

txt.setTextColor(Color.BLACK);
ll0.addView(txt);



LinearLayout.LayoutParams lop = new LinearLayout.LayoutParams(-1,
-2);
LinearLayout ll7 = new LinearLayout(mcontext);
lop.weight = 1;
ll7.setOrientation(LinearLayout.HORIZONTAL);
Button btn = new Button(mcontext);
btn.setLayoutParams(lop);
btn.setText(button1);
btn.setTextColor(Color.BLACK);
btn.setTag(0);
btn.setOnClickListener(this);
Button btn1 = new Button(mcontext);
btn1.setLayoutParams(lop);
btn1.setText(button2);
btn1.setTextColor(Color.BLACK);
btn1.setTag(1);
btn1.setOnClickListener(this);
ll7.addView(btn);
ll7.addView(btn1);

LinearLayout ll = new LinearLayout(mcontext);
ll.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams mrealp1 = new RelativeLayout.LayoutParams(
-1, -2);
ll.setLayoutParams(mrealp1);
View v = new View(mcontext);
v.setLayoutParams(new RelativeLayout.LayoutParams(-1, 2));

v.setBackgroundColor(Color.parseColor("#A62A2A"));
View v1 = new View(mcontext);
v1.setLayoutParams(new RelativeLayout.LayoutParams(-1, 2));
v1.setBackgroundColor(Color.parseColor("#A62A2A"));
View v2 = new View(mcontext);
v2.setLayoutParams(new RelativeLayout.LayoutParams(-1, 2));
v2.setBackgroundColor(Color.parseColor("#A62A2A"));
View v3 = new View(mcontext);
v3.setLayoutParams(new RelativeLayout.LayoutParams(-1, 20));
View v4 = new View(mcontext);
v4.setLayoutParams(new RelativeLayout.LayoutParams(-1, 20));
ll.addView(v);
ll.addView(ll0);
ll.addView(v1);
ll.addView(v3);
RelativeLayout.LayoutParams mrealp2 = new RelativeLayout.LayoutParams(
-1, -2);
if (edit == false) {

TextView txt1 = new TextView(mcontext);
txt1.setLayoutParams(mrealp2);

txt1.setGravity(Gravity.CENTER);
txt1.setText(mmsg);
txt1.setTextColor(Color.BLACK);
ll.addView(txt1);
} else {
RelativeLayout.LayoutParams mre = new RelativeLayout.LayoutParams(
-1, -1);
txt0 = new EditText(mcontext);
txt0.setLayoutParams(mre);
             txt0.setTag("msg");
txt0.setGravity(Gravity.TOP);
txt0.setHint(mmsg);
if(msg10!=null)
{
txt0.setText(msg10);
}
if(multi!=false)
{
txt0.setMinLines(5);
}else
{
txt0.setSingleLine(true);
}
txt0.setTextColor(Color.BLACK);
ll.addView(txt0);

}

ll.addView(v4);
ll.addView(v2);
ll.addView(ll7);

mreal.addView(ll);

}
this.show();

}
EditText txt0=null;
public void setButtonClickLisner(AlertTwoButtonListener mli) {
mlistener = mli;
}

AlertTwoButtonListener mlistener = null;
public void setEditButtonClickLisner(AlertEditTwoButtonListener mli) {
ml = mli;
}

public interface AlertTwoButtonListener {
public void onAlertFirstButtonClick();

public void onAlertSecondButtonClick();

}
AlertEditTwoButtonListener ml=null;
public interface  AlertEditTwoButtonListener
{
public void onAlertEditFisrtButtonClick(String msg);
public void onAlertEditSecondButtonClick();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int which = ((Integer) v.getTag()).intValue();

if (mlistener != null)

{
switch (which) {
case 0:
mlistener.onAlertFirstButtonClick();
break;
case 1:
mlistener.onAlertSecondButtonClick();
break;

default:
break;
}


}else if (ml != null)

{



switch (which) {
case 0:
ml.onAlertEditFisrtButtonClick(txt0.getText().toString().trim()+"");
break;
case 1:
ml.onAlertEditSecondButtonClick();
break;

default:
break;
}

}
}

}

No comments:

Post a Comment