Input text dialog Android
Sounds like a good opportunity to use an AlertDialog.
As basic as it seems, Android does not have a built-in dialog to do this (as far as I know). Fortunately, it's just a little extra work on top of creating a standard AlertDialog. You simply need to create an EditText for the user to input data, and set it as the view of the AlertDialog. You can customize the type of input allowed using setInputType, if you need.
If you're able to use a member variable, you can simply set the variable to the value of the EditText, and it will persist after the dialog has dismissed. If you can't use a member variable, you may need to use a listener to send the string value to the right place. (I can edit and elaborate more if this is what you need).
Within your class:
private String m_Text = "";
Within the OnClickListener of your button (or in a function called from there):
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
// Set up the input
final EditText input = new EditText(this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
EditText in Alert Dialog Android
The reason you are not able to access editTextField is because of it is declared as local variable in alertDialog() method.
If in case you want to keep that variable as local ,you need to set the listener method directly in that method as i show below.
private EditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setView(inputEditTextField)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String editTextInput = inputEditTextField.getText().toString();
Log.d("onclick","editext value is: "+ editTextInput);
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
Else you can replace this line from alertDialog() method with this below line
editTextField = new EditText(this.getContext());
you need add this line above onCreate method
EditTextField editTextField;
Built-in single input text field dialog for Android
No, there is no such component available but we can manage to achieve this by using setView(View) without creating any xml in layout/inflate. As per the code below:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message :");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
Log.d("", "Pin Value : " + value);
return;
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
return;
}
});
alert.show();
android align Input text dialog in textview
Use this one:-
// Set an EditText view to get user input
final EditText input = new EditText(this);
LayoutParams lp = new LayoutParams();
lp.gravity= Gravity.CENTER_HORIZONTAL;
input.setLayoutParams(lp);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
TextView tv = (TextView)findViewById(R.id.mj);
tv.setText("" + value);
}
});
please let me know if it works or not :)
Android studio Alert dialog block spaces for input in text
I find this more difficult than working on an XML file.
You could (and probably should) create custom dialogs that have their own view/XML file.
To do this, extend DialogFragment:
public class MyDialog extends DialogFragment {
EditText editText;
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// get your custom layout
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.my_custom_layout, null);
builder.setView(view).setCancelable(false);
editText = view.findViewById(R.id.editText);
// add your code here
}
}
XML (R.layout.my_custom_layout):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo"
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
android:inputType="textFilter" />
</androidx.constraintlayout.widget.ConstraintLayout>
and in MainActivity:
public void openDialog(Context context) {
MyDialog dialog = new MyDialog();
dialog.show(getSupportFragmentManager(), "myDialog");
}
also check Android disable space only for Edittext to disable space from XML and
https://www.youtube.com/watch?v=ARezg1D9Zd0&t=446s&ab_channel=CodinginFlow for a more in depth tutorial on custom dialogs and how to send and recieve info from them.
Placing plain text input in an AlertDialog
What you're looking for is the setView method of the AlertDialog.Builder. You can create an xml layout that includes the edittext and then set it as the view of your dialog:
<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"/>
And then you can use
final View customLayout = getActivity().getLayoutInflater().inflate(R.layout.edittext, null);
builder.setView(customLayout);
Dialog dialog = builder.create();
EditText editText = (EditText) customLayout.findViewById(R.id.myEditText);
editText.setText("Editable text");
How to make a edittext box in a dialog
Use Activtiy Context
Replace this
final EditText input = new EditText(this);
By
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog.setView(input); // uncomment this line
AlertDialog Input Text
You EditText
should have android:password="true"
.
Check this link.
how to handle empty text from the input alert dialog and control the alert button
You should set the listener later:
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Save QR Code");
builder.setMessage("Please enter the name for the QR Code.");
builder.setCancelable(true);
// Set up the input
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT );
builder.setView(input);
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(view1 -> {
m_Text = input.getText().toString().trim();
if(TextUtils.isEmpty(m_Text)){
input.setError("Please Enter Name of the Image QR Code");
} else {
dialog.dismiss();
}
});
The problem with your approach is that AlertDialog will still use its listener as a kind of decorator of yours. So it will call your listener but then it will still close the dialog.
If you want to prevent it, you need to overwrite the internal listener with yours after the dialog is shown.
How to save user input from Alert Dialog?
The issue with your initialization.
LayoutInflater inflater = LayoutInflater.from(AssignmentActivity.this);
View dialogLayout = inflater.inflate(R.layout.assignment_edit_dialog, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(AssignmentActivity.this);
dialog.setView(dialogLayout);
final TextView name = (TextView) dialogLayout .findViewById(R.id.assignmentNameView);
final TextView mark = (TextView) dialogLayout .findViewById(R.id.assignmentMarkView);
final TextView overallMark = (TextView) dialogLayout .findViewById(R.id.assignmentOverallMarkView);
final TextView weight = (TextView) dialogLayout .findViewById(R.id.assignmentWeightView);
As you said TextViews are hooked up with AlertDialog custom view then they view context should be of Dialog view.
You have a lot to learn in terms of concept. You are setting adapter before initializing your list.
assignmentAdapter = new AssignmentListAdapter(assignmentList, this); // at this line **assignmentList** is null as you initiliaze after this line
assignment = new Assignment(this);
You must initialize first like this
assignment = new Assignment(this);
assignmentAdapter = new AssignmentListAdapter(assignmentList, this);
Related Topics
Validate Username and Email Crashed and Cannot Insert to Firebase Database
Recyclerview Changing Items During Scroll
Set Span for Items in Gridlayoutmanager Using Spansizelookup
How to Draw a Route, Along an Existing Road, Between Two Points
Error Message:This Android Sdk Requires Android Developer Toolkit Version 22.6.1 or Above
Apache Commons Codec with Android: Could Not Find Method
Android - Acceleration Down (Smash)
How Does the Getview() Method Work When Creating Your Own Custom Adapter
Android Deep Linking: Use the Same Link for the App and the Play Store
How to Use Mapview in Android Using Google Map V2
How to Determine Which Dependency Causes Google Play Openssl Warning
Jni and Gradle in Android Studio
Livedata Update on Object Field Change
Getting the Battery Current Values for the Android Phone
Pathpattern to Match File Extension Does Not Work If a Period Exists Elsewhere in the File Name
Android: How to Check If the Server Is Available
Centering Recyclerview Items with Flexboxlayoutmanager
How to Change the Color of a Switchcompat from Appcompat Library