So I'm trying to get my application to show an editable text-field in a dialog. Once you've entered your desired text, you can hit 'OK' to use that text to go to the next view and do something based on that text.
So this is what my code looks like.
public void onClick(View v){
final EditText input = new EditText(MainActivity.this);
final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("New Query");
alertDialog.setView(input);
alertDialog.setPositiveButton("Fire Query!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
TableView.putExtra("query", input.getText());
startActivity(TableView);
}
});
alertDialog.show();
}
However, the compiler is telling me that it can't resolve setPositiveButton() and in fact when I look look in the code-complete box, it indeed does not show even though it is listed for the builder on the Android documentation
Any ideas? I should mention that everything but the setPositiveButton is working. I just can't do anything with a dialog with a title and an editText field.
You are not calling it on the builder. Move the create() call to later on (and change the type to the Builder).
Related
I am trying to create a very simple dialog box. I found the code references from the internet obviously.
My code is this:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
ConsignmentConViewActivity.this);
alertDialog.setTitle("Alert Dialog");
alertDialog.setMessage("Welcome to AndroidHive.info");
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialogMain = alertDialog.create();
alertDialogMain.show();
The youtube videos and google link that talks about dialog boxes and are similar to the code above. It even works for the people showing the demo on youtube.
But when I run it, the dialog box appears for half a second like I can see it. and then closes automatically. It appears and then its gone.
I have also tried to reboot my system. Nothing works.
Yes because you created it as it shows.
Why are you declaring a new variable as the below code if you already declared alertDialog above?
AlertDialog alertDialogMain = alertDialog.create();
alertDialogMain.show();
This problem is showing because you are declaring a new variable when you have already declared it. Make your code as below I maintained.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
ConsignmentConViewActivity.this);
alertDialog.setTitle("Alert Dialog");
alertDialog.setMessage("Welcome to AndroidHive.info");
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
alertDialog.create();
alertDialog.show();
OR USE DIRECT WITHOUT DECLARING VARIABLE
new AlertDialog.Builder(this)
.setTitle("Alert Dialog")
.setMessage("Welcome to AndroidHive.info")
//.setIcon(R.drawable.ic_round_warning) // set your dialog icon if you want to show. JJust uncomment.
.setPositiveButton("OK", (dialogInterface, i) -> {
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
//dialogInterface.dismiss(); // for dismiss dialogbox
}).create().show();
Let me know if the problem not solved yet.
After grinding on it for almost the whole day, I realized that I have an Intent function right after the dialog box.
The intent function was switching to the next activity.
By the time the dialog would appear, the next code (which was the intent) would run. The activity would change and thats why I was getting that problem.
It works all fine when i removed the intent.
My code (in the question) is all correct and has no problem in it.
I'm trying to create a popup.
I would like to make the background of my popup transparent, and the border, because the popup is not filling the page, a bit more opaque, is there an easy way to do this?
I've tried with an activity but i can't get the effect i want, can you help me?
Something like this:
You can use Alert Dialogs for this and check this link for different uses
And a simple one like this
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Alert Dialog Title");
builder.setMessage("Your Message");
builder.setNegativeButton("No", null);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
result
I need to create a popup window programmatically, with a scrollview is this possible? i need to do everything in java side.
I was using a alert dialog but maybe is better a popup window, but didn't find much information on how to do it programatically.
i was using this code for the alert dialog
AlertDialog.Builder alert = new AlertDialog.Builder(OFActivityA.this);
alert.setTitle("Something");
alert.setText("fe");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//what you need to do after click "OK"
}
});
alert.show();
i need to do everything in java side., not really - you can create your custom dialog with a custom layout and have any kind of layout that you would like to have.
For example, create dialogClass:
public class ProgressDialog extends Dialog {
public ProgressDialog(#NonNull Context context) {
super(context);
setContentView(R.layout.progress_dialog); //this is your layout for the dialog
}
}
And all you need to do to show your dialog is call those line:
ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.show(); // this line shows your dialog
You can put all your logic into the class using java as you want to only that now you can control your layout and how it looks in easier way.
I created two fairly long functions which create an AlertDialogBuilder which builds an AlertDialog which is populated and launched. The alert dialogs produced are fairly large for dialogs, so a lot of views need to be populated within the dialog. For the sake of cleanliness, testability, SRP etc. I decided to move these functions into a new class.
Originally the functions were placed directly in the Activity class, and the alert dialogs launched fine. I've now moved both to an AlertDialogLauncher class, which takes an Activity parameter when launched, most of the original code is the same, I've got the alert dialog working, but the colours of the text and background colours of some of my views are off.
So the dialog is launching fine, just with incorrect colours, so I imagine it is loading an incorrect style or something similar?
Code...
Original version (shortened)
private void addNormalRow(final ScannedWiFiNetwork network) {
TableRow row = (TableRow) View.inflate(this, R.layout.regular_network_table_row, null);
// loads of code
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
launchNewNetworkDialog(network);
}
});
}
private void launchPreferredNetworkDialog(final ScannedWiFiNetwork network) {
final AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Connect to "+network.getSsid()+"?")
.setView(getLayoutInflater().inflate(R.layout.preferred_network_dialog,null))
.setCancelable(false)
.show();
final EditText passwordInput = (EditText) dialog.findViewById(R.id.edit_text_password);
TextView passwordText = (TextView) dialog.findViewById(R.id.dialog_password);
//loads of code
}
Essentially, the launch dialog function is called and creates a new AlertDialog by passing the AlertDialog.Builder a reference to this (the Java class for my activity).
Refactored Activity/Interface class
private void addNormalRow(final ScannedWiFiNetwork network) {
TableRow row = (TableRow) View.inflate(this, R.layout.regular_network_table_row, null);
//loads of code
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Activity activity = (Activity) v.getContext();
dialogs.launchNewNetworkDialog(network, activity);
}
});
}
New AlertDialogLauncher class
public void launchPreferredNetworkDialog(final ScannedWiFiNetwork network, final Activity activity) {
Context con = activity.getApplicationContext();
LayoutInflater layoutInflater = LayoutInflater.from(con);
final AlertDialog dialog = new AlertDialog.Builder(activity)
.setTitle("Connect to "+network.getSsid()+"?")
.setView((layoutInflater.inflate(R.layout.preferred_network_dialog,null)))
.setCancelable(false)
.show();
As you can see, in this new class an Activity must be passed in to be used to create the layout inflater and context objects used later on to populate the views. All this works fine, however as mentioned earlier, the text colours change and the background of the buttons.
I'm loading the same xml layout file, so I'd assume it is loading an incorrect or default style somehow when I provide it with a default activity object (retrieved from calling getContext on the button view added to the dialog).
I've tried replacing 'activity' with NetworkListActivity.this (my activity name), that compiles but gives me the same ruined style outcome.
Thanks in advance for any help. Hope the question is clear!
Figured out my problem.. I never applied a style to my dialog xml layout file, some of the text views which I did not assign explicit text colors two are defaulting to the style of the dialog, I found this out when experimenting with changing the style which can be done by adding R.style.STYLE_NAME to the AlertDialogBuilder constructor (after the 'activity' parameter).
So yeah, I just didn't apply a style and need to be careful with views I haven't applied a style to!
This is my ProgressDialog
progressBar = new ProgressDialog(Wallpapers.this);
progressBar.setCancelable(false);
progressBar.setMessage("Downloading " + downloadedFile + ".png");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
progressBar.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
progressBar.show();
Given that i can force the Buttons to become VISIBLE INVISIBLE GONE with the following:
progressBar.getButton(ProgressDialog.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
I had hoped to be able to do the same with the ProgressBar, this would allow more room for text to describe which button to press after the task is completed.
But the following is my best guess and its creating a NullPointer
progressBar.getButton(ProgressDialog.STYLE_HORIZONTAL).setVisibility(View.INVISIBLE);
Please note I am not looking to close the Dialog, only force the ProgressBar to be Invisible.
If it cannot be done then so be it, but any help would be great
ProgressDialog.STYLE_HORIZONTAL and ProgressDialog.STYLE_SPINNER are not Buttons, nor are they identifiers for Buttons in the AlertDialog class.
If you really want to stick with a ProgressDialog, you could hack your way around it by getting a View within the dialog (e.g. a Button or the View that has focus via getCurrentFocus()), then get the root View of the Dialog and traverse it's children until you find a ProgressBar. I wouldn't recommend this.
A better alternative would be to create your own layout that includes a ProgressDialog, and set that as an AlertDialog's View with setView(). This way you can define your own ID for the bar and retrieve it via the Dialog's findViewById() method.