alertdialog shows no text - java

I have a problem creating an AlertDialog.
No matter what i do, the title and the message are always empty
here is my code:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(BigActivity.this);
dialogBuilder
.setTitle(item.getTitle())//no problem whith getters
.setMessage(item.getMessage())
.setIcon(R.drawable.ic_launcher)
.setNegativeButton(R.string.cerrar, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
;
AlertDialog dialog = dialogBuilder.create();
dialog.show();
any help would be appreciated
P.S.
System.out.println(item.getTitle()+" "+item.getMessage())
works ok.

You forgot to add show()
Dialog.show();
It would also be better if you follow the coding guidelines to use a lowercase letter for the first variable name character.

Assuming that you are able to see the Dialog window (and you are not calling this in the background thread) then the only possible reason could be is that both text and background colors are same. Try to change the text color using either of the following method:
Using Inline HTML
.setMessage(Html.fromHtml("item.getMessage()")
Custom layout theme.
link

Try to convert it to string using toString() function like:
.setMessage(item.getMessage().toString())

Related

How to Create Custom Dilaog on Button Click

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.

How to test AlertDialog item click in Robotium for Android Studio

I have an AlertDialog as below, I don't know how to test it with Robotium in Android Studio. Can anyone give me a hint for that?
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle("Select");
final String[] items = {"Take a picture using carmera", "Choose a picture from Album"};
alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i == 0) {
...
...
See this answer to a similar question:
This works for me:
solo.clickOnView(solo.getView(android.R.id.button1));
where the 'Positive' button is android.R.id.button1, the 'Negative'
button is android.R.id.button2 and 'Neutral' is android.R.id.button3.
It means that for your AlertDialog you would need to use the solo.clickOnView(solo.getView(dialogId)) method.
Check out also this answer to a similar question:
lets say you have some code like this
solo.clickOnView(view1);
solo.clickOnView(view2);
and you know the dialog can appear between these two steps of your test, you can place in code that is something like:
if(solo.waitForView(dialogView, 1000, false)){
solo.clickOnView(dialogDismissButton);
solo.clickOnView(view2) //retry the step above
}

How to style AlertDialogs android

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();

How do i implement a cancel-button in a Custom Dialog without AlertDialog?

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.

how to set an alert box

in my app i have placed three edit boxes with a ok button. Before pressing the ok button the user must enter all the edit text fields, if any of the edit text is been left empty i want to give an alert box.
in the edit text box the field name such as "Name", "Age" should be in diminished and when it is clicked it must get disappeared.
How to do this , pls anyone help me
Check length:
if (edit1.getText().length() > 0 && edit2.getText.length() > 0 && edit3.getText.length() > 0) {
// Do your normal code here
} else {
// Call your alert dialog creation
}
Diminished? You mean a hint (which is shown when there's no text in the field)? This is done like this...
XML inside the EditText field:
android:hint="Clear by clicking"
Source code:
nameEditText.setHint("Clear by clicking");
Remove text on click (if you have already created an EditText field called nameEditText):
// Clear text when clicked
nameEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
nameEditText.setText("");
}
});
And then do what Vladimir said
Just use AlertDialog. Check all the conditions and if there is an error build a dialog and show it.
you can design this ui in your activity, and this activity should have the theme android:theme="#android:style/Theme.Dialog". And when you want your activity to disappear. simpally call finish()
its simple..
for name checking :
if(editname.getText().tostring().length==0)
show alert...
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("something,");
builder.setMessage("something..");
builder.show():
you can also add button...by
builder.setNeutralButton("name",new DialogInterface.onclick
............}
just try this
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Error Msg).setPositiveButton("OK", alertClickListener).show();
DialogInterface.OnClickListener alertClickListener = new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
}
};
In your Ok Button OnClick do the following
if (et1.getText().toString().length() != 0) {
emailid = String.valueOf(et1.getText());
}
if((emailid==null|| emailid=="")){
tvError.setVisibility(View.VISIBLE);
tvError.setText("All fields are Mandatory");
Toast.makeText(Signin.this,"All fields are Mandatory", Toast.LENGTH_SHORT).show();
}else{
// Your operation
}
where et1 is Edit box 1,emailid is String ..
In your XML file create a textview with option android:visibility="GONE"..
Now in if part Make that textview visible if error occurs or do your process in Else..
Also you can keep toast mesaage...
use this it is best as compared with all validation check

Categories