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();
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'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);
Following... this link ..How to set image as wall paper in viewpager app?. I am able to set wallpaper directly located in my drawable folder. However, i want to give user a chance to set wallpaper by displaying pop up dialogue box which should be displayed. When user clicks on images for 3-5 sec.
I am kind of new to android programming.. So, please help..
android framework already supporting class "alertDialogue.builder". you can set message whatever you wanna show in addition to dialogue buttons, title etc.
http://developer.android.com/reference/android/app/AlertDialog.Builder.html
5sec is too long to stay in one fingerpoint. in onsimplegesturedetector class, there is already "long press" detector
http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html
you can set your own ontouchListener that implements onTouchListener that has gesture detector, detects long press and can show dialogue .
there can be better way but I'm using this logic in my project so you can reference and fix it if you find better way
findViewById("your wall paper image id").onTouchListener(new MyOnTouchListener());
class MyOnTouchListener implements onTouchListener{
GestureDetector gd = new GestureDetector(new SimpleOnGestureListener(){
#Override
public void onLongPress(MotionEvent e) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Your Title");
alertDialogBuilder
.setMessage("click yes to set wallpaper!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//setting wallpaper
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.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.