Stop using AlertDialog for TrackSelectionDialog.

AlertDialog owns its view, which causes problems with TabLayout on API 21
and 22. Now using AppCompatDialog instead so that we can own the view
ourselves.

Also:
 - Renamed layout files from download_xyz to track_selection_xyz.
 - Added OK and Cancel buttons to the view.
 - Applied alert dialog style to the "normal" dialog to get a nicer UI.
PiperOrigin-RevId: 233944647
This commit is contained in:
tonihei 2019-02-14 14:35:27 +00:00 committed by Andrew Lewis
parent 73d9b2d21a
commit 1c38b226ea
4 changed files with 73 additions and 39 deletions

View file

@ -15,7 +15,6 @@
*/
package com.google.android.exoplayer2.demo;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.Resources;
@ -27,10 +26,13 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatDialog;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
@ -162,11 +164,15 @@ public final class TrackSelectionDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle(titleId)
.setPositiveButton(android.R.string.ok, onClickListener)
.setNegativeButton(android.R.string.cancel, /* listener= */ null)
.create();
// We need to own the view to let tab layout work correctly on all API levels. That's why we
// can't use an AlertDialog as it owns its view itself. Using AppCompatDialog instead. We still
// want the "alertDialogTheme" style attribute of the current theme instead of AppCompatDialog's
// default "dialogTheme" style, so obtain that manually.
TypedValue alertDialogStyle = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.alertDialogTheme, alertDialogStyle, true);
AppCompatDialog dialog = new AppCompatDialog(getActivity(), alertDialogStyle.resourceId);
dialog.setTitle(titleId);
return dialog;
}
@Override
@ -200,12 +206,19 @@ public final class TrackSelectionDialog extends DialogFragment {
tabFragments.put(i, tabFragment);
tabTitles.add(trackTypeString);
}
View dialogView = inflater.inflate(R.layout.download_dialog, container, false);
TabLayout tabLayout = dialogView.findViewById(R.id.download_dialog_tab_layout);
ViewPager viewPager = dialogView.findViewById(R.id.download_dialog_view_pager);
View dialogView = inflater.inflate(R.layout.track_selection_dialog, container, false);
TabLayout tabLayout = dialogView.findViewById(R.id.track_selection_dialog_tab_layout);
ViewPager viewPager = dialogView.findViewById(R.id.track_selection_dialog_view_pager);
Button cancelButton = dialogView.findViewById(R.id.track_selection_dialog_cancel_button);
Button okButton = dialogView.findViewById(R.id.track_selection_dialog_ok_button);
viewPager.setAdapter(new FragmentAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
((AlertDialog) getDialog()).setView(dialogView);
cancelButton.setOnClickListener(view -> dismiss());
okButton.setOnClickListener(
view -> {
onClickListener.onClick(getDialog(), DialogInterface.BUTTON_POSITIVE);
dismiss();
});
return dialogView;
}
@ -281,7 +294,8 @@ public final class TrackSelectionDialog extends DialogFragment {
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View rootView =
inflater.inflate(R.layout.download_dialog_tab, container, /* attachToRoot= */ false);
inflater.inflate(
R.layout.track_selection_dialog_tab, container, /* attachToRoot= */ false);
trackSelectionView = rootView.findViewById(R.id.download_dialog_track_selection_view);
trackSelectionView.setShowDisableOption(true);
trackSelectionView.setAllowMultipleOverrides(allowMultipleOverrides);

View file

@ -1,28 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2018 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/download_dialog_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/download_dialog_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

View file

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2018 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/track_selection_dialog_view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.design.widget.TabLayout
android:id="@+id/track_selection_dialog_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.view.ViewPager>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end">
<Button
android:id="@+id/track_selection_dialog_cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/cancel"
style="?android:attr/borderlessButtonStyle"/>
<Button
android:id="@+id/track_selection_dialog_ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/ok"
style="?android:attr/borderlessButtonStyle"/>
</LinearLayout>
</LinearLayout>