Update result to TextView of AlertDialog Android - java

When I tried to set the result of my program to a textView in a dialog box , the app force closed. I made the dialog box by linking an xml which has a textview and it is this textview that I tried to update.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater factory = LayoutInflater.from(this);
resultOne=(TextView)findViewById(R.id.resultOne); //resultone is a textview in xml dialog
resultOne.setText("hello"); //this code is making the app close
final View textEntryView = factory.inflate(R.layout.dialog, null);
alert.setView(textEntryView);
alert.show();

Change the order so that you acess the View's children after inflating it. You will also need to use textEntryView to find the id, like so:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialog, null);
resultOne=(TextView)textEntryView.findViewById(R.id.resultOne); //resultone is a textview in xml dialog
resultOne.setText("hello");
alert.setView(textEntryView);
alert.show();

Related

Problem with blurred background on AlertDialog

I'm tried to blur background using this post:
https://stackoverflow.com/a/21278278/10837992.
It's work good but i have a little problem with the size of the dialog.
I'm created custom layout :
I'm changed the realization of the method.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_dialog_layout, null);
builder.setView(dialogView);
TextView title = dialogView.findViewById(R.id.Title);
TextView desc = dialogView.findViewById(R.id.Desc);
title.setText("Title");
desc.setText("Desc");
AlertDialog alert = builder.create();
Bitmap map = new BlurUtils().takeScreenShot(getActivity());
Bitmap fast = new BlurUtils().fastblur(map, 50);
final Drawable drawable = new BitmapDrawable(getResources(), fast);
alert.getWindow().setBackgroundDrawable(drawable);
alert.show();
While running this method, the size of the Dialog is cropped:
Screenshoot:
Ok , I find a solution:
Just added Theme to AlertDialog.Builder.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen);

I am creating a alert box in android studio

I am trying to build a alert box in android studio, i have 2 xml and 1 activity class where on click of a button i want to show the other layout.
I am getting a error, here is my code of MainActivity
cancelBookingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.alert_cancel_confirm, null);
final TextView disAgree = (TextView) alertLayout.findViewById(R.id.TextDisagree);
final TextView Agree = (TextView) alertLayout.findViewById(R.id.TextAgree);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Current Booking");
// This will set the view from XML inside ALertDialog
alert.setView(alertLayout);
// disabling cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
AlertDialog dialog = alert.create();
dialog.show();
}
});
In this line i am getting error
AlertDialog.Builder alert = new AlertDialog.Builder(this);
In Builder cannot be applied
Any idea why i am getting this.
AlertDialog.Builder alert = new AlertDialog.Builder(YourActivity.this);
alert.setTitle("Current Booking");
// This will set the view from XML inside ALertDialog
alert.setView(alertLayout);
// disabling cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
AlertDialog dialog = alert.create();
dialog.show();

Alertdialog setView() using existing xml widget [duplicate]

This question already has answers here:
How to implement a custom AlertDialog View
(11 answers)
Closed 7 years ago.
I have an EditText in a .xml file with LinearLayout and want to add it as the setView() parameter on an AlertDialog. Is this possible? Here's what I've tried:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
builder.setView(input);
But the dialog is blank when launched. What am I doing wrong?
Use it like this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_layout, null);
builder.setView(dialogView);
AlertDialog alertDialog = builder.create();
alertDialog.show();
using dialogView you can get your EditText like this
EditText editText = (EditText) dialogView.findViewById(R.id.your_edit_text);
It might help
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.YourLayout, null));
AlertDialog ad = builder.create();

Add a popup dialog over the call screen

How do I add a popup dialog over the call screen? I'll be using the BroadcastReceiver to listen for incoming calls and show it. I need an idea about how to write an activity that allows a dialog over an incoming call. Also, how do I make the dialog movable to any part of the screen? I already have the BroadcastRceiver implemented and performing other functions, so I could just use an intent and start the activity from this BroadcastRceiver
start an activity, then use AlertDialog Builder from that activity to prompt a dialog
set custom view to customize the dialog appearence
Try this
AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.caller_dialog, null);
ImageView button = dialogView.findViewById(R.id.close_btn);
builder.setView(dialogView);
final AlertDialog alert = builder.create();
alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
alert.setCanceledOnTouchOutside(true);
alert.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alert.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.TOP);
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//close the service and remove the from from the window
alert.dismiss();
}
});

How do I create a floating window using onClick attribute in Android?

So I'm working on an Android app (in Eclipse) and I've hit a wall. In my app I have a drawer that slides out with a list of options. I would like the user to be able to click one of the options and to bring up a floating window with a form in it. I'm trying to do this using the onClick attribute on the buttons rather than using a onClickListener. Is this possible without having to use a onClickListener or am I trying to avoid the inevitable? The button's onClick attribute in my layout has a value of "newWindow".
My MainActivity class
public void newWindow(View v){
Intent intent = new Intent(){
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
};
startActivity(intent);
}
I see my popupView variable is unused but I'm not sure where to place or if I'm even headed in the right direction. Thanks in advance for the help!
I can't for the life of me figure out why you are trying to start an Activity with an Intent here. Your code shold be:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(...); // or showAsDropdown(...)

Categories