How can I access the EditText field in a DialogBox? - android

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.

Related

How to access EditText placed in a custom layout made for an AlertDialog?

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);

How controll alertdialog button text size on android?

Hello senior developer.
recently I create alertdialog.
I know title size, message size controll
but I don't know controll button text size
How controll button text size ?
I must this format please
thanks.
View view = getLayoutInflater().inflate(R.layout.connecting_error_dialog, null);
TextView txtTitle = (TextView) view.findViewById(R.id.title);
txtTitle.setTextSize(40);
txtTitle.setTextColor(Color.RED);
txtTitle.setText("fail");
TextView message = (TextView) view.findViewById(R.id.message);
message.setTextSize(30);
message.setText("dddddd");
AlertDialog.Builder builder = new AlertDialog.Builder(Connectingreceiver.this);
builder.setView(view)
.setCancelable(false)
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
moveTaskToBack(true);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="HelloAlert!"/>
<TextView android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip"/>
</LinearLayout>
The alert dialog needs to be created first and then it's elements become accessible to be changed.
final AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button btnPositive = alert.getButton(Dialog.BUTTON_POSITIVE);
btnPositive.setTextSize("desired font size");
Button btnNegative = alert.getButton(Dialog.BUTTON_NEGATIVE);
btnNegative.setTextSize("desired font size");
}
});
return alert;

QustomDialogBuilder can't find item

I need to change the appearence of my dialogs so I decided to use QustomDialog.
I'm using a custom layout via setCustomView, but when i handle onClick of PositiveButton I can't find any element of my layout.
The layout consist in a EditText:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/setServer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/server_url" />
</LinearLayout>
The Listener is:
QustomDialogBuilder builder = new QustomDialogBuilder(this);
/* Some customization */
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String url = ((EditText) findViewById(R.id.setServer)).getText().toString();
if (Utilities.isUrl(url)) {
PreferenceHelper.setRestUrl(getApplicationContext(), url);
} else {
Toast.makeText(getApplicationContext(), "Invalid URL", Toast.LENGTH_SHORT);
}
}
});
But when i press "OK" i get java.lang.ClassCastException: com.android.internal.view.menu.ActionMenuItemView cannot be cast to android.widget.EditText.
How can I solve this?
Probably the findViewById method is looking in the wrong layout (in the activity layout instead of the dialog).
You can try to create a final variable with the Dialog when it is shown:
final AlertDialog dialog = builder.show();
And then use it in the findViewById:
String url = ((EditText) dialog.findViewById(R.id.setServer)).getText().toString();

Android trouble in setting text from Edit TextView from alert dialog to EditText View

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);

AlertDialog Builder Control Access

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.

Categories

Resources