I added an Action button to show about Alert Dialog for my app that I'm developing.... I just fetched one xml file for that Action Dialog popup.
I used this xml as Alert Dialog popup. using below java code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="300dp"
android:weightSum="1">
<TextView
android:id="#+id/info"
android:layout_width="277dp"
android:layout_height="wrap_content"
android:text="Click the links below to contact us"
android:layout_marginTop="13dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/wa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/wa"
android:textSize="30dp"
android:layout_marginTop="8dp"
android:layout_below="#+id/info"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fb"
android:textSize="30dp"
android:layout_marginTop="8dp"
android:layout_below="#+id/wa"
android:layout_centerHorizontal="true" />
</RelativeLayout>
java code :
public void info(MenuItem item) {
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.alertdiag, null);
TextView waf = (TextView) findViewById(R.id.wa);
TextView fbf = (TextView) findViewById(R.id.fb);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("About");
builder.setView(v);
builder.setCancelable(false);
builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
Now I can see those TextViews in my main activity as alert Dialog. But I can't click the links. Any solution????
Thank you in advance.....
Use AlertDialog.setView() to display a custom layout containing your hyperlink TextView in the message area.
E.g. Make a hyperlink_layout.xml containing a TextView with id = hyperlink_text
Related
I have made an alert dialog which shows a custom view,
Java Code
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.about_me_alert, null);
CircleImageView imageView = view.findViewById(R.id.image);
Glide.with(view).load("https://example.com/example.jpg").into(imageView);
TextView title = view.findViewById(R.id.title);
title.setText("This is a title");
TextView note = view.findViewById(R.id.note);
note.setText("This is a note");
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(view);
builder.setPositiveButton("Okay", (dialog, which) -> dialog.dismiss()).show();
XML Layout- about_me_alert.xml
<?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="?attr/cardbackground"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/image"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_centerHorizontal="true"
android:layout_margin="8dp" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:textColor="#color/white"
android:textSize="16sp" />
<TextView
android:id="#+id/note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_margin="8dp"
android:textColor="#color/white"
android:textSize="16sp" />
</RelativeLayout>
When alert is shown, it reserves space for the imageview, and two textviews but does not set text or image.
NOTE: This only happens when I make a production apk for release, not while I am testing on my phone.
I'm trying to create a dialog popup window with a custom layout, but my custom layout height and popup window height is not matching. Also, the popup window is hiding a button. What should I do, how can I match my popup window height with a custom layout?
Please help me.
I want my popup window to look like this
But it is looking like this
My custom layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="336dp"
android:layout_height="156dp"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|center"
android:background="#drawable/rectangle"
android:layout_marginBottom="88dp">
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="58dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/bikepic" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="64dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delivery" />
<TextView
android:id="#+id/biketextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="69dp"
android:layout_marginTop="17dp"
android:background="#drawable/biketextview"
android:gravity="center"
android:text="Bike"
android:textColor="#FFFFFF"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/deliverytextView"
android:layout_width="64dp"
android:layout_height="18dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="64dp"
android:background="#drawable/deliverytextview"
android:gravity="center"
android:text="Delivery"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView7"
android:layout_width="23dp"
android:layout_height="14dp"
android:layout_marginStart="78dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="74"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView5" />
<TextView
android:id="#+id/textView8"
android:layout_width="23dp"
android:layout_height="14dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="80dp"
android:gravity="center"
android:text="39"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView6" />
popup dialog code
popAddPost = new Dialog(this);
popAddPost.setContentView(R.layout.pop_up_confirm_pickup);
//popAddPost.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//popAddPost.getWindow().setLayout(Toolbar.LayoutParams.MATCH_PARENT,Toolbar.LayoutParams.WRAP_CONTENT);
popAddPost.getWindow().getAttributes().gravity = Gravity.BOTTOM;
In your xml, the parent layout is using RelativeLayout LayoutParams on a ConstraintLayout
Remove android:layout_alignParentLeft="true" and android:layout_alignParentEnd="true"
ConstraintLayout also doesn't support layout_gravity so you can also remove that.
<androidx.constraintlayout.widget.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="#drawable/rectangle"
android:layout_marginBottom="88dp">
Code
popAddPost = new Dialog(this);
popAddPost.setContentView(R.layout.pop_up_confirm_pickup);
Objects.requireNonNull(popAddPost.getWindow()).setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popAddPost.show();
EDIT
To position the dialog at the bottom use this:
Window window = popAddPost.getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM;
window.setAttributes(params);
This is how i do it:
private AlertDialog alertDialog;
alertDialog = buildDialog();
alertDialog.show();
private AlertDialog buildDialog(){
final AlertDialog dialogBuilder = new AlertDialog.Builder(context).create();
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.dialog, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setButton(DialogInterface.BUTTON_NEGATIVE, "CLOSE", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialogBuilder.dismiss();
}
});
return dialogBuilder;
}
I think dialog is not a good solution for your problem as a dialog would have its own window with overlay.
As I can see you want the dialog to be at the bottom of the screen, which makes the BottomSheet as a perfect match for your problem.
You can use the following to get started with:
https://www.androidhive.info/2017/12/android-working-with-bottom-sheet/
https://material.io/develop/android/components/bottom-sheet-behavior/
https://medium.com/#droidbyme/android-bottom-sheet-7e9cfcec6427
With BottomSheet, you would get a separate window just like dialog without the overlay you are facing.
I've encountered a lot of pop up images like the example below, i've made a pop up image but there is no close button at the top left corner, do u guys have any idea how to make a pop up image with close button at the top left cornel like the example below in Android?
You need to make your own Dialog with your custom layout
Dialog dialog;
private void showDialog() {
// custom dialog
dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
// set the custom dialog components - text, image and button
ImageButton close = (ImageButton) dialog.findViewById(R.id.btnClose);
Button buy = (Button) dialog.findViewById(R.id.btnBuy);
// Close Button
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
//TODO Close button action
}
});
// Buy Button
buy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
//TODO Buy button action
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
custom_dialog.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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#android:color/white"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/images" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<Button
android:id="#+id/btnBuy"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/holo_green_light"
android:text="BUY"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/btnBuy"
android:text="Thank You (Domestic Album Version)"
android:textColor="#android:color/black"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle"
android:text="Still Not Gettin' Any, 2004" />
</RelativeLayout>
</LinearLayout>
<ImageButton
android:id="#+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/black"
android:src="#android:drawable/ic_menu_close_clear_cancel" />
</RelativeLayout>
Result is:
You can use this library. There is close button on top right corner. only need to set image only.
https://github.com/chathuralakmal/AndroidImagePopup
I want use Spinner in Custom Dialog Box. i use activity show as Dialog Box, inside activity use Spinner. but when convert Activity to Dialog Box Spinner's items not shown !!
My XML Code :
<RelativeLayout 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="#drawable/add_sms_dialog_background"
tools:context="com.tellfa.smsbox.addSMS_Page">
<RelativeLayout
android:id="#+id/addSMS_cover_layout"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#drawable/add_sms_dialog_coverlayout">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/addSMS_profile_image"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:src="#drawable/all_users_avatar"
app:border_color="#color/activity_bg_color"
app:border_width="2dp" />
<com.tellfa.smsbox.components.tellfa_TextView
android:id="#+id/addSMS_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="#+id/addSMS_profile_image"
android:text="title"
android:textColor="#color/activity_bg_color"
android:textSize="23sp" />
<ImageView
android:id="#+id/addSMS_close_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:alpha="0.7"
android:src="#drawable/close_dialog" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/addSMS_smsInfo_layout"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="#+id/addSMS_cover_layout"
android:layout_marginBottom="200dp">
<com.rey.material.widget.Spinner
style="#style/Material.Widget.Spinner.Light"
android:id="#+id/spinner_no_arrow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="128dp"
android:layout_marginTop="16dp"
app:rd_style="#style/Material.Drawable.Ripple.Wave.Light"
app:rd_enable="true"
app:spn_labelEnable="true"
app:spn_label="Spinner without arrow"
app:spn_arrowSize="0dp"
app:spn_popupItemAnimation="#anim/abc_fade_in"/>
</RelativeLayout>
</RelativeLayout>
my Java code :
final Dialog dialog = new Dialog(Main_Page.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.add_sms);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Window window = dialog.getWindow();
window.setLayout(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
dialog.setCancelable(false);
ImageView closeDialog_image = (ImageView) dialog.findViewById(R.id.addSMS_close_image);
closeDialog_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
});
i use this library for Spinner : Click to see
how to fix it?
Provide an adapter for spinner , without it no item will be shown provide this code in your dialog
String[] items ={"item 1","item 2","item 3"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spin=(Spinner)dialog.findViewById(R.id.spinner_no_arrow);
spin.setAdapter(adapter);
I have an Activity which launches an AlertDialog with the following code. Unfortunately, the id that comes from the RadioGroup as checked is always the ID of the first radio button, no matter which radio button was actually checked.
I suspect the problem is that the view has already been disposed of when the OK button is pressed, but I don't know why the string would still work if that were the case.
Why would this happen, and what could I do to get the actual radio button checked?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View v = getLayoutInflater().inflate(R.layout.dialog_add_team, null);
builder.setView(v);
// Add the buttons
builder.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String name = ((EditText) v.findViewById(R.id.team_name)).getText()+"";
if (name.equals(""))
return;
int checkedId=((RadioGroup)v.findViewById(R.id.radioGroup1))
.getCheckedRadioButtonId();
teamAdded(name, checkedId);
}
});
builder.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
Here is the XML for my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/team_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/teamname" >
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/bothgenders" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/boys" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/girls" />
</RadioGroup>
</LinearLayout>
I think you should set setOnCheckedChangeListener to your radioGroup. You can use this answer. Hope this helps.