I try to display a RatingBar in a AlertDialog (It is regularly called from a Handler).
My problem is that I can't change the number of stars. I know there is a lot of answers ot there, but always passing through the xml, which I don't want.
Can anyone help ?
public void taskRatingPopup() {
final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
final RatingBar rating = new RatingBar(this);
rating.setMax(5);
rating.setNumStars(5);
rating.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
popDialog.setIcon(android.R.drawable.btn_star_big_on);
popDialog.setTitle("BlaBla");
popDialog.setView(rating);
// Button OK
popDialog.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
writeRatingFile(rating.getProgress());
//Remove the dialog
dialog.dismiss();
}
})
// Button Cancel
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
popDialog.create();
popDialog.show();
}
Thanks a lot for any help !
//code block
public void itemRatingPopup() {
final AlertDialog.Builder popDialog = new AlertDialog.Builder(
mainScreen);
final RatingBar rating = new RatingBar(mainScreen);
rating.setNumStars(5);
rating.setStepSize(0.1f);
rating.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
LinearLayout parent = new LinearLayout(mainScreen);
parent.setGravity(Gravity.CENTER);
parent.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
parent.addView(rating);
// popDialog.setIcon(android.R.drawable.btn_star_big_on);
popDialog.setTitle(mainScreen.getResources().getString(
R.string.user_ratings));
popDialog.setView(parent);
// Button OK
popDialog.setPositiveButton(R.string.submit,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String fanid = StorageManager.getInstance()
.getSharedPrefs(mainScreen)
.getString(AppConstants.PREFS_FAN_ID, "0");
if (fanid != null) {
new SubmitUserRatingAsyncTask("Item", ""
+ item.getId(), fanid, rating.getRating())
.execute();
}
dialog.dismiss();
}
}).setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
popDialog.create();
popDialog.show();
}
your dialog UI will not show exactly 5 stars, it will may show any number of start because you have assign RatingBar as a root view of Dialog, for reference see the above code:
you can change rating programatically by below code:
float myRating=0f;
rating.setRating(myRating)
For adding the no. of Stars in your Rating bar,
just get the that number and use this tag.
rb_rate.setNumStars(<no.of stars,integer value>);
Related
How do I set the max of how many characters can be entered. So now the user can enter a lot in the alertdialog While it should have the max. it's not like XML. That would have been easier, any tips for this alert dialog?
private void RequestNewGroup()
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialog);
builder.setTitle("Enter Group Name ");
final EditText groupNameField = new EditText(MainActivity.this);
groupNameField.setHint("");
builder.setView(groupNameField);
builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
String groupName = groupNameField.getText().toString();
if (TextUtils.isEmpty(groupName))
{
Toast.makeText(MainActivity.this, "Please write Group Name...", Toast.LENGTH_SHORT).show();
}
else
{
CreateNewGroup(groupName);
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
dialogInterface.cancel();
}
});
builder.show();
}
You need to add a InputFilter to your EditText
final EditText groupNameField = new EditText(MainActivity.this);
groupNameField.setHint("");
// ### -> max length
groupNameField.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(###) });
This is my dialog and i want to send the Tmin and Thr to on create or other public. this app is i want to set the time and show on the mainactivy but now i don't know how to send the value to the other public can anyone help me
private String Tmin;
private String Thr;
public void set_dialog(View v){
AlertDialog.Builder alertDialog = new AlertDialog.Builder( this );
View row = getLayoutInflater().inflate( R.layout.settime,null );
final TextView textView=(TextView)row.findViewById( R.id.textView10 ) ;
final TextView textView1=(TextView)row.findViewById( R.id.textView8 ) ;
NumberPicker numPicker=(NumberPicker)row.findViewById(R.id.numberPicker);
numPicker.setMaxValue(12);
numPicker.setMinValue(0);
numPicker.setValue(1);
NumberPicker numPickerhr=(NumberPicker)row.findViewById(R.id.numberPicker1);
numPickerhr.setMaxValue(59);
numPickerhr.setMinValue(0);
numPickerhr.setValue(1);
numPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener (){
#Override
public void onValueChange(NumberPicker view, int oldValue, int newValue) {
//Toast.makeText(getApplicationContext(), "pick number is " + String.valueOf(newValue), Toast.LENGTH_SHORT).show();
//textView.setText( String.valueOf(newValue));
Tmin = String.valueOf(newValue);
textView.setText(Tmin);
}
});
numPickerhr.setOnValueChangedListener(new NumberPicker.OnValueChangeListener (){
#Override
public void onValueChange(NumberPicker view, int oldValue, int newValue) {
//Toast.makeText(getApplicationContext(), "pick number is " + String.valueOf(newValue), Toast.LENGTH_SHORT).show();
Thr = String.valueOf(newValue);
}
});
alertDialog.setView( row );
/*alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//按下按鈕時顯示快顯
}
});*/
AlertDialog dialog=alertDialog.create();
dialog.show();
}
I assume you will have one more button in your alertDialog, example OK button. Once OK button is clicked, return these two values to Main Activity.
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//get text to string
String text = "Min : "+TMin+"Thr : "+Thr;
textView_main.setText(text);
}
});
Declare textView named textView_main for your Main Activity.
I have a button that open a dialog for change a number. The button is in activity, and generate a new class names dialogs, for storage different dialogs.
Dialog consigna: Dialogs.class
public String consigna(){
AlertDialog.Builder alert = new AlertDialog.Builder(ctxt);
alert.setTitle("Nueva temperatura");
alert.setMessage("Agrega una nueva temperatura");
final EditText input = new EditText(ctxt);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
data = input.getText().toString();
}
});
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Put actions for CANCEL button here, or leave in blank
}
});
alert.show();
return data;
}
on click button: MainActivity
//I need change these two values:
final public double temperatura = 200.3;
String newData;
...
public void onClick(View v) {
switch (v.getId()){
case R.id.tempConsigna:
dialog = new Dialogs(MainActivity.this);
String data = dialog.consigna();
newData = data;
break;
...//other cases...
The issue is in newData = data; I doesn't have a data, because the dialog not are closed. The dialog work in other thread,no?
How to change newData var with the dialog result? It is posible into a dialogs class?
You need a callback method implemented via an interface:
public String consigna(final OnConfirm confirm){
final String[] data = new String[1];
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle("Nueva temperatura");
alert.setMessage("Agrega una nueva temperatura");
final EditText input = new EditText(activity);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
data[0] = input.getText().toString();
confirm.onConfirm(data[0]);
}
});
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Put actions for CANCEL button here, or leave in blank
}
});
alert.show();
return data[0];
}
public interface OnConfirm {
void onConfirm(String s);
}
And in your Main Activity
Dialogs dialog = new Dialogs(MainActivity.this);
dialog.consigna(new Dialogs.OnConfirm() {
#Override
public void onConfirm(String s) {
Log.d("data", s);
}
});
It will not return result as you want but after click on dialog ok button then access data variable. no need to return.
I am trying to get a number of cards to pop up using alertdialog.builder. Even though I did .create().show(); the dialog does not show on my screen. I'm not sure what is causing this problem.
I have marked the place where I am getting nothing in the comments.
Java Code:
ImageView image_questionmark= new ImageView(this);
final ImageView image_pass = new ImageView(this);
final ImageView image_youpay = new ImageView(this);
image_questionmark.setImageResource(R.drawable.card_questionmark);
image_pass.setImageResource(R.drawable.card_pass);
image_youpay.setImageResource(R.drawable.card_youpay);
AlertDialog.Builder dialog = new AlertDialog.Builder(PayActivity.this);
for(int i=0; i<people; i++) {
/*PASS*/
if(array[i] == 0) {
dialog.setView(image_questionmark);
dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(PayActivity.this)
.setView(image_pass)
.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(); /*move on to next value in array*/
}
}).create().show(); /*HERE: Nothing showed on my screen when running debugger...*/
}
});
dialog.create().show();
/*If not the first card, show previous card*/
if(i!=0) {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
i--; /*return to previous value in array*/
} /*First card*/
else {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
}
});
}
}/*YOU PAY*/
else {
dialog.setView(image_questionmark);
dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(PayActivity.this)
.setView(image_youpay)
.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(); /*move on to next value in array*/
}
}).create().show();
}
});
dialog.create().show();
/*If not the first card, show previous card*/
if(i!=0) {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
i--; /*return to previous value in array*/
} /*First card*/
else {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
}
});
}
}
}
Thank you for your time :)
I did not try your code.But you can try it like -
First initilise your image object then assign to another final variable.
I think it will work.
ImageView image_pas1 = new ImageView(this);
final ImageView image_youpay = new ImageView(this);
image_questionmark.setImageResource(R.drawable.card_questionmark);
image_pass1.setImageResource(R.drawable.card_pass);
image_youpay.setImageResource(R.drawable.card_youpay);
final ImageView image_pass=image_pass;
You need to set LayoutParams for your ImageView in order to know how to draw itself. Maybe something like this:
image_questionmark.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, // width
LayoutParams.WRAP_CONTENT)); // height
And do this as well for your other 2 ImageViews
First of all, Dialogs need
buildervariablename.create();
only.
Second, every single Dialog has to be in a seperate class, like this:
public class TestDialog extends DialogFragment
{
// Put your dialog code in here
}
And then you just call your dialog there, where it is needed. Inside another method, or even another dialog, by using the following:
DialogFragment fragment = new TestDialog();
fragment.show(getFragmentManager(),"testdialog");
Hope this helped you, if you have further questions, just comment this answer.
I have an AlertDialog which displays an array into a single selected choice:
protected boolean blFrom, blTo;
protected void showSelectToDialog() {
boolean[] checkedDate = new boolean[toDate.length];
int count = toDate.length;
DialogInterface.OnClickListener setD2 = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//TODO Auto-generated method stub
onChangeSelectedTo(which);
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select To Year");
builder.setSingleChoiceItems(toDate, count, setD2);
builder.setCancelable(true);
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
dialog2 = builder.create();
dialog2.show();
}
protected void onChangeSelectedTo(int j) {
bTo.setText(toDate[j]);
sTo = ((AlertDialog)dialog2).getListView().getCheckedItemPosition();
blTo = true;
displayToast(String.valueOf(sTo));
to = j;
dialog.dismiss();
}
What I want to do is the first time it loads it should display the default. Once I select a choice and the dialog closes and I bring up the same dialog again it should show the previously selected choice I made and scroll to it.
How do I accomplish it?
As of right now, I can get the selected position but what next?
You can store the chosen value in a variable of your Activity or using SharedPreferences