Alex
Alex

Reputation: 1486

DialogFragment : How to set Placement and Dimentions?

In my application i have a requirement to create a DialogFragment like this (Shown in image - Bottom 1/4 Screen). I have achived full screen DialogFragment through

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int style = DialogFragment.STYLE_NO_TITLE;
        int theme = android.R.style.Theme_Holo;
        setStyle(style, theme);
    }

But I am not sure how to set height of DialogBox and Keep it at the bottom of the screen?

enter image description here

Upvotes: 2

Views: 2238

Answers (4)

Atiq
Atiq

Reputation: 14835

well you could achieve that simply overriding OnCreateView and then setting the params manually as you want.

Here is an example

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {

    Window window = getDialog().getWindow();

   // set gravity
   window.setGravity(Gravity.BOTTOM|Gravity.CENTER);

   // then set the values to where you want to position it
   WindowManager.LayoutParams params = window.getAttributes();
   params.x = 300;
   params.y = 100;
   window.setAttributes(params);
} 

Upvotes: 1

Bhavesh Desai
Bhavesh Desai

Reputation: 239

You can use Bottomsheet for your problem

In Bottomsheet you can also use Fragment and Views

Bottomsheet examples are here

Upvotes: 1

Ratul Ghosh
Ratul Ghosh

Reputation: 1500

You can use onCreateDialog(Bundle savedInstance) method to create the dialog, then set the position. e.g -

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Make the dialog view ready
    //LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
    //View v = layoutInflater.inflate(R.layout.dialog,null);

    // Create the dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Set the view in dialog
    //builder.setView(v);
    Dialog dialog = builder.create();

    // Set the dialog position
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

    wmlp.gravity = Gravity.BOTTOM| Gravity.CENTER;
    wmlp.x = 100;   //x position
    wmlp.y = 100;   //y position


    return dialog;
}

Upvotes: 1

fsebek
fsebek

Reputation: 389

When you create the fragment you are give the option to state where you want the fragment layout to be added to.

add(R.layout....)

Create a empty layout in the activities layout that is the defined size of that box and then simply change the add method to that layout

 add(R.layout.theNewlyCreatedLayoutWiththeRightSizeAndPosition)

Upvotes: 0

Related Questions