I'll get straight into my problem; here is what I want the ProgressDialog box to be formatted as (this is an AlertDialog):
And this is my ProgressDialog with no button:
Created using:
dialog = new ProgressDialog(MainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Gathering all sent SMS messages...");
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
dialog.setTitle("Gathering...");
dialog.setCancelable(false);
dialog.show();
However, when I set a button like so:
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//do cancel stuff
}
});
It takes up way more white space than it should, and more than a normal dialog.
So does anyone know how to add a button to a ProgressDialog and not have a bunch of whitespace like this? Thanks in advance!
It has nothing to do with your design or code. It is about Google's design. If you check out the Design page for dialogs here. You can see that every dialog with a button has the same amount of space after the content.
But you can try to create your custom dialog and create your buttons yourself. This way you can put your buttons wherever you want.
Create your xml file under res/layouts/xml directory.
Create your dialog like this:
final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.custom);
Related
I am working on an application for my personal project.
I plan to have a table/chart in my application, and next to the application I plan to put a button which is what I need help with.
Could you please tell me what code I can use to create a button which when clicked:
Creates a new window(popup or not is fine) where there is a editText
for the users to input some text and when they are done the text is
inserted into a cell of the table?
Maybe useless information:
My application's "table" should be borderless and maybe instead of table is list with separators between the user's text
The application is essentially a to do list but when they click the button to insert a new task it is in a new window.
Create an AlertDialogBox
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext(), );
builder.setMessage("Message")
.setIcon(R.drawable.icon)
.setTitle("Title");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Todo
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Todo
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
When you put a button in the XML code you need to fetch it by it's id in the activity (java class), then use
button.onClickListener( v -> {
//here you can have a code block that will be done once the button is clicked
//put Ashutosh Sagar code for the alert dialog
Keep in mind that you can make the dialog as you want by customizing it to your preferences. This usually requires another XML layout just for the look of the dialog and some more reading on Alert Dialog.
As it was said above by Aman, we can clearly post you the code but that way you won't learn anything. Happy coding.
I want to open a small popup or something like that on a button click such that the popup contains two buttons which on clicking goes to their corresponding activities.
Please provide the code as i am totally new to android?
Yes you can do this with Dialogs
use Below Code to make dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//Uncomment the below code to Set the message and title from the strings.xml file
//builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);
//Setting message manually and performing action on button click
builder.setMessage("Do you want to close this application ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("AlertDialogExample");
alert.show();
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.
find details of dialogs in below link:
Dialogs
You can use material alert dialog for this
Check this!
I need to make a math quiz. Here is workflow:
On button click(Start) i need to show a question e.g. 5+5, which stays on screen for 2 seconds after which a dialog i shown asking for result of previous operation. And i need to repeat that for e.g. 5 times. At the end I need to show to user number of correct answers.
I have Java experience but I'm new to Android. I have read various articles and examples regarding Android threading but haven't found solution to this workflow.
As far as I know I cannot block or pause main/UI thread, but the problem is that i should repeat questions for e.g. 5 times and after each one(after 2 sec.) I should pause activity to show Dialog and then go back to showing another question.
Thanks in advance!
EDIT:
Here is my code for now. The number is shown on screen and after 2 seconds alert dialog is shown asking to enter that number. But the problem is that the application doesn't wait for user input but continues to show new random number an opens new alert dialog every 2 seconds.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
tv = (TextView) findViewById(R.id.questionText);
handler = new Handler();
Runnable r = new Runnable() {
public void run() {
tv.setText(String.valueOf(rand.nextInt(50) + 1));
createDialog();
handler.postDelayed(this, 2000);
}
};
handler.postDelayed(r, 2000);
}
protected void createDialog() {
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.activity_answer_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
PS. Also I tried to create separate Activity for dialog and added this line to Manifest:
android:theme="#android:style/Theme.Dialog"
but the same thing happens.
You use event driven programming and timers. To show the dialog after 2 seconds, the easiest way is to post a runnable to a handler using postDelayed, which will call your runnable in N milliseconds. Then display the dialog box.
You don't need to pause the activity to show the dialog box, just show it. Any code that you want to run after the dialog is finished with should be put in the handler for the dialog's ok button. In other words, your code is called in response to the event of a button being pressed.
I need help with the dialog interface in android. i don't get it.. i' ve searched here to, i got many answers but with every code i used from here my application crashed..
So I make a notes app an you can choice a with an alert dialog which type of note you want.
The dialog window is black. So can someone show me how to chage the color maybe simple in white so that i understand how it works?
here is my code:
private void showNewNoteChoices() {
final CharSequence[] items = {
getResources().getString(R.string.text_note_type),
getResources().getString(R.string.log_note_type),
getResources().getString(R.string.important_type),
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select type of note");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
loadNoteFragment(item, newNoteTitles, null);
}
});
AlertDialog alert = builder.create();
alert.show();
}
I know that i have to make an xml file for the specific layout and i have to make an style . Can someone show me how I can make my dialog interface in white?
I would recommend you using Dialog. You can style it the way you want.
For example, you create a custom dialog with custom layout like this;
final Dialog dialog = new Dialog(MainActivity.this);
dialog .setContentView(R.layout.custom_layout);
Then you can create views and listeners;
Button button = (Button) dialog.findViewById(R.id.button);
//Other views and listeners etc..
Finally you show it;
dialog.show();
i know, similar questions have been asked, and i've already looked through everything i could find, but i didn't find any answer to this problem.
Here's the Code:
protected Dialog onCreateDialog(int id)
{
Dialog dialog = new Dialog(this);
switch(id)
{
case R.layout.database_feed:
dialog.setContentView(R.layout.database_feed);
((Button) dialog.findViewById(R.id.discard_button)).setOnClickListener(
new View.OnClickListener()
{
//#Override
public void onClick(View v)
{
//dialog.cancel();
}
}
);
break;
}
return dialog;
}
I simply want to close the Dialog on a click on R.layout.database_feed button. But i don't have acces to the dialog within the onClick-method. I really feel confused.
I don't want to use an AlertDialog or a DialogBuilder, because there are other things in the Dialog that are difficult to implement in an AlertDialog or something.
Also, i already know the solution to make a separate Activity for the Dialog - but actually i want to know how it works the way i'm trying here.
Moreover i have already tried to use a DialogInterface.OnClickListener() but i can't use that in the setOnClickListener(...)-Method.
Just cancelling the dialog can't be that hard... but i don't get it.
Any hint/help is appreciated!
Thx
Change
Dialog dialog = new Dialog(this);
to
final Dialog dialog = new Dialog(this);
Then you can access dialog in your onClick() method.
Either store the 'dialog' as a class variable, or make it final in your method.