Adding styling for ListPopupWindow along with dark theme support.

PiperOrigin-RevId: 249244399
This commit is contained in:
leticiars 2019-05-21 10:18:54 -04:00 committed by Leticia Santos
parent 90837ec485
commit 0f7eaaa94f
9 changed files with 199 additions and 81 deletions

View File

@ -30,6 +30,7 @@ import androidx.annotation.MenuRes;
import androidx.annotation.Nullable;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.widget.ListPopupWindow;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.PopupMenu.OnMenuItemClickListener;
import android.text.Spannable;
@ -44,6 +45,9 @@ import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import io.material.catalog.feature.DemoFragment;
@ -84,6 +88,16 @@ public class MenuMainDemoFragment extends DemoFragment {
TextView contextMenuTextView = view.findViewById(R.id.context_menu_tv);
registerForContextMenu(contextMenuTextView);
Button listPopupWindowButton = view.findViewById(R.id.list_popup_window);
ListPopupWindow listPopupWindow = initializeListPopupMenu(listPopupWindowButton);
listPopupWindowButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
listPopupWindow.show();
}
});
return view;
}
@ -125,32 +139,57 @@ public class MenuMainDemoFragment extends DemoFragment {
popup.show();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
TextView contextMenuTextView = (TextView) v;
Context context = getContext();
menu.add(android.R.string.copy).setOnMenuItemClickListener(
new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
ClipboardManager clipboard = (ClipboardManager) context
.getSystemService(CLIPBOARD_SERVICE);
clipboard.setPrimaryClip(
ClipData.newPlainText(CLIP_DATA_LABEL, contextMenuTextView.getText()));
return true;
}
});
menu.add(android.R.string.copy)
.setOnMenuItemClickListener(
new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
ClipboardManager clipboard =
(ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
clipboard.setPrimaryClip(
ClipData.newPlainText(CLIP_DATA_LABEL, contextMenuTextView.getText()));
return true;
}
});
menu.add(R.string.context_menu_highlight).setOnMenuItemClickListener(
new MenuItem.OnMenuItemClickListener() {
menu.add(R.string.context_menu_highlight)
.setOnMenuItemClickListener(
new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
highlightText(contextMenuTextView);
return true;
}
});
}
private ListPopupWindow initializeListPopupMenu(View v) {
ListPopupWindow listPopupWindow =
new ListPopupWindow(getContext(), null, R.attr.listPopupWindowStyle);
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<>(
getContext(),
R.layout.cat_popup_item,
getResources().getStringArray(R.array.cat_list_popup_window_content));
listPopupWindow.setAdapter(adapter);
listPopupWindow.setAnchorView(v);
listPopupWindow.setOnItemClickListener(
new OnItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
highlightText(contextMenuTextView);
return true;
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Snackbar.make(
getActivity().findViewById(android.R.id.content),
adapter.getItem(position).toString(),
Snackbar.LENGTH_LONG)
.show();
listPopupWindow.dismiss();
}
});
return listPopupWindow;
}
private void highlightText(TextView textView) {
@ -160,10 +199,7 @@ public class MenuMainDemoFragment extends DemoFragment {
context.getTheme().resolveAttribute(R.attr.colorPrimary, value, true);
Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
spanText.setSpan(
new BackgroundColorSpan(value.data),
0,
text.length(),
SPAN_EXCLUSIVE_EXCLUSIVE);
new BackgroundColorSpan(value.data), 0, text.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText);
}
}

View File

@ -15,72 +15,97 @@
limitations under the License.
-->
<LinearLayout
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="match_parent">
<TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/menu_demo_description"/>
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:text="@string/popup_menu"
android:textSize="16sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/menu_demo_description"/>
<Button
android:id="@+id/menu_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:text="@string/show_popup"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:text="@string/popup_menu"
android:textSize="16sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:text="@string/menu_with_icons"
android:textSize="16sp"/>
<Button
android:id="@+id/menu_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:text="@string/show_popup"/>
<Button
android:id="@+id/menu_button_with_icons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:text="@string/show_popup"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:text="@string/menu_with_icons"
android:textSize="16sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:text="@string/context_menu"
android:textSize="16sp"/>
<Button
android:id="@+id/menu_button_with_icons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:text="@string/show_popup"/>
<TextView
android:id="@+id/context_menu_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/menu_context_menu_description"/>
</LinearLayout>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:text="@string/list_popup_window"
android:textSize="16sp"/>
<Button
android:id="@+id/list_popup_window"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:text="@string/show_popup"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingStart="16dp"
android:paddingLeft="16dp"
android:text="@string/context_menu"
android:textSize="16sp"/>
<TextView
android:id="@+id/context_menu_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/menu_context_menu_description"/>
</LinearLayout>
</ScrollView>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 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.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1"/>

View File

@ -23,6 +23,7 @@
<string name="menu_with_icons">Popup menu with icons</string>
<string name="popup_menu">Popup Menu</string>
<string name="context_menu" description="label for section where the context menu is featured [CHAR_LIMIT=20].">Context Menu</string>
<string name="list_popup_window" translatable="false">List Popup Window</string>
<string name="menu_demo_description">
Menus within a Material theme will default to a text style of textAppearanceSubtitle1.
\n\n The menu background adds top and bottom padding, rounded corners, and 8dp elevation.
@ -34,4 +35,11 @@
\n\nIt provides actions that affect the selected content or context frame.
</string>
<string name="context_menu_highlight" description="text for a menu option to highlight text [CHAR_LIMIT=20].">Highlight</string>
<string-array name="cat_list_popup_window_content" translatable="false">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
</string-array>
</resources>

View File

@ -48,6 +48,7 @@
<item name="chipStandaloneStyle">@style/Widget.MaterialComponents.Chip.Entry</item>
<item name="extendedFloatingActionButtonStyle">@style/Widget.MaterialComponents.ExtendedFloatingActionButton.Icon</item>
<item name="floatingActionButtonStyle">@style/Widget.MaterialComponents.FloatingActionButton</item>
<item name="listPopupWindowStyle">@style/Widget.MaterialComponents.PopupMenu.ListPopupWindow.Dark</item>
<item name="materialButtonStyle">@style/Widget.MaterialComponents.Button</item>
<item name="materialButtonOutlinedStyle">@style/Widget.MaterialComponents.Button.OutlinedButton</item>
<item name="materialButtonToggleGroupStyle">@style/Widget.MaterialComponents.MaterialButtonToggleGroup</item>
@ -99,6 +100,7 @@
<item name="chipStandaloneStyle">@style/Widget.MaterialComponents.Chip.Entry</item>
<item name="extendedFloatingActionButtonStyle">@style/Widget.MaterialComponents.ExtendedFloatingActionButton.Icon</item>
<item name="floatingActionButtonStyle">@style/Widget.MaterialComponents.FloatingActionButton</item>
<item name="listPopupWindowStyle">@style/Widget.MaterialComponents.PopupMenu.ListPopupWindow</item>
<item name="materialButtonStyle">@style/Widget.MaterialComponents.Button</item>
<item name="materialButtonOutlinedStyle">@style/Widget.MaterialComponents.Button.OutlinedButton</item>
<item name="materialButtonToggleGroupStyle">@style/Widget.MaterialComponents.MaterialButtonToggleGroup</item>

View File

@ -31,4 +31,10 @@
<item name="android:overlapAnchor">true</item>
</style>
<style name="Base.Widget.MaterialComponents.PopupMenu.ListPopupWindow" parent="Base.Widget.MaterialComponents.PopupMenu.ListPopupWindow">
<item name="android:popupBackground">@drawable/mtrl_popupmenu_background</item>
<item name="android:popupElevation">8dp</item>
<item name="android:overlapAnchor">true</item>
</style>
</resources>

View File

@ -34,4 +34,13 @@
<item name="android:popupBackground">@drawable/mtrl_popupmenu_background_dark</item>
</style>
<style name="Widget.MaterialComponents.PopupMenu.ListPopupWindow" parent="Base.Widget.MaterialComponents.PopupMenu.ListPopupWindow">
<item name="android:popupBackground">@drawable/mtrl_popupmenu_background</item>
<item name="android:popupElevation">8dp</item>
</style>
<style name="Widget.MaterialComponents.PopupMenu.ListPopupWindow.Dark">
<item name="android:popupBackground">@drawable/mtrl_popupmenu_background_dark</item>
</style>
</resources>

View File

@ -41,4 +41,10 @@
<style name="Widget.MaterialComponents.PopupMenu.ContextMenu.Dark"/>
<style name="Base.Widget.MaterialComponents.PopupMenu.ListPopupWindow" parent="Widget.AppCompat.ListPopupWindow"/>
<style name="Widget.MaterialComponents.PopupMenu.ListPopupWindow" parent="Base.Widget.MaterialComponents.PopupMenu.ListPopupWindow"/>
<style name="Widget.MaterialComponents.PopupMenu.ListPopupWindow.Dark"/>
</resources>

View File

@ -51,6 +51,7 @@
<item name="chipStandaloneStyle">@style/Widget.MaterialComponents.Chip.Entry</item>
<item name="extendedFloatingActionButtonStyle">@style/Widget.MaterialComponents.ExtendedFloatingActionButton.Icon</item>
<item name="floatingActionButtonStyle">@style/Widget.MaterialComponents.FloatingActionButton</item>
<item name="listPopupWindowStyle">@style/Widget.MaterialComponents.PopupMenu.ListPopupWindow.Dark</item>
<item name="materialButtonStyle">@style/Widget.MaterialComponents.Button</item>
<item name="materialButtonOutlinedStyle">@style/Widget.MaterialComponents.Button.OutlinedButton</item>
<item name="materialButtonToggleGroupStyle">@style/Widget.MaterialComponents.MaterialButtonToggleGroup</item>
@ -114,6 +115,7 @@
<item name="chipStandaloneStyle">@style/Widget.MaterialComponents.Chip.Entry</item>
<item name="extendedFloatingActionButtonStyle">@style/Widget.MaterialComponents.ExtendedFloatingActionButton.Icon</item>
<item name="floatingActionButtonStyle">@style/Widget.MaterialComponents.FloatingActionButton</item>
<item name="listPopupWindowStyle">@style/Widget.MaterialComponents.PopupMenu.ListPopupWindow</item>
<item name="materialButtonStyle">@style/Widget.MaterialComponents.Button</item>
<item name="materialButtonOutlinedStyle">@style/Widget.MaterialComponents.Button.OutlinedButton</item>
<item name="materialButtonToggleGroupStyle">@style/Widget.MaterialComponents.MaterialButtonToggleGroup</item>