I've made a construction in Android to easy build & show a DialogFragment on the screen. The dialog contains a TextView, ListView and a Button. It's build to easily inflate different views in the ListView so I can make custom Dialogs.
The left image is correct. On the right I inflated an extra view. A view I use to display text in the DialogFragment. But when I add that view it doesn't wrap content anymore. The ListView makes itself scrollable.
I can add more of the bluebuttons you see on the image and the ListView will still wrap_content correct. But when I add one or more of the other views it the ListView makes itself scrollable.
The problem is I just can't get the ListView to wrap_content correct.
This is the xml of the "blue button" view: (the correct inflated view)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overlay_item_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/tile_button_categorytile"
android:orientation="horizontal">
<ImageView
android:id="@+id/popUpImage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_speaker" />
<com.joanzapata.iconify.widget.IconTextView
android:id="@+id/popUpButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="4dp"
android:layout_marginRight="15dp"
android:layout_weight="1"
android:padding="4dp"
android:text="Bewerken {fa-android}"
android:textColor="@color/white"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
The view used as text in the DialogFragment: (which causes the ListView to make itself scrollable)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overlay_item_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
android:layout_weight="1"
android:weightSum="1">
<TextView
android:id="@+id/LabelOverlay"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/placeholder"
android:textColor="@color/purple"
android:textSize="12sp" />
</LinearLayout>
When I add extra paddingTop and paddingBottom to the view that is used to show text, the ListView doesn't make itself scrollable (on only a few devices). The padding is set to around 20dp and then it on some devices it doesn't make itself scrollable, but then on other devices its too much padding so it shows whitespace...
This is the code of the fragment that shows the DialogFragment:
public class PopUpFragmentPhone extends DialogFragment {
PopUp popUpData;
ListView listView;
public static PopUpFragmentPhone newInstance(PopUp data) {
PopUpFragmentPhone popUpFragment = new PopUpFragmentPhone();
Bundle args = new Bundle();
args.putParcelable("PopUpData", data);
popUpFragment.setArguments(args);
return popUpFragment;
}
@Override
public void onStart(){
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.70f;
windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(windowParams);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().setLayout(MainApplication.width / 2, ViewGroup.LayoutParams.WRAP_CONTENT);
getDialog().setCanceledOnTouchOutside(true);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme);
popUpData = getArguments().getParcelable("PopUpData"); //get the PopUp class.
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.pop_up_phone, container, false);
//Set the title of the popUp
TextView popUpTitle = (TextView)v.findViewById(R.id.overlayTitle);
popUpTitle.setText(popUpData.getHeader());
listView = (ListView) v.findViewById(R.id.listViewItemsOverlay);
listView.setAdapter(new PopUpAdapterPhone(MainApplication.getContext(), 1, popUpData.getItems(), this));
boolean showCancel = popUpData.isCancel();
Button cancelButton = (Button) v.findViewById(R.id.cancel_button);
Drawable cancelIcon = new IconDrawable(getActivity(), FontAwesomeIcons.fa_times).colorRes(R.color.red).sizeDp(20);
cancelButton.setCompoundDrawablesWithIntrinsicBounds(cancelIcon, null, null, null);
if(showCancel) {
// Cancel button click
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getDialog().dismiss();
}
});
} else {
cancelButton.setVisibility(View.GONE);
}
getDialog().getWindow().setLayout(MainApplication.width / 2, ViewGroup.LayoutParams.WRAP_CONTENT);//set the size of the fragment, so the rest is a touch to cancel the popUP
return v;
}
}
The view of the pop up fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="5dp"
android:weightSum="3"
android:background="@drawable/tile_button_wordtile"
android:id="@+id/overlay_master_view">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/menu"
android:id="@+id/overlayTitle"
android:textStyle="bold"
android:textSize="15sp"
android:padding="4dp"
android:textColor="@color/purple"
android:layout_weight="1"
android:background="@color/white"
android:layout_margin="4dp"
android:layout_gravity="center_horizontal" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_weight="1"
android:divider="@android:color/transparent"
android:dividerHeight="5.0sp"
android:id="@+id/listViewItemsOverlay">
</ListView>
<com.joanzapata.iconify.widget.IconButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:padding="5dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="15sp"
android:drawablePadding="5dp"
android:id="@+id/cancel_button"
android:layout_marginTop="5dp"
android:textColor="@color/red"
android:background="@drawable/tile_button_functiontile"
android:text="@string/cancel"/>
</LinearLayout>
I tried lots of things, inflate it in a LinearLayout or RelativeLayout. Wrap_content, match_parent, weightSum and all that kinds of stuff. Tried changing the layouts in a lot of ways but it all didn't work out.
I hope someone can help me.