Android AlertDialogs are not showing buttons in specific acitivity - java

I want to show a basic Alert Dialog with Yes No options, but none of the methods seems to work. The dialog is shown but the YES NO buttons no I am working with API 26.
I am showing Alert Dialog in another activity with no problem (None of them are the main activity), even with custom layouts.
I am using many Alerts dialogs, but there is an example for go back confirmation.
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage(getResources().getString(R.string.goBackAlert))
.setCancelable(false)
.setPositiveButton(_yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ScanRouteActivity.super.onBackPressed();
}
})
.setNegativeButton(_no, null)
.show();
}
This function is working fine in another activity.
I've also tried something like this:
AlertDialog.Builder builder =new AlertDialog.Builder(getApplicationContext());
builder.setMessage(getResources().getString(R.string.goBackAlert));
builder.setCancelable(false);
builder.setPositiveButton(_yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
QuarantineActivity.super.onBackPressed();
}
});
builder.setNegativeButton(_no, null);
AlertDialog dialog = builder.create();
dialog.show();
AND
final AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage(getResources().getString(R.string.goBackAlert))
.setCancelable(false)
.setPositiveButton(_yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
QuarantineActivity.super.onBackPressed();
}
})
.setNegativeButton(_no, null);
final AlertDialog ad = builder.create();
ad.show();
I've already try the followings AlertDialog imports:
import android.app.AlertDialog;
import androidx.appcompat.app.AlertDialog;
This are the XML for the Activities
In this activity is working fine:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorBackground"
tools:context=".ScanRouteActivity">
<TextView
android:id="#+id/txtVIdRoute"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/TitleFont"
android:text=""/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/menuLabels"
android:layout_gravity="center"
android:id="#+id/txtVCurrentContent" />
<FrameLayout
android:id="#+id/frmScanContent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
HERE IS NOT WORKING
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorBackground"
tools:context=".QuarantineActivity">
<TableRow
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Select container type:"
android:textColor="#android:color/black"
/>
<Spinner
android:id="#+id/spinQType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/containerType_list"
/>
<Button
android:layout_marginStart="#dimen/default_margin"
android:layout_gravity="center"
android:gravity="center"
android:drawableStart="#drawable/add_photo"
android:id="#+id/btnAddPhoto"
android:enabled="false"
style="#style/circleBtnDark"/>
</TableRow>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/frmQuarantine"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgVPhoto"
android:maxWidth="150dp"
android:minWidth="150dp"
android:maxHeight="150dp"
android:minHeight="150dp"
android:layout_margin="#dimen/default_margin"
android:longClickable="true"
android:visibility="gone"
app:srcCompat = "#drawable/no_image"/>
</LinearLayout>

Okay,
Please try
<ImageView
...
android:src= "#drawable/no_image"/>
in your XML instead of
<ImageView
...
app:srcCompat = "#drawable/no_image"/>
GOODLUCK Bro

I had to implement a whole class and XML layout to manage AlertDialgs and after that I have not have issues with that, the bright side is that I had the opportunity to customize my AlertDialogs.

Related

How do I make a Dialog Fragment's background invisible?

I used youtuber Coding in Flow's method to create a custom dialog. I've been trying all day to make the dialog's background transparent. I've used every single method I've found online. Non worked.
Here's how it goes:
First here's the Dialog layout layout_dialog.xml :
<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="match_parent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#drawable/dialog_background">
<!-- The contents of the Dialog go here -->
</RelativeLayout>
<ImageView
android:id="#+id/iconImageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:background="?attr/actionBarItemBackground"
android:scaleType="fitCenter"
app:srcCompat="#android:mipmap/sym_def_app_icon" />
</RelativeLayout>
Here's the Dialog class:
public class DialogBrightness extends AppCompatDialogFragment {
//declare whatever variables here
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog, null);
builder.setView(view)
.setTitle("Login")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//get whatever values
listener.apply(//values);
}
});
//findViewById for your dialog contents here
return builder.create();
}
public interface DialogBrightnessListener {
void apply(//values);
}
}
And here's the Dialog being called from the Main activity:
DialogBrightness dialogBrightness = new DialogBrightness();
dialogBrightness.show(getSupportFragmentManager(), "Brightness Dialog");
This is how the Dialog appears:
I'm trying to make the top white part invisible. Nothing works!
Try this:
put the code below in the onCreateDialog:
// set the dialog background to transparent
getDialog().getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));
// remove background dim
getDialog().getWindow().setDimAmount(0);
You can design the layout like following. There is an extra layout, but in case of dialogs, it will help
Try this:
set the background color of the parent layout to:
android:background="#android:color/transparent"
like this:
<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="match_parent"
android:background="#android:color/transparent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#drawable/dialog_background">
<!-- The contents of the Dialog go here -->
</RelativeLayout>
<ImageView
android:id="#+id/iconImageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:background="?attr/actionBarItemBackground"
android:scaleType="fitCenter"
app:srcCompat="#android:mipmap/sym_def_app_icon" />
</RelativeLayout>

Android: Custom Dialog unwanted extra space

I'm having an issue with my custom dialog, it shows empty space at the right side when I set the width and hight to wrap_content. I don't want to use match_parent because the picture will be bigger and the layout will not be right when I do landscape.
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:id="#+id/share_layout">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:id="#+id/rl4">
<ImageView
android:id="#+id/media_share_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/step_one" />
<Button
android:background="#drawable/red_button"
android:id="#+id/media_share_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dialog_button_share"
style="#style/button_text"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_alignLeft="#+id/media_share_img"
android:layout_below="#+id/media_share_img"
android:layout_alignRight="#+id/media_share_img" />
</RelativeLayout>
</RelativeLayout>
java:
View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialog_share, null);
dialog = new AlertDialog.Builder(getActivity()).setView(dialogView)
/* .setNeutralButton(getResources().getString(R.string.dialog_button_close), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
})*/.create();
/*
.setIcon(R.mipmap.ic_launcher).*/
dialog.setCanceledOnTouchOutside(true);
dialog.show();

Fragment replacing returns NullPointerException

There are many topics on this subject, but I couldn't find any solution that helped my case.
IDEA BEHIND:
What I have is a ViewPager, for swipping between 3 fragments. That works. But in one of the mentioned fragments, I do some functionalities - which then I check in another one of those before mentioned Fragments. Everything works.
Then, when I do the check, I open an AlertDialog, telling the user that his configuration is succesfull. By button click, I want that app transfers the user to one different Fragment (not one of the ViewPager fragments).
CODE SNIPPET - FragmentTwo:
public void displayAlertSuccess(Context ctx) {
new AlertDialog.Builder(ctx)
.setTitle("ACCESS GRANTED!")
.setMessage("Congratulations!")
.setPositiveButton("Great!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with
replaceFragment();
}
})
.setNegativeButton("Format my Card", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mainActivity.setNFC_ACTION("FORMAT");
}
})
.setIcon(android.R.drawable.ic_dialog_info)
.show();
}
private void replaceFragment() {
Fragment frag = new CardDesfire1();
getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
}
CODE SNIPPET - XML Layouts:
I. activity_holder.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
II. activity_access.xml
<!-- activity_screen_slide.xml -->
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
III. fragment_access_slide2.xml (fragment that has the Dialog)
<!-- fragment_screen_slide_page.xml -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0080ff">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/imageButton"
android:src="#drawable/img_access_slide2_2"/>
</ScrollView>
IV. activity_desfire1.xml (Fragment that I want to launch onClick)
<?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"
android:background="#drawable/bg_des2">
<Button
android:layout_width="fill_parent"
android:layout_height="25dp"
android:text="You have discovered..."
android:id="#+id/btn_des1_discovered"
android:background="#3A5FCD"
android:typeface="monospace"
android:textColor="#ffffffff"
android:enabled="false"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn_loy"
android:text="Loyalty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttons_shape"
android:shadowColor="#000000"
android:textColor="#FFFFFF"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_above="#+id/btn_pay"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAllCaps="false"
android:visibility="invisible"/>
<Button
android:id="#+id/btn_pay"
android:text="µPay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttons_shape"
android:shadowColor="#000000"
android:textColor="#FFFFFF"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:textAllCaps="false"
android:layout_alignParentBottom="true"
android:layout_marginBottom="70dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"/>
<Button
android:id="#+id/btn_acc"
android:text="Access"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttons_shape"
android:shadowColor="#000000"
android:textColor="#FFFFFF"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:textAllCaps="false"
android:layout_above="#+id/btn_pay"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="invisible"/>
<ImageView
android:layout_width="wrap_content"
android:visibility="invisible"
android:layout_height="wrap_content"
android:id="#+id/img_card_des1"
android:layout_below="#+id/btn_des1_discovered"
android:src="#drawable/img_card_des1"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:visibility="invisible"
android:textSize="18dp"
android:gravity="center"
android:text="#string/des1_desc"
android:id="#+id/tv_des1_desc"
android:textColor="#ffffffff"
android:textStyle="italic"
android:background="#ff8db6cd"
android:layout_below="#+id/img_card_des1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />
</RelativeLayout>
LINE OF ERROR:
getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
Error Description from LogCat:
java.lang.NullPointerException
Error occurs when:
My error occurs when the user clicks the button "Great" in AlertDialog, which should then invoke the Fragment replacement.
I have tried:
I have tried to fix it with the getChildFragment , but then I get different kind of error: IllegalStateException : Activity has been destroyed.
Any help is kindly appreciated.
Have you declared default constructor in your CardDesfire1 fragment. If not then declare it
public CardDesfire1(){}
Make your parent Activity extend FragmentActivity
class ParentActivity extends FragmentActivity
The problem occurs because you call getFragmentManager() in a Fragment, you should delegate displaying the dialog to the activity.
Make activity implement an interface, and have the fragment call this interface method when it's time to show the dialog.
Example: You define a new inner interface in FragmentTwo(this code goes in FragmentTwo):
public interface OnReplaceFragmentListener {
void onReplaceFragment();
}
You link your activity to the fragment(this code goes in FragmentTwo):
private OnReplaceFragmentListener mCallback;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnReplaceFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnReplaceFragmentListener");
}
}
private void replaceFragment() {
mCallback.onReplaceFragment();
}
Then make activity implement OnReplaceFragmentListener.
And(this code goes into FragmentTwo's hosting activity):
#Override
void onReplaceFragment() {
Fragment frag = new CardDesfire1();
getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
}

How to add a 'Click for more details' expandable field to AlertDialog in android

I have a AlertDialog that is shown when there is an error and it prints 'Error, could not be added/whatever", I also have the result of the exception that I'd like to parse but not shown to all users, only to those that want to click on 'details' and read the exception.
AlertDialog.Builder dialogo1 = new AlertDialog.Builder(activity);
dialogo1.setTitle("Error");
dialogo1.setMessage("Could not update movie: " + result);
dialogo1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogo1, int id) {
activity.finish();
}
});
dialogo1.show();
There is my code, pretty simple, right? I haven't been able to find this answer anyway and I'm starting to think it is just not possible
what you want is a custom alert dialog.
so for that you will have to us a layout and define your dialog.Then initialize your listeners etc like it was a normal view that you are defining
dialog_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/error"
andorid:text="Error Message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/moreDetailsButton"
android:text="Click for more details"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/moreDetailsView"
android:text="Click for more details"
android:inputType="textPassword"
android:layout_width="match_parent"
android:visibility="gone";
android:layout_height="wrap_content"/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/moreDetailsButton"
/>
</RelativeLayout>
and in your class
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
TextView error=(TextView)dialog.findViewById(R.id.error);
Button errorButton=(Button)dialog.findViewById(R.id.moreDetailsButton);
Button okButton=(Button)dialog.findViewById(R.id.ok);
//Do whatever you want with the rest of the dialog
// initialize all the onclick listeners a usual
errorButton.setOnClickListener(new View.onClickListener(){
#Override
public void onClick(View v){
TextView errorDetails=(TextView)dialog.findViewById(R.id.moreDetailsView);
errorDetails.setText(detailedErrorMessage) //add the details to this text view;
errorDetails.setVisibility(View.VISIBLE);
}
});
dialog.show();
Sorry if there are any syntactically errors if the ctrl+c,ctrl+v of this doesnt work.My IDE decided to misbehave today.
Follow this for details on a custom alert dialog.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/error"
android:text="Error Message"
android:layout_width="match_parent"
android:layout_height="37dp"
android:layout_toEndOf="#+id/moreDetailsView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/moreDetailsView"
android:text="No existen detalles para este tipo de error."
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="wrap_content" />
<Button
android:id="#+id/moreDetailsButton"
android:text="Click for more details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/moreDetailsView"
android:layout_below="#+id/error"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="139dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/moreDetailsView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>

How can I access an ImageView inside of AlertDialog.Builder?

I have some code that creates a popup and assign it an XML layout that has two ImageView items in it.
popupWindow.setTitle("New note");
popupWindow.setMessage("choose the type of note to create");
View noteChoices = inflater.inflate(R.layout.addnotepopup, null);
popupWindow.setView(noteChoices);
popupWindow.setPositiveButton(null, null);
popupWindow.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//
}
});
popupWindow.show();
addnotepopup.xml has the two ImageView items
How can I add click listeners to those two items? When I try doing it the same way as any other button, findViewById() returns a null value
XML code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2" >
<ImageView
android:id="#+id/recordnoteoption"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:src="#drawable/medium_mic"
android:layout_weight="1" />
<ImageView
android:id="#+id/typenoteoption"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:src="#drawable/medium_keyboard"
android:layout_weight="1" />
</LinearLayout>
Try this:
popupWindow.findViewById(R.id.yourId);
or if you want:
noteChoices.findViewById(R.id.yourId);

Categories

Resources