6

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)

enter image description here

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();
    }

enter image description here

Are there any other solutions or can I somehow modify the existing ones to make them work?

Community
  • 1
  • 1
src091
  • 2,807
  • 7
  • 44
  • 74

3 Answers3

6

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.

How do i create PopupWindow

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 .

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

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);
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
0

This here looks like your first solution but it's little bit different - maybe it works written like that - link

If this don't work you can create your own Dialog class which uses popuWindow - if you want i can show you some of my code to get you started.

Community
  • 1
  • 1