android studio edittext force enter | keydown event - android

I have an edittext that performs an action and if the result of that actions returns an int greater than 1 a dialog box pops up for user input, how can I after the user has inputted data to force enter on the edittext (from_loc_code) again?
from_loc_code being my edittext that already has data
int total_tickets_to_loc = Integer.valueOf(VALUES.total_tickets_to_loc);
if (total_tickets_to_loc > 1) {
// force user to scan product id
AlertDialog.Builder aBuilder = new AlertDialog.Builder(transfer.this);
aBuilder.setTitle("Scan Product");
final EditText input = new EditText(transfer.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
aBuilder.setView(input);
aBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final String sText = input.getText().toString().toUpperCase();
VALUES.ticket_product_id = " and p.PRODUCT_ID = '" + sText + "' ";
handler.postDelayed(new Runnable() {
#Override
public void run() {
from_loc_code.requestFocus();
from_loc_code.performClick();
}
}, 200);
}
});
aBuilder.show();
}

Related

AlertDialog with LinearLayout should not dismiss on button click

I only want my AlertDialog to be dismissed when certain conditions are met (when name and surname given are valid) - else it should always be on top of the parent view. My code is this:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final TextView instructions = new TextView(this);
instructions.setText(R.string.alert_enter_data);
final EditText name = new EditText(this);
name.setHint(R.string.name);
final EditText surname = new EditText(this);
surname.setHint(R.string.surname);
LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(instructions);
ll.addView(name);
ll.addView(surname);
alert.setView(ll);
alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String name_txt = name.getText().toString();
String surname_txt = surname.getText().toString();
if ((name_txt.length() > 1) && (surname_txt.length() > 1)) {
dialog.dismiss();
}
}
});
final AlertDialog alert_dialog = alert.create();
alert_dialog.setCanceledOnTouchOutside(false);
alert_dialog.show();
With this code the AlertDialog disappears when the button is pressed, regardless of the input text. I then tried this:
alert_dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name_txt = name.getText().toString();
String surname_txt = surname.getText().toString();
String email_txt = email.getText().toString();
String cellphone_txt = cellphone.getText().toString();
String postcode_txt = postcode.getText().toString();
if ((name_txt.length() > 1) && (surname_txt.length() > 1) && (email_txt.length() > 4)) {
if (debug_mode) {Log.i(TAG,"clause 1");}
String data_to_upload = name_txt + ", " + surname_txt + ", " + email_txt + ", "+ cellphone_txt + ", " + postcode_txt + "\n";
// upload_to_github(data_to_upload);
alert_dialog.dismiss();
}
}
});
}
});
But this way I get not Button at all. The Alert Dialog only contains the EditText fields.
alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {
and
Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);
both are different buttons.
try
Button b = alert.getButton(AlertDialog.BUTTON_NEUTRAL);

How to get selected value from alert dialog?

I'm using alert dialog to show some records.those records are comming from db and display in Alertdialog.when user click on item i want to get item name to Log.this is my code.it show item as [test] but i want it showing as test
ArrayList arrayList and String [] categoryStrings initialized on top
List<Video> vd=Video.findWithQuery(Video.class, "select * from Video");
if (vd.size()>0) {
for (Video v : vd) {
arrayList.add(v.getTitle());
}
final List<String> list = Arrays.asList(arrayList.toString());
categoryStrings=new String[list.size()];
categoryStrings=list.toArray(categoryStrings);
AlertDialog.Builder alert = new AlertDialog.Builder(Editmedia.this);
alert.setTitle("Media List");
alert.setCancelable(false);
final int selected = 0; // or whatever you want
alert.setSingleChoiceItems(categoryStrings, selected, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
//onclick
String categoryString = categoryStrings[item];
Log.d("sel", " " + item+" "+categoryString);
edit();
}
});
alert.show();
log value showing
0 [test] i want it as test
Log
Try this, it should help
String categoryString = categoryStrings[item];
categoryString = categoryString.replaceAll("[\\p{Ps}\\p{Pe}]","");
Log.d("sel", " " +" "+categoryString);
[EDIT]
String categoryString = categoryStrings[item];
categoryString = categoryString.replace("[","");
categoryString = categoryString.replace("]","");
Log.d("sel", " " +" "+categoryString);
Try this:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Media List");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
arrayAdapter.clear();
for (int i = 0; i < list.size(); i++) {
Log.i(LOG_TAG, list.get(i));
arrayAdapter.add(list.get(i));
}
builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), list.get(which),
Toast.LENGTH_LONG).show();
}
});
builder.setPositiveButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();

Android Spinner: Set selected item as default

I am making an android app that asks for the users to select a country via spinner.
When the user opens the app first time, user selects a country from list.
Then when app opens second time, I want the same country to be selected. I don't want user to select the country every time the app is opened.
How to do that?
You can use SharedPreferences to store the selection the first time that the user selects a country, and then use SharedPreferences again for the app to remember the selection, when the user returns a second time.
To store the selection in a SharedPrefence:
SharedPreferences.Editor editor = getPreferences(0).edit();
int selectedPosition = yourSpinner.getSelectedItemPosition();
editor.putInt("spinnerSelection", selectedPosition);
editor.apply();
To load the selection onto the spinner when reusing the app:
SharedPreferences prefs = getPreferences(0);
yourSpinner.setSelection(prefs.getInt("spinnerSelection",0));
Hope this solves your issue :)
Try this one
String citySelected
final CharSequence[] items = {" abc ", " def ", " ghi ", " jkl ", " mno ",
" pqr ", " stu ",
" vwzyz "};
List<String> lanSelected = new ArrayList<>();
final boolean[] checkedItems = new boolean[]{false, false, false, false, false, false, false, false};
List<String> temp = new ArrayList<>();
for (int o = 0; o < items.length; o++) {
temp.add(items[o].toString());
}
final List<Integer> seletedItems = new ArrayList();
if (citySelected.equals("") || citySelected == null) {
} else
lanSelected = Arrays.asList(citySelected.split(","));
if (lanSelected.size() > 0) {
for (int p = 0; p < lanSelected.size(); p++) {
String x = lanSelected.get(p);
int xpos = temp.indexOf(x);
if (xpos != -1) {
checkedItems[xpos] = true;
seletedItems.add(xpos);
}
}
}
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
// arraylist to keep the selected items
AlertDialog dialog = builder
.setTitle("city")
.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
seletedItems.add(indexSelected);
} else if (seletedItems.contains(indexSelected)) {
// Else, if the item is already in the array, remove it
seletedItems.remove(Integer.valueOf(indexSelected));
}
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
String city = "";
for (int i = 0; i < seletedItems.size(); i++) {
if (i == seletedItems.size() - 1) {
city = city + items[Integer.parseInt(seletedItems.get(i).toString())];
} else {
city = city + items[Integer.parseInt(seletedItems.get(i).toString())] + ",";
}
}
btn_city_todisplay.setText(city);
dialog.dismiss();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on Cancel
}
}).create();
dialog.show();
Use spinner method to show selected item
spinner.setSelection(position);
Here position is the last selected position
You can use sharedPreference to store the country you have chosen, then search the map and find the position of country in your array, finally use setSelection(int position)to set the default country

How add setText in input view AlertDialog

I write an algorithm to rename a file, how can I print the current name in the input field?
final EditText input = new EditText(MainActivity.this);
new AlertDialog.Builder(MainActivity.this)
.setTitle("Rename")
.setMessage("Add new name:")
.setView(input)
.setPositiveButton("Ok", new OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
if (directory.isDirectory()) {
File from = new File(directory.getAbsolutePath());
File to = new File(directory.getParent() + "/" + value);
from.renameTo(to);
} else {
File from = new File(directory.getAbsolutePath());
File to = new File(directory.getParent() + "/" + value + "." + checkFormat);
from.renameTo(to);
}
go(currentDirectory);
}
}).setNegativeButton("Cancel", new OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}).show();
I would greatly appreciate the help
If I understand correctly, this should work to put the old name in the input box.
File old = null;
if (directory.isDirectory()) {
old = new File(directory.getAbsolutePath());
}
final EditText input = new EditText(MainActivity.this);
input.setText(old.toString());
...
//the rest of your code

Dialog box filter for edit text minimum charatures

I have set up a dialog box with one edit text box. I used a set filter for max number of charactures allowed but I dont see an a filter for minimum. I added code to add "x" to entered text if the user enters less than 8 chars. Code compiles but crashes.
and throws Error is "string longer than 9". and gives (16 as piece.length()). could use some help solving
public void onClick(View item) {
final EditText input = new EditText(this);
switch (item.getId()) {
case R.id.textView1:
strEditText = Title1.getText().toString(); // OK
input.setText(strEditText);
input.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8) }); // Set max # of charactures for Input
for (int i = 0; i < strEditText.length(); i++) { //
String piece = strEditText; //
String toBeAdded = ""; //
if (piece.length() < 8) {
int length = piece.length();
toBeAdded = piece;
while (length < 8) {
toBeAdded = toBeAdded + "X";
length++;
}
} else if (piece.length() > 8) {
throw new IllegalArgumentException("Error, string longer than 9");
} else {
toBeAdded = piece;
}
strEditText = strEditText + toBeAdded;
}// return to for-loop //
new AlertDialog.Builder(ElsEditTitles.this)
.setTitle("Ttile").setView(input).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
strReturn = input.getText().toString();
Title1.setText(strReturn);
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}).show();
break;
i Cant Understand Why You Put This For : for (int i = 0; i < strEditText.length(); i++)
For Example if EditText is 'abc' strEditText is 'abc' and that for repeats 3 Time.
in First Time toBeAdded = 'abcXXXXX' and when code "strEditText = strEditText + toBeAdded;" run strEditText = 'abcabcXXXXX' and when That for goes into 2nd loop its Already more than 8 character and throws your Error.
Sorry For My Bad English..

Categories

Resources