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 ...
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 .
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);
If Seems like you mistyped the EditText id, you put R.id.btnAddCategory and seems like it should be R.id.etAddCategoryName.
Related
I have created a custom alertdialog with a radiogroup, radiobuttons, and edittext.
The goal is to trying to get the text value from the selected radiobutton and the edittext. At this point, I can only get the value entered in the edittext.
I have seen the following link and tried to adjust the code to adapt, but it seems that the code still doesn't work.
Android getting value from selected radiobutton
http://www.mkyong.com/android/android-radio-buttons-example/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="#+id/radioPersonGroup"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="#+id/gButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="G" />
<RadioButton
android:id="#+id/kButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="K" />
</RadioGroup>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:ems="10"
android:hint="$10.00"
android:inputType="numberDecimal" />
</LinearLayout>
java file
private RadioButton radioSelectedButton;
...
FloatingActionButton fab = findViewById(R.id.fab);
final RadioGroup group = findViewById(R.id.radioPersonGroup);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final View dialogView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
builder.setTitle("Who Paid")
.setView(dialogView)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
int selectedId = group.getCheckedRadioButtonId();
radioSelectedButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MainActivity.this,
radioSelectedButton.getText(), Toast.LENGTH_SHORT).show();
EditText input = dialogView.findViewById(R.id.editText2);
Toast.makeText(MainActivity.this,input.getText().toString(), Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
When I click on either buttons followed by the Ok to submit the dialog the following exception occurs.
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
You're getting a NullPointerException in this line
int selectedId = group.getCheckedRadioButtonId();
because you tried to "find" the RadioGroup in the wrong View when you wrote
final RadioGroup group = findViewById(R.id.radioPersonGroup);
The RadioGroup is part of the Dialog, so you need to look for it in the View tree of dialogView:
// inside onClick()
final View dialogView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
final RadioGroup group = dialogView.findViewById(R.id.radioPersonGroup);
Similarly, you need to "find" selectedRadioButton in a ViewGroup which contains it. e.g.
radioSelectedButton = (RadioButton) dialogView.findViewById(selectedId);
or
radioSelectedButton = (RadioButton) group.findViewById(selectedId);
I have made a custom layout for AlertDialog in which there is an EditText.
On click of a button I want to check whether this edittext is empty or filled so that I can carry on further operations.
Here is xml code for custom layout for AlertDialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<EditText
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="type here"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Here's what I'm doing with java code:
final AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
builder.setTitle("Choose a unique username");
builder.setView(R.layout.choose_unique_name_dialog);
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (uniqueUserName.getText().toString().isEmpty()) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Please choose a unique username", Snackbar.LENGTH_SHORT);
snackbar.show();
} else {
signingUpMethod();
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
So, I want to know how to access this EditText from my MainActivity?
Please let me know.
Try this code,
make it as public static method and you can access from Any other activity
Store edittext value in some static variable and access from another class.
Inflate the layout you're setting to your alertDialog like this
LayoutInflater inflater = this.getLayoutInflater();
View alertDialogView = inflater.inflate(R.layout.your_layout,null);
Access your EditText like this
EditText editText = (EditText) alertDialog.findViewById(R.id.id);
I have a custom AlertDialog with radio buttons which will open on click of a textview. Everything is working as it is supposed to except that the radio buttons are never shown as "checked". Whenever I open the AlertDialog all of the buttons are unchecked. After selecting a button and opening the AlertDialog again the buttons are unchecked again.
My XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<RadioGroup
android:id="#+id/rg_gender"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<RadioButton
android:id="#+id/buttonMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/therapist_male"
/>
<RadioButton
android:id="#+id/buttonFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/therapist_female"
/>
<RadioButton
android:id="#+id/buttonEither"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/therapist_either"
/>
</RadioGroup>
</LinearLayout>
My AlertDialog:
private void therapistDialog() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.gender_dialog, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setNegativeButton(getResources().getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog alertDialog = dialogBuilder.show();
final RadioGroup rg = (RadioGroup) dialogView.findViewById(R.id.rg_gender);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.buttonMale){
tvGender.setText(getResources().getString(R.string.therapist_male));
therapistSex = 0;
mMassage.setTherapistSex(therapistSex);
male.setChecked(true);
alertDialog.dismiss();
} else if (checkedId == R.id.buttonFemale){
tvGender.setText(getResources().getString(R.string.therapist_female));
therapistSex = 1;
mMassage.setTherapistSex(therapistSex);
alertDialog.dismiss();
} else if (checkedId == R.id.buttonEither){
tvGender.setText(getResources().getString(R.string.therapist_either));
therapistSex = 2;
mMassage.setTherapistSex(therapistSex);
alertDialog.dismiss();
}
}
});
You are not restoring the state of dialog on opening and assign a new instance of AlertDialog every time You invoke therapistDialog method.
Try to check the button on AlertDialog view init.
Add
int buttonToCheck = mMassage.getTherapistSex();
RadioGroup radioGroup = dialogView.findViewById(R.id.rg_gender);
int buttonId = radioGroup.getChildAt(buttonToCheck).getId();
radioGroup.check(radioGroup.getChildAt(buttonToCheck).getId());
Before invoking dialogBuilder.setView(dialogView);
Every time you call the method therapistDialog() is creating a new instance of the alertDialogBuilder. You must create instance of the alertDialogBuilder only once globally,
and just call
final AlertDialog alertDialog = dialogBuilder.show(); inside your method.
note: this is not tested but this should work and will maintain your check box states also
I have an Text View on which when user click will open a alert dialog. The dialog consists of Edit Text and a button. In Edit text when user enters it's height and clicks on "OK" button,the entered height will gets displayed to the Edit Text.
MainActivity.java
et_weightAndHeight = (EditText) findViewById(R.id.et_weightAndHeight);
et_weightAndHeight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater li = LayoutInflater.from(AccountActivity.this);
View promptView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountActivity.this);
alertDialog.setView(promptView);
final EditText input = (EditText)findViewById(R.id.editTextDialogUserInput);
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
et_weightAndHeight.setText(input.getText().toString());
}
});
AlertDialog aD = alertDialog.create();
aD.show();
}
});
}
}
prompts.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Weight in lbs : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editTextDialogUserInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</LinearLayout>
Without proper explanation and logcat, I am assuming you are getting NullPointerException because you haven't initialized the TextView inside the dialog properly.
Change,
final TextView input =(TextView)findViewById(R.id.editTextDialogUserInput);
to
final TextView input =(TextView)promptView.findViewById(R.id.editTextDialogUserInput);
How can I access the EditText field in a DialogBox?
Place your EditText widget into your dialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/myEditText"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
Then inflate the View from within your Dialog and get the content of the EditText.
private Dialog myTextDialog() {
final View layout = View.inflate(this, R.layout.myDialog, null);
final EditText savedText = ((EditText) layout.findViewById(R.id.myEditText));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(0);
builder.setPositiveButton("Save", new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String myTextString = savedText.getText().toString().trim();
}
});
builder.setView(layout);
return builder.create();
}
After pressing the "Save"-button myTextString will hold the content of your EditText. You certainly need to display the dialog of this example first.