Reputation: 25
I have a Custom AlertDialog.Builder but I couldn't reach the EditText.
This is my DEFAULT Class;
AlertDialog.Builder Builder = new AlertDialog.Builder(this);
LayoutInflater Inflater = this.getLayoutInflater();
View AddCategory = Inflater.inflate(R.layout.category_new, null);
final EditText etCategoryName = (EditText) AddCategory.findViewById(R.id.btnAddCategory);
Builder.setView(AddCategory)
.setTitle("Add Category")
.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Time Today = new Time(Time.getCurrentTimezone());
Today.setToNow();
//This code has error !...
String CategoryName = etCategoryName.getText().toString();
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
Builder.show();
This String CategoryName = etCategoryName.getText().toString();
This is my Costum AlertDialog layout;
<?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="60dp" >
<TextView
android:id="@+id/tvAddCategoryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:text="@string/AddCategoryName"
android:textColor="@color/White"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/etAddCategoryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvAddCategoryName"
android:layout_alignBottom="@+id/tvAddCategoryName"
android:layout_toRightOf="@+id/tvAddCategoryName"
android:layout_marginLeft="20dp"
android:hint="@string/CategoryName"
android:ems="10" >
<requestFocus />
</EditText>
Please help me ...
Upvotes: 1
Views: 163
Reputation: 22291
The Problem is occurred because you are passing button id instead of edittext id
Please Write below line of code
final EditText etCategoryName = (EditText) AddCategory.findViewById(R.id.etAddCategoryName);
instead of
final EditText etCategoryName = (EditText) AddCategory.findViewById(R.id.btnAddCategory);
Upvotes: 1
Reputation: 1461
If Seems like you mistyped the EditText id, you put R.id.btnAddCategory and seems like it should be R.id.etAddCategoryName.
Upvotes: 0
Reputation: 56925
try this.
final EditText etCategoryName = (EditText) AddCategory.findViewById(R.id.etAddCategoryName);
In Your xml file your editText id is etAddCategoryName
and in your java file you initialize different name (btnAddCategory).
So please change btnAddCategory
to etAddCategoryName
.
Upvotes: 2