am trying to add this this line in my Activity builder.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
but I find find this error Cannot resolve method'getwindow'
as am trying to add my code like this
NewGuestCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.this);
builder.setTitle("Insert Table Name");
// Set up the input
final EditText input = new EditText(TabsActivity.this);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
// 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_CLASS_TEXT);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final String Table_Name = input.getText().toString();
AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.this);
builder.setTitle("Insert number of Covers");
// Set up the input
final EditText input2 = new EditText(TabsActivity.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_NUMBER | InputType.TYPE_CLASS_NUMBER);
builder.setView(input2);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String Cover_check = input2.getText().toString();
TablesFragment.Check_Items = ConnectionClass.Ret_dt("Select * From ChecksItems Where Check_ID = 0");
if (!TablesFragment.Check_Items.containsKey("Change_Temp")) {
if (TablesFragment.Check_Items.size() > 0) {
ArrayList<Object> Valrows = new ArrayList<Object>();
if (TablesFragment.Check_Items.get("Item_ID").size() > 0) {
for (int i = 0; i < TablesFragment.Check_Items.get("Item_ID").size(); i++) {
Valrows.add("");
}
}
TablesFragment.Check_Items.put("Change_Temp", Valrows);
}
}
if (Integer.parseInt(Cover_check) > 0) {
String st = ConnectionClass.Ret_Col("Select Max (CheckSerail) AS Ser From Checks_V where Officer = 0 AND OutLet_ID = " + ConnectionClass.OutletID + " And Rest_ID_Active = " + ConnectionClass.Rest_ID);
if (st.trim() == "")
st = "0";
int Check_Serial = Integer.parseInt(st) + 1;
long Check_ID = Long.parseLong(ConnectionClass.SelectNewIDCheck());
st = "insert into Checks .......
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
builder.show();
as I need from this method that when the alert dialog start the keyboard starts automatically that I don't need to touch the edit text to show the key board
sorry if any thing is not clear and sorry for my bad english
I hope this case could be solved
by the way activity looks
public class TabsActivity extends AppCompatActivity {
There is a similar question on StackOverflow. You need to call getWindow() on the Dialog Class.
call getWindow() by giving any views reference.ex
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Related
I am implementing a simple dialog with a checked listview in it. This is what I've done so far:
CharSequence[] items = {"Brand A", "Brand B", "Brand C"};
AlertDialog.Builder builder = new AlertDialog.Builder(StrengthOfDemandsView.this);
builder.setTitle("Select Brands");
final ArrayList seletedItems=new ArrayList();
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
// indexSelected contains the index of item (of which checkbox checked)
#Override
public void onClick(DialogInterface dialog, int indexSelected,
boolean isChecked) {
if (isChecked) {
seletedItems.add(indexSelected);
} else{
seletedItems.remove(Integer.valueOf(indexSelected));
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
});
dialog = builder.create();
dialog.show();
PROBLEM:
Initially, I'm passing an Array to setMultiChoiceItems method and it works fine but how to pass an ArrayList instead of an array? Like this:
ArrayList<Products> brandList = new ArrayList<>();
Whenever I'm trying to pass an ArrayList to setMultiChoiceItems method it gives me this error:
Cannot resolve method 'setMultiChoiceItems(java.util.ArrayList<com.application.marketvisit.dataItem.Products>, null, anonymous android.content.DialogInterface.OnMultiChoiceClickListener)'
You need to pass a String array to AlertDialog.Builder#setMultiChoiceItemsso collect it as a String array
String arr = new String[brandList.size()];
for(int i=0 ; i< brandList.size();i++){
arr[i] = brandList.get(i).getProductName();
//getProductName or any suitable method
}
Try this...and let me know if it works..
ArrayList<String> strBrandList = new ArrayList<String>();
for (int i = 0; i < brandList.size(); i++) {
strBrandList.add(brandList.get(i).getProductName())
}
I'm displaying checkboxes in alertdialog. When user clicks OK, toast should come up like You've selected PHP, Java, JSON. Right now, its displaying IDs. How can I get values?
Dialog dialog;
final String[] items = {" Objective C", " JAVA", " JSON", " C#", "PHP"};
final ArrayList itemsSelected = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Languages you know : ");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int selectedItemId,
boolean isSelected) {
if (isSelected) {
itemsSelected.add(selectedItemId);
} else if (itemsSelected.contains(selectedItemId)) {
itemsSelected.remove(Integer.valueOf(selectedItemId));
}
}
})
.setPositiveButton("Done!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
//Your logic when OK button is clicked
Toast.makeText(MainActivity.this,"You've selected "+itemsSelected,Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
});
dialog = builder.create();
dialog.show();
[UPDATE] I found solution but it doesn't seem ideal. For now its working by adding following code.
String one="";
String two="";
String three="";
String four="";
String zero="";
if (s.contains("0" ))
{
zero="Obj C ";
}
if (s.contains("1"))
{
one="JAVA ";
} if (s.contains("2"))
{
two="JSON ";
} if (s.contains("3"))
{
three="C# ";
} if (s.contains("4"))
{
four="PHP ";
}
This part of your code
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int selectedItemId,
boolean isSelected) {
if (isSelected) {
**itemsSelected.add(selectedItemId);**
} else if (itemsSelected.contains(selectedItemId)) {
itemsSelected.add(selectedItemId) simply adds the id to your arraylist. You do not want the id added. Since you want the exact values added, You can simply use something like itemsSelected.add(items[selectedItemId]) since the items array contains what values you need to display. When you have put your correct values in your arraylist, then you display your toast maybe using itemsSelected.toString() or something. You also need to handle all the other checks.
I'm checking for invalid input to a group of edit texts in an alert dialog, by checking for null input and calling setError. But in my current implementation the dialog still closes even though there has been invalid input.
A boolean check has been added to each edit text to prevent the dialog from being dismissed if any of the edit texts set the boolean to false like this:
else if(TextUtils.isEmpty(strColour)) {
colourText.setError("Please enter a value");
entriesValid = false;
`
But the dialog is still dismissed despite the invalid input.
My question, whats the error here that allows the dialog to close on invalid input?
I set a break point on this line, if(entriesValid) to check if the condition is triggered but it doesn't break here meaning that the check is be skipped.
This is the complete custom dialog class:
public class MyMessageDialog {
public interface MyMessageDialogListener {
public void onClosed(String ship, String scientist, String email, String volume, String color);
}
#SuppressLint("NewApi")
public static AlertDialog displayMessage(Context context, String title, String message, final MyMessageDialogListener listener){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
builder.setTitle(title);
builder.setMessage(message);
final View layoutView = inflater.inflate(R.layout.custom_view, null);
builder.setView(layoutView);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
boolean entriesValid = true;
// get the edit text values here and pass them back via the listener
if(listener != null)
{
EditText shipText = (EditText)layoutView.findViewById(R.id.shipNameEditText);
EditText scientistNameText = (EditText)layoutView.findViewById(R.id.scientistEditText);
EditText scientistEmailText = (EditText)layoutView.findViewById(R.id.emailEditText);
EditText volumeText = (EditText)layoutView.findViewById(R.id.volumeEditText);
EditText colourText = (EditText)layoutView.findViewById(R.id.colourEditText);
listener.onClosed(shipText.getText().toString(),
scientistNameText.getText().toString(),
scientistEmailText.getText().toString(),
volumeText.getText().toString(),
colourText.getText().toString());
String strShipName = shipText.getText().toString();
String strScientistName = scientistNameText.getText().toString();
String strScientistEmail = scientistEmailText.getText().toString();
String strVolume = volumeText.getText().toString();
String strColour = colourText.getText().toString();
if(TextUtils.isEmpty(strShipName)) {
shipText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strShipName)) {
shipText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strScientistName)) {
scientistNameText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strScientistEmail)) {
scientistEmailText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strVolume)) {
volumeText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strColour)) {
colourText.setError("Please enter a value");
entriesValid = false;
}
}
if(entriesValid)
dialog.dismiss();
}
});
builder.show();
return builder.create();
}
}
Instead of checking listener to be null, add a try catch block. I have not tried this code. But my idea is to remove listener block with try catch and set the boolean flag accordingly. That way it becomes simple.
#SuppressLint("NewApi")
public static AlertDialog displayMessage(Context context, String title, String message, final MyMessageDialogListener listener){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
builder.setTitle(title);
builder.setMessage(message);
final View layoutView = inflater.inflate(R.layout.custom_view, null);
builder.setView(layoutView);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
boolean entriesValid = true;
// get the edit text values here and pass them back via the listener
try
{
EditText shipText = (EditText)layoutView.findViewById(R.id.shipNameEditText);
EditText scientistNameText = (EditText)layoutView.findViewById(R.id.scientistEditText);
EditText scientistEmailText = (EditText)layoutView.findViewById(R.id.emailEditText);
EditText volumeText = (EditText)layoutView.findViewById(R.id.volumeEditText);
EditText colourText = (EditText)layoutView.findViewById(R.id.colourEditText);
String strShipName = shipText.getText().toString();
String strScientistName = scientistNameText.getText().toString();
String strScientistEmail = scientistEmailText.getText().toString();
String strVolume = volumeText.getText().toString();
String strColour = colourText.getText().toString();
if(TextUtils.isEmpty(strShipName)) {
shipText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strShipName)) {
shipText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strScientistName)) {
scientistNameText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strScientistEmail)) {
scientistEmailText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strVolume)) {
volumeText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strColour)) {
colourText.setError("Please enter a value");
entriesValid = false;
}
}
catch(Exception e)
{
entriesValid = false;
}
if(entriesValid)
dialog.dismiss();
}
});
builder.show();
return builder.create();
}
update - New solution - Tried and worked for me
public class Help_DialogScreen extends Dialog implements OnClickListener{
Context context;
public Help_DialogScreen(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context=context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.help_homescreen);
EditText tvGoToAddExpense = (EditText)findViewById(R.id.txtGoToAddExpense);
Button btnTestCLick = (Button)findViewById(R.id.btnTestClick);
btnTestCLick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Click fired", Toast.LENGTH_SHORT).show();
// I have used Toast to show that on click of button, dialog is not getting dismissed. You can add your code and do your logic here.
}
});
}
#Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
}
In the code where you should show the dialog, add this code
Help_DialogScreen cdd=new Help_DialogScreen(CURRENTACTIVITY.this);
cdd.show();
I am having some problem when trying to refresh the rating bar after user submitted their rating. So basically I am passing the existing rating amount when certain button on my other Activity was triggered:
viewDtlEventBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Object[] obj = new Object[2];
obj[0] = String.valueOf(eventIDTV.getText());
obj[1] = eventReviewModel;
new GetEventDetailAsyncTask(new GetEventDetailAsyncTask.OnRoutineFinished() {
public void onFinish() {
// Passing whole object with value into another activity
Intent eventDtlIntent = new Intent(context, EventDetailMain.class);
// Pass in a list of rating star together with amount
eventDtlIntent.putExtra("eventPopulateStarObj", populateRatingStar);
context.startActivity(eventDtlIntent);
}
}).execute(obj);
}
});
And I am populating the rating bar when onCreate():
ratingStarList = (ArrayList<EventReview>) i
.getSerializableExtra("eventPopulateStarObj");
public void populateRatingProgressBar() {
int totalStar = 0;
// Get the total amount of rate records
for (int j = 0; j < ratingStarList.size(); j++) {
if (ratingStarList.get(j).getStarAmt() != null) {
totalStar += Integer.parseInt(ratingStarList.get(j)
.getStarAmt());
}
}
txtTotalRate.setText(totalStar + " Ratings for this event");
// Set progress bar based on the each rates
for (int i = 0; i < ratingStarList.size(); i++) {
if (ratingStarList.get(i).getStarAmt() != null) {
if (ratingStarList.get(i).getEventReviewRate().equals("5")) {
pb5Star.setProgress(Integer.parseInt(ratingStarList.get(i)
.getStarAmt()));
} else if (ratingStarList.get(i).getEventReviewRate()
.equals("4")) {
pb4Star.setProgress(Integer.parseInt(ratingStarList.get(i)
.getStarAmt()));
} else if (ratingStarList.get(i).getEventReviewRate()
.equals("3")) {
pb3Star.setProgress(Integer.parseInt(ratingStarList.get(i)
.getStarAmt()));
} else if (ratingStarList.get(i).getEventReviewRate()
.equals("2")) {
pb2Star.setProgress(Integer.parseInt(ratingStarList.get(i)
.getStarAmt()));
} else if (ratingStarList.get(i).getEventReviewRate()
.equals("1")) {
pb1Star.setProgress(Integer.parseInt(ratingStarList.get(i)
.getStarAmt()));
}
}
}
}
It did populated correctly. However, I not sure how to refresh the rating bar after user submitted their rating. Here is the code when user submit their rating:
public void promptSubmitStar() {
AlertDialog.Builder Dialog = new AlertDialog.Builder(getActivity());
Dialog.setTitle("Confirm Rating");
LayoutInflater li = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View dialogView = li.inflate(R.layout.option_submit_star, null);
txtPromptStarRate = (TextView) dialogView
.findViewById(R.id.txtPromptStarRate);
txtPromptStarRate.setText("Confirm to submit " + starRate
+ " stars for this event?");
Dialog.setView(dialogView);
Dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
EventReview eventReviewModel = new EventReview();
eventReviewModel.setEventID(eventID);
eventReviewModel.setEventReviewBy(userID);
eventReviewModel.setEventReviewRate(String.valueOf(starRate));
new CreateEventReviewAsyncTask(context)
.execute(eventReviewModel);
dialog.dismiss();
// Disable the rating bar by setting a touch listener which
// always return true
ratingBar.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
return true;
}
});
}
});
Dialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
Dialog d = Dialog.show();
EventDialogueBox.customizeDialogueBox(context, d);
}
Any ideas? Thanks in advance.
Use setRating(starRate); to programmatically set the rating on the RatingBar.
I know I could solve this with a header but I'd rather not have one. Is there anyway to access the properties of the cancel button?
My code and an image of the AlertView is below.
for(int i = 0; i < 10; i++)
{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
if(view.getId() == (getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud")))
{
//alert.setTitle("Notes");
// Sets an EditText view to get user input
final EditText input = new EditText(this);
input.setText(table.seats[i].getPlayer().getNotes());
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
for(int i = 0; i < 10; i++)
{
if(view.getId() == (getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud")))
{
table.seats[i].getPlayer().setNotes(input.getText().toString());
}
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
return true;
}
}
I'd reckon you'd need to set your EditText to a defined width, or fill_parent/match_parent.
setMinimumWidth() does the job. The code above it is simply to generate display pixels rather than pixels.
DisplayMetrics metrics = getResources().getDisplayMetrics();
float dp = 250f;
int pixels = (int) (metrics.density * dp + 0.5f);
input.setMinimumWidth(pixels);
you can just set a view for your negative button, like this:
alert.setNegativeButton(R.id.cancelButton, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Some action to be done when clicked..
}
});
and you can create a layout with a button as a root like this:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel">
</Button>
where you will set the width of the button normally to wrap_content
Edit: there is also a AlertDialog.Builder setNegativeButton (int textId, DialogInterface.OnClickListener listener)