How to show Cancel and Accept button in AlertDialog Android - java

I am having troubles setting the two buttons on the lower part of the alert what I am trying to do is get a similar look to the IOS look of the buttons.
here is my current code:
public void openInstructions() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setNewDateOnView();
dialog.cancel();
}
}).setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).create();
TextView myMsg = new TextView(getActivity());
myMsg.setText("ExampleText ");
myMsg.setTextSize(15);
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
dialog.setView(myMsg);
dialog.setTitle("Confirm Date of Purchase");
dialog.setOnShowListener( new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface arg0) {
Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setTextColor(0xFFFF0000);
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setTextColor(0xFF0000FF);
}
});
dialog.show();
}
Example of how it looks right now:
How I wanted it to look:

To design Dialog like ios , we can create custom dialog using custom layout like this :
dialog_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical"
app:layout_constraintHeight_max="wrap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/custom_bg">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/title"
android:gravity="center"
android:text="Confirm Date of Purchase"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="17sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/back_ll"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/rebooking_single_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textAlignment="center"
android:text="#string/dummuy"
android:textColor="#android:color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/back_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/end_time_recycle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#8F8F8F" />
<LinearLayout
android:id="#+id/rebooking_back_button_ll"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="#+id/ok_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".49"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Ok"
android:textAlignment="center"
android:textColor="#FF6363"
android:textSize="17sp"
android:textStyle="bold" />
<View
android:layout_width="0dp"
android:layout_weight=".01"
android:layout_height="match_parent"
android:layout_below="#+id/end_time_recycle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#8F8F8F" />
<TextView
android:id="#+id/cancel_btn"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAlignment="center"
android:paddingRight="10dp"
android:text="Cancle"
android:textColor="#7BC5FF"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Main_Activity
final Dialog dialog=new Dialog(FirstActivity.this);
dialog.setCancelable(false);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setContentView(R.layout.dialog_custom);
TextView cancel_btn=dialog.findViewById(R.id.cancel_btn);
cancel_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
dialog.dismiss();
}
});
TextView ok_btn=dialog.findViewById(R.id.ok_btn);
ok_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
dialog.dismiss();
}
});
dialog.show();

Well, as a start, google recommends you use the Android Design Guidelines when creating Android apps, really you should be sticking to those as that's what your users will be used to (not Apple's).
Also there's no reason to be assigning the dialog a TextView if all you're doing is showing text, the AlertDialog provides this functionality with setMessage.
If that hasn't convinced you to stick to the normal design rules, the only other option would be creating a Custom View and assign it to the AlertDialog with setView(similar to how you're doing it with the TextView).
Here's a tutorial on creating a Custom View

You can create a custom view such as the one sent and inflate it
Example:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
View mView=getActivity().getLayoutInflater().inflate(R.layout.custom_layout, null);
mBuilder.setView(mView);
Button close = (Button) mView.findViewById(R.id.close);
Button OK = (Button) mView.findViewById(R.id.ok);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//close the dialog
hideDialog1();
}
});
OK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
dialog = mBuilder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.show();

Use .setNeutralButton that make place left most
TextView textview;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button)findViewById(R.id.button1);
textview = (TextView)findViewById(R.id.textView1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.ic_launcher_background)
.setTitle("Alert Dialog Box Title")
.setMessage("Are you sure( Alert Dialog Message )")
.setPositiveButton("YES", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on Yes", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on Cancel", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("NO", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on NO", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
}
===============================================

Related

Custom Layout button is not working in an AlertDialog

I've been trying to fix this for a while now including searching online but I just can't find solution. Everyone saying you should add onClickListener and I did but it just does not work. When I click the button nothing happens. No verbose log ever appears. This is all in one class which implements Runnable so it can do stuff in the background and this EditText onClickListener is implemented inside onCreate of a class.
//Setting up EditText onClick Listeners
oof.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Creating custom layout dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final View inflatedView = getLayoutInflater().inflate(R.layout.dialog_box_popup, null);
builder.setView(R.layout.dialog_box_popup);
builder.setTitle("Edit");
final EditText indc = (EditText)inflatedView.findViewById(R.id.weightInDcEtxt);
Button incrementBtn = (Button)inflatedView.findViewById(R.id.incrementBtn);
Button decrementBtn = (Button)inflatedView.findViewById(R.id.decrementBtn);
//Enabling the increment button in a dialog
incrementBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("increment", "Button clicked: " + MiscVariables.rocket);
}
});
//Displaying dialog
AlertDialog dialog = builder.create();
dialog.show();
}
});
And here is the custom layout for the dialog using ConstraintLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/decrementBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="-"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/incrementBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="+"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/weightInDcEtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:gravity="center"
android:inputType="numberDecimal"
android:text="#string/editTextFromDialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/incrementBtn"
app:layout_constraintStart_toEndOf="#+id/decrementBtn"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayou
From there I have no idea what to do. If anyone can help me out I would appreciate it thanks!
I've found a solution. Here is the code:
//Setting up EditText onClick Listeners
oof.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Creating custom layout dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(R.layout.dialog_box_popup);
builder.setTitle("Edit");
//Enabling the increment button in a dialog
incrementBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("increment", "Button clicked: " + MiscVariables.rocket);
}
});
//Displaying dialog
AlertDialog dialog = builder.create();
dialog.show();
final EditText indc =
(EditText)inflatedView.findViewById(R.id.weightInDcEtxt);
Button incrementBtn =
(Button)dialog.findViewById(R.id.incrementBtn);
Button decrementBtn =
(Button)dialog.findViewById(R.id.decrementBtn);
}
});
Inflated view was useless here. All had to be accessed by doing dialog.findViewById.
final View inflatedView = View.inflate(getActivity(), R.layout.dialog_box_popup, null);
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setView(inflatedView);
final EditText indc = (EditText) inflatedView.findViewById(R.id.weightInDcEtxt);
Button incrementBtn = (Button) inflatedView.findViewById(R.id.incrementBtn);
Button decrementBtn = (Button) inflatedView.findViewById(R.id.decrementBtn);
//Enabling the increment button in a dialog
incrementBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Hellloo", Toast.LENGTH_SHORT).show();
}
});
alert.show();
Try this, Hope this solution will be work...!!!

How to call removeView() on dialog box number one so as to show dialog number 2

I am working with an application that involves to dialog boxes that should appear one after the other. What I mean is that, when I click on the positive button of the first dialog box it should then bring up another second dialog box. The issue I am facing is with the second dialog box, the second one will not pop up and it causes the application to crash.
I get an error that says "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
public static void showBusinessOrPrivateStartDialog(final Context context, final CallLogEntry call)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
// If new version of android, then check permission
if (!Settings.canDrawOverlays(context))
{
Log.e(TAG, "Permission Denied: Draw overlays (for popup)");
return;
}
}
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View view = inflater.inflate(R.layout.dialog_call_type, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context, Theme_Material_Light_Dialog_Alert);
builder.setTitle("Was this a private or a business call?");
builder.setMessage("Please select a call type");
builder.setIcon(R.drawable.ic_deviceinsight);
builder.setCancelable(true);
//builder.setView(view);
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// Occurs when user cancels the dialog, or clicks somewhere else in the screen or presses the back button
Log.i(TAG, "cancelled");
saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, "", true, "");
}
});
builder.setPositiveButton("Business", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
CallClassification.showBusinessCallPopup(context, call);
}
});
builder.setNegativeButton("Private", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
saveCallEntry(context, call, CLASSIFICATION_TYPE_PRIVATE, "", false, "");
}
});
AlertDialog dialog = builder.create();
// Rather use something like TYPE_SYSTEM_ALERT, if possible.
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // TYPE_SYSTEM_ERROR
dialog.show();
}
This is the second method for the second dialog box and my app crashes on dialog.show
public static void showBusinessCallPopup(final Context context, final CallLogEntry call)
{
// NEW NRF
if (AppPreferences.isNRF(context)) {
CallItem callItem = saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, "", false, "");
// SHOW NEW FORM
Intent intent = new Intent(context, CallClassificationFormActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
EventBus.getDefault().postSticky(callItem);
context.startActivity(intent);
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
// If new version of android, then check permission
if (!Settings.canDrawOverlays(context))
{
Log.e(TAG, "Permission Denied: Draw overlays (for popup)");
return;
}
}
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_call_details, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context, Theme_Material_Light_Dialog_Alert);
String toOrFrom = call.Type == CallLog.Calls.INCOMING_TYPE ? "from" : "to";
//builder.setTitle("Business call");
builder.setCustomTitle(view);
//builder.setMessage("Please enter reference details for the call " + toOrFrom + " " + call.Number);
//builder.setIcon(R.drawable.ic_deviceinsight);
builder.setCancelable(true);
builder.setView(view);
final EditText etReference = (EditText)view.findViewById(R.id.etReference);
final EditText etComment = (EditText)view.findViewById(R.id.etComment);
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String reference = etReference.getText().toString();
String comment = etComment.getText().toString();
saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, reference, false, comment);
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
Log.i(TAG, "cancelled");
}
});
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // TYPE_SYSTEM_ERROR
dialog.show();
}
and here is my xml. I have included the xml in this post because I read somewhere that the way the height and width of my textboxes is defined
might be causing an issue.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
tools:context=".calls.CallClassification">
<TextView
android:id="#+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:text="Please enter reference details for the call "
android:textColor="#color/colorPrimaryDark"
android:textSize="16sp"/>
<AutoCompleteTextView
android:id="#+id/etReference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:hint="Matter Code *"
android:imeOptions="actionNext"
android:inputType="text"
android:maxLength="255"
android:text=""
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorTextHint"
android:textSize="14sp"/>
<CheckBox
android:id="#+id/cbBillable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:button="#null"
android:buttonTint="#color/colorAccent"
android:checked="true"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="Billable"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"/>
<EditText
android:id="#+id/etComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:hint="Enter comment"
android:imeOptions="actionDone"
android:inputType="text"
android:lines="1"
android:maxLength="1000"
android:maxLines="4"
android:minLines="1"
android:text=""
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorTextHint"
android:textSize="14sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/bCancel"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"/>
<Button
android:id="#+id/bOk"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GO"/>
</LinearLayout>
</LinearLayout>
After hearing to your experience, I could think of one more solution. Rather than declaring AlertDialog variable inside each of the methods, declare one global variable so that you can make sure that only one Dialog is shown at any instance.
While declaring, make
AlertDialog dialog = null;
in class
Then in,
showBusinessOrPrivateStartDialog() {
if(dialog == null) {
dialog = build.create();
}
}
builder.setPositiveButton("Business", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
CallClassification.showBusinessCallPopup(context, call);
}
});
builder.setOnDismissListener(new OnDismissListener() {
public void onDismiss() {
dialog = null;
}
}
Then again in showBusinessCallPopup(),
if(dialog == null) {
dialog = build.create();
}
Hope this helps you.
Before calling
CallClassification.showBusinessCallPopup(context, call);
you need to call
dialog.dismiss();
within the same onClick().
Hope this solves your case.
In your second code you are adding view twice, maybe you should remove one of them.
builder.setCustomTitle(view); -> Here
builder.setCancelable(true);
builder.setView(view); --> Here
:)

Attempt to invoke virtual method `MaterialEditText.getText()' on a null object reference

public class MainActivity extends AppCompatActivity {
MaterialEditText edtNewUser, edtNewPassword, edtNewEmail; //pentru Sign up
MaterialEditText edtUser, edtPassword, edtEmail; //pentru Sign in
Button btnSignUp, btnSignIn;
FirebaseDatabase database;
DatabaseReference users;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Firebase
database = FirebaseDatabase.getInstance();
users = database.getReference("Users");
edtUser = (MaterialEditText)findViewById(R.id.edtUser);
edtPassword = (MaterialEditText)findViewById(R.id.edtPassword);
btnSignIn = (Button)findViewById(R.id.btn_sign_in);
btnSignUp = (Button)findViewById(R.id.btn_sign_up);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showSignUpDialog();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
signIn(edtUser.getText().toString(), edtPassword.getText().toString());
}
});
}
private void signIn(final String user, final String pwd) {
users.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child(user).exists()){
if(!user.isEmpty()){
User login = dataSnapshot.child(user).getValue(User.class);
if(login.getPassword().equals(pwd))
Toast.makeText(MainActivity.this, "Login ok!", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "Parola incorecta!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "Va rog introduceti username!", Toast.LENGTH_SHORT).show();
}
}
else
Toast.makeText(MainActivity.this, "Username-ul nu exista!", Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void showSignUpDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Sign up");
alertDialog.setMessage("Introduceti informatiile necesare");
LayoutInflater inflater = this.getLayoutInflater();
View sign_up_layout = inflater.inflate(R.layout.sign_up_layout,null);
edtNewUser = (MaterialEditText)sign_up_layout.findViewById(R.id.edtNewUserName);
edtNewEmail = (MaterialEditText)sign_up_layout.findViewById(R.id.edtNewEmail);
edtNewPassword = (MaterialEditText)sign_up_layout.findViewById(R.id.edtNewPassword);
alertDialog.setView(sign_up_layout);
alertDialog.setIcon(R.drawable.ic_account_circle_black_24dp);
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
final User user = new User(edtNewUser.getText().toString(),
edtNewPassword.getText().toString(),
edtNewEmail.getText().toString());
users.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child(user.getUserName()).exists())
Toast.makeText(MainActivity.this, "User-ul exista deja!",Toast.LENGTH_SHORT).show();
else{
users.child(user.getUserName()).setValue(user);
Toast.makeText(MainActivity.this, "Inregistrat cu success!",Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
dialogInterface.dismiss();
}
});
alertDialog.show();
}
}
XML file
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/info_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardElevation="4dp"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/edtNewUserName"
android:hint="Username"
android:textColorHint="#color/colorPrimary"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:met_baseColor="#color/colorPrimary"
app:met_floatingLabel="highlight"
app:met_primaryColor="#color/colorPrimary"
app:met_singleLineEllipsis="true"
/>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/edtNewPassword"
android:hint="Password"
android:textColorHint="#color/colorPrimary"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:met_baseColor="#color/colorPrimary"
app:met_floatingLabel="highlight"
app:met_primaryColor="#color/colorPrimary"
app:met_singleLineEllipsis="true"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_below="#id/info_login"
android:orientation="horizontal"
android:weightSum="2"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#id/btn_sign_up"
android:text="Sign up"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#id/btn_sign_in"
android:text="Sign in"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
Please help me! I've tried anything I knew but nothing works
The program I work in is ANDROID STUDIO. I need to solve this to finish my app.
signIn(edtUser.getText().toString(), edtPassword.getText().toString());
This is the line that gives me ERROR. If I comment this line the app works properly but I need it for the rest of the app.
Your edtUser MaterialEditText and edtPassword are not present in your xml file, you have to change the ids in xml file to match the Activity ids, or remplace
edtUser = (MaterialEditText)findViewById(R.id.edtUser);
edtPassword = (MaterialEditText)findViewById(R.id.edtPassword);
by :
edtUser = (MaterialEditText)findViewById(R.id.edtNewUserName);
edtPassword = (MaterialEditText)findViewById(R.id.edtNewPassword);

Android - Click Button Inside alertDialog

I wonder how to click a Button inside of AlertDialog in Android and this is my code
activity_float_info.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
MainActivity.java
QR_ = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_float_info, null);
MAIN_QR_SCAN = (LinearLayout) findViewById(R.id.MAIN_QR_SCAN);
MAIN_QR_SCAN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new AlertDialog.Builder(MainActivity.this)
.setCancelable(Boolean.TRUE)
.setNegativeButton(getString(R.string.CANCEL), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setView(R.layout.activity_float_info)
.show();
button = (Button)QR_.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setText("TEst");
}
});
}
});
I have inflate the layout..
I think the main problem is on
button = (Button)QR_.findViewById(R.id.button);
try this create a layout for dialog box
<TextView android:id="#+id/dialogtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/black"
android:text="Please enter the email address you used for the account"
/>
<EditText
android:id="#+id/emailedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:ems="10"
android:padding="5dp"
android:cursorVisible="true"
android:singleLine="true"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:hint="Enter Mail id"
android:textSize="20dp" >
<requestFocus />
</EditText>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:id="#+id/cancelbtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="CANCEL"/>
<Button
android:id="#+id/okbtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Ok"/>
</LinearLayout>
Then create a dialog box using this layout and handle button clicks
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.forgotpassword);
// Set dialog title
dialog.setTitle("ALERT!!");
// set values for custom dialog components - text, image and button
Button okbtn = (Button) dialog.findViewById(R.id.okbtn);
Button cancelbtn = (Button) dialog.findViewById(R.id.cancelbtn);
final EditText emailedittext = (EditText) dialog.findViewById(R.id.emailedittext);
dialog.show();
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
// if decline button is clicked, close the custom dialog
cancelbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
okbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email=emailedittext.getText().toString();
//do something more here
}
});
Refer: https://coderzpassion.com/android-show-alertdialog/
Here is a hack to use AlertDialog:
public class CustomDialog extends AlertDialog {
protected CustomDialog(Context context) {
super(context);
}
}
And then in your Activity:
CustomDialog dialog = new CustomDialog(this);
View view = getLayoutInflater().inflate(R.layout.custom_dialog_layout,null);
dialog.setView(view);
Button button = (Button)view.findViewById(R.id.custom_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(YourActivity.this,"Your message", Toast.LENGTH_LONG).show();
}
});
dialog.show();
And the layout of the dialog (which has nothing in it barring the Button):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="New Button" />
</RelativeLayout>
And you should be able to see a Toast when you click on the button. Hope this helps.
Dialog is like a popup window to show some options to users(options like accept/decline).
Using class android.app.Dialog to create dialog.
Using dialog.xml file to create custom dialog layout.
Example:
res/layout/dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />
<TextView
android:id="#+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/imageDialog"/>
<Button
android:id="#+id/declineButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Submit "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/textDialog"
android:layout_toRightOf="#+id/imageDialog"
/>
</RelativeLayout>
Java Code
// Create custom dialog object
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.image0);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});

dialog.show() crashed InputMethodService?

Sorry for bothering again, but in my Android keyboard I have
public class zyz extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {
private LinearLayout mInputView;
#Override public View onCreateInputView() {
mInputView = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
AddKeys("/layout.txt");
return mInputView;
}
...
final Dialog dialog = new Dialog(this,R.style.myBackgroundStyle);
...
linLayBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
...
dialog.setContentView(R.layout.dialog);
dialog.show();
...
Which is supposed to show a dialog when a button is presses. Yet it crashes the application...
08-30 17:04:41.554: ERROR/AndroidRuntime(15712): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Any ideas how to fix it? Thanks!
dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/frame_left" android:id="#+id/frameLeft"></ImageView>
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#drawable/frame_center" android:id="#+id/LLdialog4letter">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/bun_r" android:id="#+id/ivDialogLetter" android:layout_marginTop="3px" android:layout_marginRight="9px" android:layout_marginLeft="9px"></ImageView>
</LinearLayout>
<ImageView android:layout_height="wrap_content" android:src="#drawable/frame_right" android:layout_width="wrap_content" android:id="#+id/frameRight"></ImageView>
</LinearLayout>
This will work for you Enjoy
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item)
{
// Do something with the selection
}
});
AlertDialog alert = builder.create();
Window window = alert.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
alert.show();

Categories