I have a custom View
which I use for my AlertDialog
's title and content, here's the view:
view_tip.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" style="@style/StandardLinearLayout"
android:layout_height="match_parent" android:layout_width="match_parent">
<TextView
android:maxWidth="245sp"
android:id="@+id/actionTip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
And I want AlertDialog
to wrap it's contents. I've been following solutions from this thread: AlertDialog with custom view: Resize to wrap the view's content but none of them works for me.
Solution 1, no effect at all, AlertDialog
takes the whole space:
// Scala code
val title = getLayoutInflater.inflate(R.layout.view_tip, null)
title.findViewById(R.id.actionTip).asInstanceOf[TextView] setText "title"
val view = getLayoutInflater.inflate(R.layout.view_tip, null)
view.findViewById(R.id.actionTip).asInstanceOf[TextView] setText "content"
val dialog = new android.app.AlertDialog.Builder(this).setCustomTitle(title).setView(view).show
dialog.getWindow.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
Solution 2 which uses forceWrapContent
to modify a view hierarchy, has an effect on content but the title is unaffected:
// Scala code
val title = getLayoutInflater.inflate(R.layout.view_tip, null)
title.findViewById(R.id.actionTip).asInstanceOf[TextView] setText "title"
val view = getLayoutInflater.inflate(R.layout.view_tip, null)
view.findViewById(R.id.actionTip).asInstanceOf[TextView] setText "content"
val dialog = new android.app.AlertDialog.Builder(this).setCustomTitle(title).setView(view).show
forceWrapContent(title)
forceWrapContent(view)
...
// Java code
static void forceWrapContent(View v) {
// Start with the provided view
View current = v;
// Travel up the tree until fail, modifying the LayoutParams
do {
// Get the parent
ViewParent parent = current.getParent();
// Check if the parent exists
if (parent != null) {
// Get the view
try {
current = (View) parent;
} catch (ClassCastException e) {
// This will happen when at the top view, it cannot be cast to a View
break;
}
// Modify the layout
current.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
}
} while (current.getParent() != null);
// Request a layout to be re-done
current.requestLayout();
}
Are there any other solutions or can I somehow modify the existing ones to make them work?
AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout. DatePickerDialog or TimePickerDialog. A dialog with a pre-defined UI that allows the user to select a date or time.
WRAP_CONTENT means that the view wants to be just large enough to fit its own internal content, taking its own padding into account.
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
You can use custom Dialog instead AlertDialog.
Here is example of Custom Dialog
custom_dialog = new Dialog(this,android.R.style.Theme_Holo_Light_Dialog_MinWidth);
custom_dialog.getWindow().setBackgroundDrawable(new ColorDrawable((0xff000000)));
custom_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
custom_dialog.setCancelable(false);
custom_dialog.setContentView(R.layout.your_custom_layout);
custom_dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, Color.parseColor("#FFFFFF"));
custom_dialog.show();
If above logic not works you can use Android Popup Window. It can be used to display an arbitrary view, and it can be very convenient in cases when you want to display additional information.
Create custom popup xml for contain
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Pop-Up"
/>
</LinearLayout>
Java code:
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.popup_example, null, false),
100,
100,
true);
// The code below assumes that the root container has an id called 'main'
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
Code Courtesy.
For Demo purpose you may check Git AlertDialogPro And CustomAlertDialog .I hope it will helps you .
use window manager
private WindowManager wm;
wm = (WindowManager) arg0.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
params1 = new WindowManager.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_APPLICATION |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
params1.height = (height/3);
params1.width = width;
params1.x = 0;
params1.y = 0;
params1.format = PixelFormat.TRANSLUCENT;
ly1 = new LinearLayout(arg0.getApplicationContext());
ly1.setOrientation(LinearLayout.HORIZONTAL);
ly1.addView(hiddenInfo);
wm.addView(ly1, params1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With