Reputation: 516
I am trying to get a value from the user the first time the app is opened. I am inflating an AlertDialog to get the value from the user. I am not able to input any text into the EditText, eventhough I can see the cursor blinking.
The code in MainActivity
SharedPreferences sharedPreferences = getSharedPreferences("hasRunBefore", 0);
boolean hasRun = sharedPreferences.getBoolean("hasRun", false);
if (!hasRun) {
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putBoolean("hasRun", true);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View dialogView = layoutInflater.inflate(R.layout.alertdialog, null);
final EditText editText = (EditText) dialogView.findViewById(R.id.alert_dialog_editText);
builder.setView(dialogView)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
regNum = editText.getText().toString();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
edit.putString("registerNumber", regNum);
edit.apply();
TextView textView = (TextView) findViewById(R.id.regNum_textView);
textView.setText(regNum);
} else {
//code if the app HAS run before
TextView textView = (TextView) findViewById(R.id.regNum_textView);
textView.setText(regNum);
}
The layout file is
<?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:id="@+id/alertDialog"
>
<TextView
android:id="@+id/alert_dialog_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Register Number"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/alert_dialog_editText"
android:hint="Type your register number here"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_below="@+id/alert_dialog_textview"
/>
</RelativeLayout>
What am I doing wrong?
Upvotes: 1
Views: 1286
Reputation: 516
Though the solution posted by Ferdous Ahamed is working, there weren't any problems in my code in the first place. I was invoking a couple of permissions in my application and I didn't configure them properly. What happened is that, when the application is opened, the focus is given to the permission dialog. When I close that dialog, the focus changed to the second permission dialog which I didn't configure properly. The focus was hence sent into background, waiting for user input infinitely.
The code started to work once I fixed the permissions. Thanks to each and everyone who helped me.
The permissions code initially:
/* Ensures Bluetooth is available on the device and it is enabled. If not,
displays a dialog requesting user permission to enable Bluetooth.*/
if(!bluetoothAdapter.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
/*Getting Fine Location Access from the User. For odd reasons, the device won't scan if the location access isn't granted by the user.*/
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
//Getting READ_PHONE_STATE from the User.
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
android.Manifest.permission.READ_PHONE_STATE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{android.Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
The code I changed it to: (Merged the permissions into a single time task. Should've done this earlier. My bad.)
/*Getting Fine Location Access from the User. For odd reasons, the device won't scan if the location access isn't granted by the user.
* Also getting the READ_PHONE_STATE permission in the same dialog*/
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED )
{
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION , android.Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
Upvotes: 1
Reputation: 21756
Try using this method:
public void showEditTextDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View dialogView = layoutInflater.inflate(R.layout.alertdialog, null);
final EditText editText = (EditText) dialogView.findViewById(R.id.alert_dialog_editText);
// Keyboard
final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
// Auto show keyboard
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean isFocused) {
if (isFocused)
{
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
});
builder.setView(dialogView)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
regNum = editText.getText().toString();
Log.d("STACKOVERFLOW", "Registration number: " + regNum);
// Hide keyboard
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Hide keyboard
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
OUTPUT:
Upvotes: 2
Reputation: 173
Try this code:
// get yourView.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.yourView, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
Upvotes: 0