I have a dialog which currently opens on a button click and works fine but it means I have an ugly button that does not look good, I would prefer it to be opened from a CardView.
This is the card view:
CardView manager=findViewById(R.id.manager_card);
manager.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {}
});
And this is the dialog which currently opens from a button click:
Button btnLoginDialog;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
// Init Widget Button and set click listener
btnLoginDialog = (Button) findViewById(R.id.btnLoginDialog);
btnLoginDialog.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == btnLoginDialog) {
// Create Object of Dialog class
final Dialog login = new Dialog(this);
// Set GUI of login screen
login.setContentView(R.layout.login_gui);
login.setTitle("Login to Pulse 7");
// Init button of login GUI
Button btnLogin = (Button) login.findViewById(R.id.btnLogin);
Button btnCancel = (Button) login.findViewById(R.id.btnCancel);
final EditText txtUsername = (EditText)login.findViewById(R.id.txtUsername);
final EditText txtPassword = (EditText)login.findViewById(R.id.txtPassword);
// Attached listener for login GUI button
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(txtUsername.getText().toString().trim().equals("admin") && txtPassword.getText().toString().trim().equals("admin"))
{
// Validate Your login credential here than display message
Toast.makeText(SignInActivity.this,
"Login Sucessfull", Toast.LENGTH_LONG).show();
// Redirect to dashboard / home screen.
login.dismiss();
Intent intent = new Intent(getApplicationContext(), ManagerMenu.class);
startActivity(intent);
}
else
{
Toast.makeText(SignInActivity.this,
"Please enter valid Username and Password", Toast.LENGTH_LONG).show();
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
login.dismiss();
}
});
// Make dialog box visible.
login.show();
}
}
I can't figure this out. I hope there is enough information there for someone to help me out.
Use this type,this will help you:-
CardView manager=findViewById(R.id.manager_card);
manager.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog login = new Dialog(this);
// Set GUI of login screen
login.setContentView(R.layout.login_gui);
login.setTitle("Login to Pulse 7");
// Init button of login GUI
Button btnLogin = (Button) login.findViewById(R.id.btnLogin);
Button btnCancel = (Button) login.findViewById(R.id.btnCancel);
final EditText txtUsername = (EditText)login.findViewById(R.id.txtUsername);
final EditText txtPassword = (EditText)login.findViewById(R.id.txtPassword);
// Attached listener for login GUI button
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(txtUsername.getText().toString().trim().equals("admin") && txtPassword.getText().toString().trim().equals("admin"))
{
// Validate Your login credential here than display message
Toast.makeText(SignInActivity.this,
"Login Sucessfull", Toast.LENGTH_LONG).show();
// Redirect to dashboard / home screen.
login.dismiss();
Intent intent = new Intent(getApplicationContext(), ManagerMenu.class);
startActivity(intent);
}
else
{
Toast.makeText(SignInActivity.this,
"Please enter valid Username and Password", Toast.LENGTH_LONG).show();
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
login.dismiss();
}
});
// Make dialog box visible.
login.show();
}
});
Related
I have implemented a Alertdialog, but this dialog closes automatically after 1 second. He doesn't even pay attention to the buttons, but simply closes automatically. I can't find the error in the code, do you perhaps know where my error is?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText name = new EditText(MainActivity.this);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("Ok", null);
builder.setNegativeButton("Cancel", null);
builder.setView(name);
final AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Button btnOK = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button btnCancel = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
btnOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = name.getText().toString();
if (name.isEmpty() || name.matches("")) {
name.setError("ERROR");
} else {
SharedPreferences.Editor editor;
editor = sharedPreferences.edit();
editor.putString("NAME", name);
editor.apply();
//Switch to next activity
}
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
});
dialog.show();
What is wrong?
There is nothing wrong in the code. But I have a suspicion on the code you have given.
How to correct?
Instead of adding clickListeners on btnOk and btnCancel inside the onShownListener, you can add then outside the onShow method. It will not make any difference but I feel that that was closing the dialog.That is why I feel you shall try what I have mentioned.
Currently I am using the following code and created a custom dialog. And dialog opens when button is clicked,, but I want to hide the dialog by tapping on OK button..but its not working and not showing error...
I used 'dialog.dissmis'
Code is here:
final Dialog dialog =new Dialog(ActionBarActivity. this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_dictionary);
TextView word = (TextView)dialog.findViewById(R.id.txtWord);
final TextView wordMeaning = (TextView)dialog.findViewById(R.id.txtMeaning);
dialog.getWindow().getAttributes().windowAnimations = R.style.AnimLeftRight;
//Get Words and it's meanings.
word.setText(Dictionary.getWord());
wordMeaning.setText(Dictionary.getMeaning());
Button btntts = (Button)dialog.findViewById(R.id.btntts);
final Button ok = (Button)dialog.findViewById(R.id.btnPositive);
// Show the dialog first.
dialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
btntts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String convertTextToSpeech
wordMeaning.getText().toString();
convertToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
convertToSpeech.setLanguage(Locale.US);
convertToSpeech.speak(convertTextToSpeech, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
});
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
Please anyone help me to hide the dialog on button click..thanks
This is the proper way to code a custom dialog box.
//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View builderView = getLayoutInflater().inflate(R.layout.custom_alert_view, null);
//Set the layout inside of the builder
builder.setView(builderView);
//Show the dislog
final AlertDialog alert = builder.show();
//Get Button from the layout.
Button dismiss = (Button) builderView.findViewById(R.id.dismiss);
//Click the dismiss button from within the alert. will dismiss the dialog box
dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
I have written a function to handle showing of Dialog but I am not able to use OnClickListener in it. What is wrong with my code can any one tell me?
Here is my function
private void showInputDialog() {
final Dialog dialog=new Dialog(MainDashboard.this);
dialog.setContentView(R.layout.frg_dialog_change_pass);
btn_save_password=(Button) findViewById(R.id.btn_save_password);
btn_cancel_pass=(Button) findViewById(R.id.btn_cancel_pass);
edtOldpass=(EditText) findViewById(R.id.edtOldpass);
edtNewpass=(EditText) findViewById(R.id.edtNewpass);
edtConfirmpass=(EditText)findViewById(R.id.edtConfirmpass);
dialog.show();///Show the dialog.
btn_save_password.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainDashboard.this, "Success", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
}
Calling Activity.findViewById() will look for the View in the layout of your Activity (the one you've set by calling setContentView() in onCreate()).
I guess those Views are in your Dialog layout, so you need to call findViewById() on your Dialog instance:
btn_save_password = (Button) dialog.findViewById(R.id.btn_save_password);
btn_cancel_pass = (Button) dialog.findViewById(R.id.btn_cancel_pass);
edtOldpass = (EditText) dialog.findViewById(R.id.edtOldpass);
edtNewpass = (EditText) dialog.findViewById(R.id.edtNewpass);
edtConfirmpass = (EditText) dialog.findViewById(R.id.edtConfirmpass);
// Declare this globally above oncreate
private android.app.AlertDialog dialog;
android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(MainDashboard.this);
LayoutInflater layoutInflater = getLayoutInflater();
View alertView = layoutInflater.inflate(R.layout.frg_dialog_change_pass, null);
alertDialog.setView(alertView);
alertDialog.setCancelable(false);
Button btn_save_password= (Button) alertView.findViewById(R.id.btn_save_password);
btn_save_password.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do your stuff here
}
});
if(dialog !=null)
dialog.dismiss();
dialog = alertDialog.create();
dialog.show();
Change your function to:
private void showInputDialog() {
final Dialog dialog=new Dialog(MainDashboard.this);
View view = LayoutInflater.from(MainDashboard.this).inflate(R.layout.frg_dialog_change_pass);
dialog.setContentView(view);
btn_save_password=(Button) view.findViewById(R.id.btn_save_password);
btn_cancel_pass=(Button) view.findViewById(R.id.btn_cancel_pass);
edtOldpass=(EditText) view.findViewById(R.id.edtOldpass);
edtNewpass=(EditText) view.findViewById(R.id.edtNewpass);
edtConfirmpass=(EditText)view.findViewById(R.id.edtConfirmpass);
dialog.show();///Show the dialog.
btn_save_password.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainDashboard.this, "Success", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
}
Basically you have to use findViewById with the view which is used for dialog .
When I click the calculate button for the first time it works fine but on the second click after the Result Dialog has been dismissed the app crashes.logcat shows the error The specified child already has a parent. You must call removeView on the child's parent first. What should I do now? and how do I add the removeView?
public class MainActivity extends Activity {
float Remaining,Departure,TotUplift,SG,DiscResult;
int CalUpliftResult;
TextView RemainingTV,DepartureTV,UpliftTV,SGtv,CalcUpliftTV,DiscrepancyTV,resultOne,resultTwo;
EditText RemainingET,DepartureET,TotUpliftET,SGet,CalcUpliftET,DiscrepancyET;
Button calculateButton,okButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupView();
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialog,null);
resultOne=(TextView)textEntryView.findViewById(R.id.resultOne); //resultone is a textview in xml dialog
resultTwo=(TextView)textEntryView.findViewById(R.id.resultTwo);
alert.setTitle("RESULT");
alert.setIcon(R.drawable.ic_launcher);
alert.setView(textEntryView);
alert.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(validationET())
{
getETvalue();
evaluation();
CalcUpliftTV.setText(String.valueOf(CalUpliftResult));
DiscrepancyTV.setText(String.valueOf(DiscResult));
resultOne.setText("Calc. Uplift (KG)= "+String.valueOf(CalUpliftResult));
resultTwo.setText("Discrepancy(%)= "+String.valueOf(DiscResult));
alert.show();
}
else
Toast.makeText(getApplicationContext(), "please give all inputs", Toast.LENGTH_SHORT).show();
}
});
}
I fixed it, the alertdialog must be created inside the onclick of the calculate button, so that each time I click it, the dialog gets created all over again thus preventing interference from previous dialog values. here's the corrected code segment:
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(validationET())
{
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
LayoutInflater factory = LayoutInflater.from(context);
final View textEntryView = factory.inflate(R.layout.dialog,null);
resultOne=(TextView)textEntryView.findViewById(R.id.resultOne); //resultone is a textview in xml dialog
resultTwo=(TextView)textEntryView.findViewById(R.id.resultTwo);
alert.setTitle("RESULT");
alert.setIcon(R.drawable.ic_launcher);
alert.setView(textEntryView);
alert.setNeutralButton("OK",null);
getETvalue();
evaluation();
CalcUpliftTV.setText(String.valueOf(CalUpliftResult));
DiscrepancyTV.setText(String.valueOf(DiscResult));
resultOne.setText("Calc. Uplift (KG)= "+String.valueOf(CalUpliftResult));
resultTwo.setText("Discrepancy(%)= "+String.valueOf(DiscResult));
AlertDialog alertD = alert.create();
alertD.show();
}
else
Toast.makeText(getApplicationContext(), "please give all inputs", Toast.LENGTH_SHORT).show();
}
});
I have a simple text of code in my main.class file which creates a popup dialog. When I try to close the popupdialog with finish(); method, it shuts down the whole application, and not only the popup dialog box. How can i solve this ? ( this is for android, look at my comments below aswell ).
This is the code:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.start_dialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//set up text
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(R.string.loss);
//set up image view
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.golf_ball);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
Jesmar,
Hello and welcome.
You probably intended to use the dismiss function on your Dialog.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Cheers!
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
actually that finish() is belong to your activity, not your dialog. do dialog.dismiss() instead
Use cancel() or dismiss()
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
or
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});