Android : Customize dialog title bar

on Thursday, September 18, 2014


i have coded a custom dialog class.in here i want to change the title.so i coded a layout and using LayoutInflater set it to setFeatureInt method as parameter.but once i run it run-time errors occurred. v.getid always return -1.how to solve this error.if i remove v.getid and put R.layout.titleview it works finely.



public class CusAlertDialog {

private Button btn_ok, btn_cancel;
private TextView textmsg;
private Dialog dialog;
private Context context;

public CusAlertDialog(Context context) {

this.context = context;
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
dialog.setContentView(R.layout.mydialoglayout);

setBtn_ok((Button) dialog.findViewById(R.id.btn_ok));
setBtn_cancel((Button) dialog.findViewById(R.id.btn_cancel));
textmsg = (TextView) dialog.findViewById(R.id.textmsg);

/////////////////////////////////////////////////////////////


LayoutInflater myLayout = LayoutInflater.from(context);
View v = myLayout.inflate(R.layout.titleview, null);
TextView title = (TextView) v.findViewById(R.id.title);
title.setText("lahiru");
v.getId();
CustomLog.d("inflator", myLayout+"");
CustomLog.d("view", v.getId()+"");//this return -1

dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, v.getId());

// TODO Auto-generated constructor stub
}

public void setMessage(CharSequence massage) {
textmsg.setText(massage);
}

public void setPositiveButtonName(CharSequence massage)
{
btn_ok.setText(massage);
}

public void setNegitiveButtonName(CharSequence massage)
{
btn_cancel.setText(massage);
}

public void show() {
dialog.show();
}

public Button getBtn_ok() {
return btn_ok;
}

public void setBtn_ok(Button btn_ok) {
this.btn_ok = btn_ok;
}

public Button getBtn_cancel() {
return btn_cancel;
}

public void setBtn_cancel(Button btn_cancel) {
this.btn_cancel = btn_cancel;
}

public void dismiss()
{
dialog.dismiss();
}
}


titleview xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textSize="25dp"
android:textStyle="bold"
android:maxLines="1"
android:ellipsize="end"
android:text="yeay"
/>
</RelativeLayout>

0 comments:

Post a Comment