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
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 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;
}
Hope you all are well.
I would like to hide a Button (btnAppointment) if my test readings in my EditText are below or equal 13.5 or greater than 33 in my Android Application window. The Button (btnAppointment) should only appear on the screen if my EditText field inputted values are between 13.60 and 32.99. And not be shown onscreen if outside these parameters.
I was wondering whether an IF statement with button.setEnabled(false); would do the trick and if so where about would I need to input it into my Code. Whether it be protected void onCreate(Bundle savedInstanceState) or Create my own public void appointmentTeacherOnClick?
Below I have inserted my code for calculating and displaying my Inputted Test field prompts.
public void calculateTest(View v){
String status;
test = Double.parseDouble(edtData.getText().toString());
String result = String.format("%.2f", test);
Log.d("MyActivity", result);
if( test < 9.5) {
status = "Normal - Well Done =)";
} else if (test >= 9.5 && test < 13.5){
status = "Caution - Keep on Track =|";
} else if (test >= 13.5 && test < 33.0) {
status ="Action Needed =(";
} else {
status = "Normal Results are between 0 - 33";
}
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Result Feedback...");
alertDialog.setMessage(status);
alertDialog.setButton("Acknowledged", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
if you want to control the visibility of button, enabling or disabling the button won't help. Enabling/Disabling property will still show the button and will only decide whether button can be clicked or not.
You need two things to achieve your task,
EditText - text change listener
Button Visibility property
How to do both the task are already answered on SO, here is the most popular one,
How to hide a button programmatically? (For hiding button).
Counting Chars in EditText Changed Listener (For creating edittext change listener)
To hide a view you need to use setVisibility() and set it to View.INVISIBLE
Button btnAppointment = (Button) findViewById(R.id.btn_appointment);
btnAppointment.setVisibility(View.INVISIBLE);
In your XML file:
<Button
android:id="#id+/btn_appointment
...
/>
See this question: How to hide a button programmatically?
I am following a youtube tutorial and I've got most of the works done, but I still got some problems.
I have my custom layout for my custom dialog, all I wanted to do is to set the custom dialog on a button. Once we click the button ,the dialog shows, that's it. I've already set the onclicklistener on the button, here's my code.
Credit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog credit = new Dialog(Main.this);
credit.setContentView(R.layout.creditdialog);
credit.setTitle(" ");
credit.show();
}
});
I followed all of this on a tutorial, but I don't know that the "MAIN" is about, I got an error there. Please tell me what to do. Sorry for my poor English.
new Dialog(Main.this);
The above line creates a new dialog object and associates it with the context of your Activity. SO you have to pass the context of your activity in the paranthesis..
Eg:
If you are calling the dialog from Activity "ActivityMain".. then use:
new Dialog(ActivityMain.this);
try this
Credit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog credit = new Dialog(getApplicationContext());
credit.setContentView(R.layout.creditdialog);
credit.setTitle(" ");
credit.show();
}
});
it is common question! but you can extend dialog box and can set custom layout for your dialog than on button click just call dialog like this:
new CustomDialog(activity).show();
also you can follow this tutorial on custom dialog to understand how to customise dialog and how to use it on button click.
http://www.shaikhhamadali.blogspot.com/2013/09/types-of-dialogbox-part-two-custom.html
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())