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)
Related
I making android application,I need error popup in one moment and when popup is showing and I click OK,popup is closing but main layout is dark and doesn't work,it is freezing.
please see my code,Can I refresh or reactivate this layout?
I need any solution for this.
please help and thank you
package com.example.granturismo.binaryconverter;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.calculate:
try {
EditText decimal = findViewById(R.id.decimal_value);
EditText binary = findViewById(R.id.binary);
Dialog dlg = new Dialog(this);
dlg.setCancelable(false);
dlg.setTitle("Please Input Any Number");
dlg.show();
int decimal_value = Integer.parseInt(decimal.getText().toString());
List<Integer> binary_nums = new ArrayList<Integer>();
String result = "";
int nashti = 1;
for (int i = decimal_value; i >= 2; ) {
binary_nums.add((i % 2));
i = i / 2;
nashti = i;
}
binary_nums.add((nashti % 2));
for (int i = 0; i < binary_nums.size(); i++) {
result += "" + binary_nums.get(i);
}
binary.setText(new StringBuilder(result).reverse().toString());
}
catch(Exception e){
AlertDialog.Builder alert=new AlertDialog.Builder(this);
alert.setTitle("Error");
alert.setMessage("Please Input Any Number");
alert.setCancelable(true);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
}).show();
}
break;
case R.id.clear:
EditText decimal1=findViewById(R.id.decimal_value);
EditText binary1=findViewById(R.id.binary);
decimal1.setText("");
binary1.setText("");
break;
case R.id.shecvla:
Button shecvla=(Button) findViewById(R.id.shecvla);
TextView convertSign=(TextView) findViewById(R.id.convertSign);
if(shecvla.getText()==getString(R.string.dectobin)) {
shecvla.setText(getString(R.string.dectobin));
convertSign.setText(R.string.bintodec);
}
else{
shecvla.setText(getString(R.string.bintodec));
convertSign.setText(R.string.dectobin);
}
break;
}
}
}
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);
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'm trying to set width of AlertDialog with the following code :
final View view = inflater.inflate(R.layout.action_bar_main, null);
view.findViewById(R.id.friends_bar).setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MessagesPanel.this).setTitle(
"Messages").setAdapter(messagesAdapter,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Log.d("Button Clicked",
msgs_array.get(which)
.getTitle());
}
});
AlertDialog dialog = builder.create();
WindowManager.LayoutParams dialogLocation = dialog.getWindow().getAttributes();
dialogLocation.gravity = Gravity.TOP | Gravity.LEFT;
dialogLocation.x = (int) view.findViewById(R.id.friends_bar).getX();
dialogLocation.y = (int) view.findViewById(R.id.friends_bar).getY() + 40;
dialogLocation.width = 100;
dialog.getWindow().setLayout(50, 250);
dialog.show();
};
});
but it didn't work.. Note: x and y works fine.
I also tried :
dialog.getWindow().setAttributes(dialogLocation);
Thanks :)
I am trying to resize an AlertDialog, which I have done before, but cannot remember how I did it. Below is the code Ive used, but it just inst working. Is anyone able to point out what I am doing wrong.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder
.setMessage(R.string.checkout_journey_selector_popup_title)
.setPositiveButton(R.string.checkout_journey_selector_popup_paylater_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CheckoutHelper.startReservationJourney(getActivity(),
collectibleItems);
}
})
.setNegativeButton(R.string.checkout_journey_selector_popup_paynow_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CheckoutHelper.startPrepayCollectionJourney(getActivity(),
collectibleItems);
}
}).create().show();
Dialog alert = alertDialogBuilder.create();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alert.getWindow().getAttributes());
lp.height = 100;
lp.width = 50;
alert.getWindow().setAttributes(lp);
Thanks in advance
You should replace this
Dialog alert = alertDialogBuilder.create();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alert.getWindow().getAttributes());
lp.height = 100;
lp.width = 50;
alert.getWindow().setAttributes(lp);
With
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialogBuilder.getWindow().getAttributes());
lp.height = 100;
lp.width = 50;
alertDialogBuilder.getWindow().setAttributes(lp);
Or try this
alertDialog.getWindow().setLayout(600, 400); after alertDialog.show();
Try first and give me feedback
do like this
alert.getWindow().setLayout(width, height);
//then show it
alert.show();
instead of alert.getWindow().setAttributes(lp);