Merge "Sample app for appcompat-v7's PopupMenu." into mnc-ub-dev

This commit is contained in:
Kirill Grouchnikov
2016-01-22 15:34:20 +00:00
committed by Android (Google) Code Review
6 changed files with 196 additions and 2 deletions

View File

@@ -482,6 +482,16 @@
<category android:name="com.example.android.supportv7.SAMPLE_CODE"/>
</intent-filter>
</activity>
<!-- PopupMenu demo activity -->
<activity android:name=".widget.PopupMenuActivity"
android:label="@string/popup_menu_activity"
android:theme="@style/Theme.AppCompat.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="com.example.android.supportv7.SAMPLE_CODE"/>
</intent-filter>
</activity>
</application>

View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 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.
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/test_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/popup_menu_button" />
<TextView
android:id="@+id/log"
android:layout_width="match_parent"
android:layout_height="150dip"
android:layout_alignParentBottom="true"
android:padding="8dip"
android:background="#E5E5E5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/log"
android:padding="8dip"
android:textStyle="bold"
android:text="@string/popup_menu_summary" />
</RelativeLayout>

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 Google Inc.
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.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_highlight"
android:title="@string/popup_menu_highlight" />
<item android:id="@+id/action_edit"
android:title="@string/popup_menu_edit" />
<item android:id="@+id/action_delete"
android:title="@string/popup_menu_delete" />
<item android:id="@+id/action_ignore"
android:title="@string/popup_menu_ignore" />
<item android:id="@+id/action_share"
android:title="@string/popup_menu_share">
<menu>
<item android:id="@+id/action_share_email"
android:title="@string/popup_menu_share_email" />
<item android:id="@+id/action_share_circles"
android:title="@string/popup_menu_share_circles" />
</menu>
</item>
<item android:id="@+id/action_print"
android:title="@string/popup_menu_print" />
</menu>

View File

@@ -201,5 +201,17 @@
<string name="list_popup_window_summary">This activity illustrates the use of list popup windows. The popup window is shown by clicking the button above. The checkbox controls the modality. The text area below logs various events - it should log a tap on itself when a non-modal popup window is dismissed by tapping on the log area.</string>
<string name="list_popup_window_button">Show popup!</string>
<string name="list_popup_window_is_modal">is modal</string>
<string name="popup_menu_activity">Popup menu</string>
<string name="popup_menu_summary">This activity illustrates the use of popup menus. The popup menu is shown by clicking the button above. The text area below logs various events.</string>
<string name="popup_menu_button">Show popup!</string>
<string name="popup_menu_highlight">Highlight</string>
<string name="popup_menu_edit">Edit</string>
<string name="popup_menu_delete">Delete</string>
<string name="popup_menu_ignore">Ignore</string>
<string name="popup_menu_share">Share</string>
<string name="popup_menu_share_email">Via email</string>
<string name="popup_menu_share_circles">To my circles</string>
<string name="popup_menu_print">Print</string>
</resources>

View File

@@ -26,7 +26,6 @@ import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.example.android.supportv7.R;
@@ -112,7 +111,7 @@ public class ListPopupWindowActivity extends AppCompatActivity {
mListPopupWindow.setAdapter(mListPopupAdapter);
mListPopupWindow.setAnchorView(mButton);
// Register a listener to be notified when an item in out popup window has
// Register a listener to be notified when an item in our popup window has
// been clicked.
mListPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override

View File

@@ -0,0 +1,91 @@
/*
* Copyright (C) 2016 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.
*/
package com.example.android.supportv7.widget;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.example.android.supportv7.R;
import java.text.SimpleDateFormat;
import java.util.Date;
public class PopupMenuActivity extends AppCompatActivity {
private ViewGroup mContainer;
private TextView mLog;
private Button mButton;
private PopupMenu mPopupMenu;
private SimpleDateFormat mDateFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup_menu_activity);
mDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
mContainer = (ViewGroup) findViewById(R.id.container);
mLog = (TextView) mContainer.findViewById(R.id.log);
mButton = (Button) mContainer.findViewById(R.id.test_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPopupMenu = new PopupMenu(mContainer.getContext(), mButton);
final MenuInflater menuInflater = mPopupMenu.getMenuInflater();
menuInflater.inflate(R.menu.popup_menu, mPopupMenu.getMenu());
// Register a listener to be notified when a menu item in our popup menu has
// been clicked.
mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
addToLog("Item '"+ item.getTitle() + "' clicked");
return true;
}
});
// Register a listener to be notified when our popup menu is dismissed.
mPopupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
@Override
public void onDismiss(PopupMenu menu) {
addToLog("Popup menu dismissed");
}
});
// Show the popup menu
mPopupMenu.show();
}
});
}
private void addToLog(String toLog) {
String toPrepend = mDateFormat.format(new Date()) + " " + toLog + "\n";
mLog.setText(toPrepend + mLog.getText());
}
}