Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make dialogFragment width match parent?

I have this dialog fragmentt, and I have two problems.

1.How do i make the width match parent (cleanest and best solution please).

  1. In the dialog fragment i have an editText. How do i make it pop up a soft keyboard when the dialog fragment opens?

Hope you guys can help!

Here is my dialog fragment java code:

 @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        rootView  = inflater.inflate(R.layout.activity_13g_add_comment, container, false);//The container is the rootView.

        myCognito = new MyCognito(getActivity().getApplicationContext(),getActivity().getBaseContext());

        cardClient = myCognito.getCardClient();

        bindActivity();

        return rootView;
    }

    private void bindActivity()
    {
        doneButton = (ImageButton) rootView.findViewById(R.id.add_comment_doneButton);
        doneButton.setVisibility(View.GONE);

        imageView = (ImageView) rootView.findViewById(R.id.add_comment_IV);

        RemoveGlideCacheAsyncTask removeGlideCacheAsyncTask = new RemoveGlideCacheAsyncTask(getActivity().getBaseContext(),Global_Class.getInstance().getValue().user.getUsername());
        removeGlideCacheAsyncTask.execute();


        editText = (EditText) rootView.findViewById(R.id.add_comment_ET);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                enableSubmitButton();
            }

            @Override
            public void afterTextChanged(Editable s)
            {

            }
        });

        imageView = (ImageView) rootView.findViewById(R.id.add_comment_IV);

        doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

            }
        });
    }

    private void enableSubmitButton()
    {
        boolean isReady = editText.getText().toString().length() > 0;
        if(isReady)
        {
            doneButton.setVisibility(View.VISIBLE);
        }
        else
        {
            doneButton.setVisibility(View.GONE);
        }
    }

And here is the xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">
        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/add_comment_IV"/>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Add a comment"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:id="@+id/add_comment_ET"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="5dp"
            android:background="@android:color/transparent"
            android:maxLength="140"/>
        <ImageButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@color/green_main"
            android:id="@+id/add_comment_doneButton"/>
    </LinearLayout>
</LinearLayout>
like image 739
TheQ Avatar asked Dec 03 '22 13:12

TheQ


1 Answers

Open the DialogFragment as below

SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
SampleDialogFragment.setStyle(DialogFragment.STYLE_NO_FRAME, 0);
SampleDialogFragment.show(getActivity().getSupportFragmentManager(), "sometag");

Then in the DailogFragment override onStart() method as below

@Override
public void onStart() {
  super.onStart();
  getDialog().getWindow()
         .setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
}

Then to show softkeyboard try this

((InputMethodManager) sampleedittext.getContext()
        .getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(
        sampleedittext, InputMethodManager.SHOW_IMPLICIT);

or you can Create a custom style for Dialog

<style name="CustomDialog" parent="AppTheme" >
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFullscreen">true</item>
  <item name="android:windowIsFloating">true</item>
  <item name="android:windowCloseOnTouchOutside">true</item>
</style>

then use that style in dialog fragment

@Override public void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
}

@Override public void onStart() {
  super.onStart();
  getDialog().getWindow()
    .setLayout(WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT);
}

SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
SampleDialogFragment.show(getActivity().getSupportFragmentManager(), "sometag");
like image 65
arjun Avatar answered Dec 06 '22 12:12

arjun