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
}
Related
I'm trying to build an Dialog in android studios with Java.
But the problem is, that however I try it the Error
"D/InputTransport: history resample interval is too short, cannot uses it to resample!!"
appears.
I already searched for a solution, but I found nothing similar.
I also tried it with another mobile phone, but it didn't work as well.
Here is my source-text:
public void add(View view){
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("title");
alertDialog.setMessage("text");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
The method "add" is opened by a button.
I would be happy about every help!
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 am trying to run a "presence check" on a radio group, to determine what happens if 1 of 2 radiobuttons are selected in the group (if statement), if the other of the 2 radiobuttons is selected instead (else if statement) or if neither are selected (else statement). The code for this is as follows:
if (rdbAM.isSelected()) {
strTime = rdbAM.getText().toString();
} else if(rdbPM.isSelected()){
strTime = rdbPM.getText().toString();
} else {
AlertDialog.Builder WrongDateFormat = new AlertDialog.Builder(this);
WrongDateFormat.setMessage("Please Select AM or PM");
WrongDateFormat.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertWrongDateFormat = WrongDateFormat.create();
alertWrongDateFormat.show();
return;
}
So basically, what this should do is either, set the variable called "strTime" to whatever the text of the selected radiobutton in the radiogroup is, or display an error message if neither are selected. It is instead always displaying this error message, regardless of whether either radiobutton is selected or not:
(As you can see above, the "AM" radiobutton is selected, but error is still being displayed).
Any suggestions as to why this may be would be appreciated. Please note that I am relatively new to Android development, so if it is clearly something obvious then I apologise, but I have been trying to get my head round this for several days now! If you would like to see any further code, please let me know and I'll be happy to provide it, but am trying to keep it as private as possible, so didn't want to post everything in the initial post if not necessary. Thanks in advance.
According the documentation the RadioButton extends the CompoundButton that offers the method isChecked(). However there is poorly described the difference from the method isSelected() from the extended class View that might be confusing.
Do the following and it should work:
rdbAM.isChecked();
Instead of using isSelected() go for isChecked().
A RadioButton is a two-states button that can be either checked or unchecked. For this compound button, you should use method isChecked() to know its current state.
See documentation.
Update your code as below:
if (rdbAM.isChecked()) {
strTime = rdbAM.getText().toString();
} else if(rdbPM.isChecked()){
strTime = rdbPM.getText().toString();
} else {
AlertDialog.Builder WrongDateFormat = new AlertDialog.Builder(this);
WrongDateFormat.setMessage("Please Select AM or PM");
WrongDateFormat.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertWrongDateFormat = WrongDateFormat.create();
alertWrongDateFormat.show();
return;
}
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 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())