Gödel77
Gödel77

Reputation: 839

How to wrap Dialog Window to Content?

I'm developing a Dialog, which should shrink to its content, so, I want to get a behaviour like wrap_content in a common view but for a normal Dialog. That's what I want to show in a Dialog Window

Layout

In a Dialog Window should look like this Desired behaviour

but this is what I actually get got behaviour

Could you say me what I do wrong?

I thank you in Advance.

That's my code

...
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.advanced_options);
dialog.setTitle(titleId);

// Stuff referred to builder
AlertDialog.Builder builder ...
...

int type = WindowManager.LayoutParams.TYPE_INPUT_METHOD;
WindowManager.LayoutParams w_layout_params = new WindowManager.LayoutParams(type);

dialog.getWindow().setAttributes(w_layout_params );
...
builder.create();
dialog.show();

advanced_options.xml Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relLayout_advancedOptions"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/border_advanced_options"
    android:divider="?android:listSeparatorTextViewStyle"
    android:showDividers="" >    

    <TextView
        android:id="@+id/speed_limit"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="@string/speed_limit"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/speed_limit_alert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/speed_limit"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:text="@string/speed_limit_alert"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />


    <TextView
        android:id="@+id/percent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/speed_limit_alert"
        android:layout_toRightOf="@+id/speed_limit_alert_edit_text"
        android:text="@string/percentage_symbol"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/minimum_speed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/speed_limit_alert"
        android:layout_below="@+id/speed_limit_alert_edit_text"
        android:layout_marginTop="15dp"
        android:text="@string/minimum_speed"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/minimum_speed_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/minimum_speed"
        android:layout_alignBottom="@+id/minimum_speed"
        android:layout_alignLeft="@+id/speed_limit_alert_edit_text"
        android:ems="3"
        android:inputType="number"
        android:textColor="@color/black"
        android:text="6" />

    <TextView
        android:id="@+id/minimum_speed_unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/minimum_speed"
        android:layout_alignLeft="@+id/percent"
        android:text="@string/metric_speed_unit"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/maximum_speed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/minimum_speed"
        android:layout_below="@+id/minimum_speed_edit_text"
        android:layout_marginTop="15dp"
        android:text="@string/maximum_speed"
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textColor="@color/black"/>

    <EditText
        android:id="@+id/maximum_speed_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/maximum_speed"
        android:layout_alignBottom="@+id/maximum_speed"
        android:layout_alignLeft="@+id/minimum_speed_edit_text"
        android:ems="3"
        android:inputType="number"
        android:textColor="@color/black"
        android:text="18" />

    <TextView
        android:id="@+id/maximum_speed_unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/maximum_speed"
        android:layout_toRightOf="@+id/maximum_speed_edit_text"
        android:text="@string/metric_speed_unit"
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/mobile_device_performance"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/maximum_speed_edit_text"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical"
        android:text="@string/mobile_device_performance"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/maximum_number_objects"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/maximum_speed"
        android:layout_below="@+id/mobile_device_performance"
        android:layout_marginTop="20dp"
        android:text="@string/maximum_objects_in_view"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/maximum_number_objects_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/maximum_number_objects"
        android:layout_alignBottom="@+id/maximum_number_objects"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/maximum_number_objects"
        android:ems="3"
        android:inputType="number"
        android:text="450"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/speed_limit_alert_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/percent"
        android:layout_alignBottom="@+id/percent"
        android:layout_alignLeft="@+id/maximum_number_objects_edit_text"
        android:ems="3"
        android:inputType="number"
        android:text="42"
        android:textColor="@color/black" >

        <requestFocus />
    </EditText>

    <View
        android:id="@+id/void_view"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/maximum_speed_unit"
        android:layout_below="@+id/maximum_number_objects" />

Upvotes: 0

Views: 1882

Answers (2)

G&#246;del77
G&#246;del77

Reputation: 839

I could adjust the Dialog (NOT AlertDialog!) to my custom Layout but I couldn't* set either the Title or the Buttons so that I got an Exception cause of setting content before requestFeature, then I decided to custom my Dialog and including the Buttons and title in my Custom Layout.

*I have researched and it's possible to overcome that if we define the Dialog in OnCreate other even in OnCreateDialog but I have an extra Class just for static Dialog Methods and was no solution for me.

In Addition I post what it was useful for me, perhaps someone is also useful.

Now my custom dialog looks like this

enter image description here

Here the code for whom wants to test it.

/**
 * @param c
 *            the Context
 * @return the about dialog
 */
public static void getAdvancedOptions(final Activity activity) {

    Log.i("TAG", "Dialogs::getAdvancedOptions:: 0");
    //settings            = session.getSettings();

    // Creation of a Dialog with Frame and title Characteristics
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.advanced_options_expanded);


    // Here we control the validity of "edit_text" Fields
    speed_limit_alert   = (EditText)dialog.findViewById(R.id.speed_limit_alert_edit_text);                                          
    minimum_speed       = (EditText)dialog.findViewById(R.id.minimum_speed_edit_text);                
    maximum_speed       = (EditText)dialog.findViewById(R.id.maximum_speed_edit_text);               
    max_objects_in_view = (EditText)dialog.findViewById(R.id.maximum_number_objects_edit_text);

    //showAdvancedOptions("getAdvancedOptions 1::");

    // First we set the "SharedPreferences"-saved values on EditText Fields                
    performEditText(speed_limit_alert,Constants.MIN_PERCENTAGE, Constants.MAX_PERCENTAGE,"speed_limit_alert"); 
    performEditText(minimum_speed, "","", "minimum_speed"); 
    performEditText(maximum_speed, "","","maximum_speed"); 
    performEditText(max_objects_in_view, "","","max_objects_in_view");  

    //showAdvancedOptions("getAdvancedOptions 4::");

    ok_button     = (Button)dialog.findViewById(R.id.ok_button);                                        
    cancel_button = (Button)dialog.findViewById(R.id.cancel_button);  


    // Definition of "OK" Button for the Dialog     
    ok_button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            Log.i("TAG", "Dialogs::getAdvancedOptions::onClick");
            if ( dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }

        }
    });     

    cancel_button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            Log.i("TAG", "Dialogs::getAdvancedOptions::onClick");
            if ( dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }

        }
    }); 

    dialog.show();

}

private static void performEditText(EditText edit_text, final String min_value, final String max_value, final String id) {

    String edit_text_value = edit_text.getText().toString();
    Log.i("TAG", "Dialogs::performEditText:: id: "+ id +" edit_text_value: "+edit_text_value+ " (min_value,max_value)=("+min_value+","+max_value+")");

    TextWatcher textWatcher = new TextWatcher() {
        public void afterTextChanged(Editable s) {
            int i = 0;
            Log.i("TAG","Dialogs::performEditText::afterTextChanged:id: "+ id +" - 0 : s: "+s.toString());

            int length = s.length();

            if ( length == 0 )
                i = 0;
            else if ( length < 3 ) {
                    if ( s.charAt(0) == '0')
                        s.delete(1, length );

                    i = Integer.parseInt(s.toString());
                    Log.i("TAG","Dialogs::performEditText::afterTextChanged:id: "+ id +" - 1a : s: "+s+", i:"+i);
            } else {
                //String hundred = "100";

                String s_value = s.toString();
                Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 1b : s: "+s+", i:"+i);

                if ( ( min_value != null ) && ( min_value.length() != 0 ) && ( max_value != null) && ( max_value.length() != 0 )) {


                    if ( !s_value.equalsIgnoreCase(max_value))
                        s.delete(2, length );

                }

                i = Integer.parseInt(s.toString());
                Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 2b : i: "+i);                   
            }
            if (i >= 0 && i <= 100) {
                Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 3 : (i >= 0 && i <= 100): i: "+i);
                //speed_limit_alert.setText(s); // This ensures 0-100 value for speed_limit_alert
            }               
            Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 4 : i: "+i);

        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.i("TAG","Dialogs::performEditText::beforeTextChanged: id: "+ id +" s: "+s); 
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.i("TAG","Dialogs::performEditText::onTextChanged: id: "+ id +" s: "+s);
        }
    };


    edit_text.addTextChangedListener(textWatcher);  


}



private static void showAdvancedOptions(String entryPoint) {
    String s_l_a = speed_limit_alert.getText().toString();  
    Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: s_l_a: "+s_l_a);
    String mi_s = minimum_speed.getText().toString();   
    Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: mi_s: "+mi_s);
    String ma_s = maximum_speed.getText().toString();   
    Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: ma_s: "+ma_s);
    String m_o_i_v = max_objects_in_view.getText().toString();  
    Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: m_o_i_v: "+m_o_i_v);       

}

And the layout

<?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:orientation="vertical"
    android:background="@drawable/border_advanced_options"
    android:divider="?android:listSeparatorTextViewStyle"
    android:showDividers="middle" >  

<RelativeLayout
    android:id="@+id/relLayout_advancedOptions"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:divider="?android:listSeparatorTextViewStyle"
    android:orientation="vertical"
    android:showDividers="" >

    <TextView
        android:id="@+id/speed_limit"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/void_view_0"
        android:layout_marginTop="10dp"
        android:text="@string/speed_limit"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/speed_limit_alert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/speed_limit"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/speed_limit_alert"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/percent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/speed_limit_alert"
        android:layout_toRightOf="@+id/speed_limit_alert_edit_text"
        android:text="@string/percentage_symbol"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/minimum_speed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/speed_limit_alert"
        android:layout_below="@+id/speed_limit_alert_edit_text"
        android:layout_marginTop="15dp"
        android:text="@string/minimum_speed"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/minimum_speed_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/minimum_speed"
        android:layout_alignBottom="@+id/minimum_speed"
        android:layout_alignLeft="@+id/speed_limit_alert_edit_text"
        android:ems="3"
        android:inputType="number"
        android:text="6"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/minimum_speed_unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/minimum_speed"
        android:layout_alignLeft="@+id/percent"
        android:paddingRight="5dp"
        android:text="@string/metric_speed_unit"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/maximum_speed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/minimum_speed"
        android:layout_below="@+id/minimum_speed_edit_text"
        android:layout_marginTop="15dp"
        android:text="@string/maximum_speed"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/maximum_speed_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/maximum_speed"
        android:layout_alignBottom="@+id/maximum_speed"
        android:layout_alignLeft="@+id/minimum_speed_edit_text"
        android:ems="3"
        android:inputType="number"
        android:text="18"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/maximum_speed_unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/maximum_speed"
        android:layout_alignRight="@+id/minimum_speed_unit"
        android:layout_toRightOf="@+id/maximum_speed_edit_text"
        android:paddingRight="5dp"
        android:text="@string/metric_speed_unit"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/mobile_device_performance"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/maximum_speed_edit_text"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical"
        android:text="@string/mobile_device_performance"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/maximum_number_objects"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/maximum_speed"
        android:layout_below="@+id/mobile_device_performance"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/maximum_objects_in_view"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/maximum_number_objects_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/maximum_number_objects"
        android:layout_alignBottom="@+id/maximum_number_objects"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/maximum_number_objects"
        android:ems="3"
        android:inputType="number"
        android:text="450"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/speed_limit_alert_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/percent"
        android:layout_alignBottom="@+id/percent"
        android:layout_alignLeft="@+id/maximum_number_objects_edit_text"
        android:digits="0123456789"
        android:ems="3"
        android:inputType="number"
        android:text="42"
        android:textColor="@color/black" >

        <requestFocus />
    </EditText>

    <View
        android:id="@+id/void_view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/lin_layout_buttons"
        android:layout_below="@+id/maximum_number_objects"
        android:background="@color/black" />

    <TextView
        android:id="@+id/title_advanced_options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/minimum_speed_unit"
        android:drawableLeft="@drawable/ic_launcher_48"
        android:drawableRight="@drawable/ic_action_settings_48"
        android:gravity="center|center_vertical"
        android:paddingLeft="5dp"
        android:text="Advanced Options"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/blue" />

    <View
        android:id="@+id/void_view_0"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/title_advanced_options"
        android:layout_below="@+id/title_advanced_options"
        android:background="@color/black" />
    <LinearLayout
        android:id="@+id/lin_layout_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/minimum_speed_unit"
        android:layout_alignTop="@+id/void_view"
        android:weightSum="3" >

        <Button
            android:id="@+id/ok_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/maximum_number_objects"
            android:layout_alignRight="@+id/speed_limit_alert"
            android:layout_below="@+id/maximum_number_objects_edit_text"
            android:layout_weight="1.5"
            android:text="OK" />

        <Button
            android:id="@+id/cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_toRightOf="@+id/button1"
            android:layout_weight="1.5"
            android:text="Cancel" />
    </LinearLayout>     
</RelativeLayout>

Upvotes: 0

Dheeraj Bhaskar
Dheeraj Bhaskar

Reputation: 19039

Change RelativeView's width to match_parent

What you're looking for is match_parent's behavior.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relLayout_advancedOptions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/border_advanced_options"
android:divider="?android:listSeparatorTextViewStyle"
android:showDividers="" > 

Note: wrap_content in android specifies the view to use as little space as needed and that is the behavior you're getting.

Upvotes: 1

Related Questions