am 2869b3d2: Merge "Add DialogFragment demo" into mnc-dev
* commit '2869b3d2ae129f0fb9f3b5ad807d509f260f43bc': Add DialogFragment demo
This commit is contained in:
@@ -334,6 +334,15 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity android:name=".app.DialogFragmentUsage"
|
||||
android:label="@string/dialogfragment_usage"
|
||||
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>
|
||||
|
||||
<provider android:name=".app.RecentSuggestionsProvider"
|
||||
android:authorities="com.example.android.supportv7.RecentSuggestionsProvider" />
|
||||
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
|
||||
<string-array name="dialog_types">
|
||||
<item>Simple</item>
|
||||
<item>Simple with Action Bar</item>
|
||||
<item>Button bar</item>
|
||||
</string-array>
|
||||
|
||||
|
||||
@@ -139,6 +139,7 @@
|
||||
<string name="dialog_title">My great dialog</string>
|
||||
<string name="dialog_content">My great dialog is great</string>
|
||||
<string name="alert_dialog_usage">AppCompat/Dialog/AlertDialog Usage</string>
|
||||
<string name="dialogfragment_usage">AppCompat/Dialog/DialogFragment Usage</string>
|
||||
|
||||
<string name="sample_media_route_provider_remote">Remote Playback (Simulated)</string>
|
||||
<string name="sample_media_route_activity_local">Local Playback</string>
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright (C) 2015 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.app;
|
||||
|
||||
import com.example.android.supportv7.R;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.view.WindowCompat;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.app.AppCompatDialog;
|
||||
import android.support.v7.app.AppCompatDialogFragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* This demonstrates idiomatic usage of AppCompatDialogFragment.
|
||||
*/
|
||||
public class DialogFragmentUsage extends AppCompatActivity {
|
||||
|
||||
private Spinner mSpinner;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.dialog_usage);
|
||||
|
||||
mSpinner = (Spinner) findViewById(R.id.spinner_dialogs);
|
||||
|
||||
// Add an OnClickListener to show our selected dialog
|
||||
findViewById(R.id.btn_show_dialog).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
showSelectedDialog();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showSelectedDialog() {
|
||||
switch (mSpinner.getSelectedItemPosition()) {
|
||||
case 0:
|
||||
showSimpleDialog();
|
||||
break;
|
||||
case 1:
|
||||
showButtonBarDialog();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void showSimpleDialog() {
|
||||
MenuDialogFragment fragment = MenuDialogFragment.create(R.layout.dialog_content);
|
||||
fragment.show(getSupportFragmentManager(), null);
|
||||
}
|
||||
|
||||
private void showButtonBarDialog() {
|
||||
MenuDialogFragment fragment = MenuDialogFragment.create(R.layout.dialog_content_buttons);
|
||||
fragment.show(getSupportFragmentManager(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple {@link AppCompatDialog} implementation which
|
||||
* inflates some items into it's options menu, and shows a toast when one is selected.
|
||||
*/
|
||||
public static class MenuDialogFragment extends AppCompatDialogFragment {
|
||||
|
||||
private static final String PARAM_CONTENT_VIEW = "content_view";
|
||||
|
||||
static MenuDialogFragment create(int contentView) {
|
||||
Bundle b = new Bundle();
|
||||
b.putInt(PARAM_CONTENT_VIEW, contentView);
|
||||
|
||||
MenuDialogFragment fragment = new MenuDialogFragment();
|
||||
fragment.setArguments(b);
|
||||
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
Bundle args = getArguments();
|
||||
int contentView = args.getInt(PARAM_CONTENT_VIEW);
|
||||
return inflater.inflate(contentView, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.actions, menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
Toast.makeText(getActivity(), "Dialog action selected: " + item.getTitle(),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -58,9 +58,6 @@ public class DialogUsage extends AppCompatActivity {
|
||||
showSimpleDialog();
|
||||
break;
|
||||
case 1:
|
||||
showSimpleDialogWithActionBar();
|
||||
break;
|
||||
case 2:
|
||||
showButtonBarDialog();
|
||||
break;
|
||||
}
|
||||
@@ -73,15 +70,6 @@ public class DialogUsage extends AppCompatActivity {
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void showSimpleDialogWithActionBar() {
|
||||
AppCompatDialog dialog = new MenuDialog(this);
|
||||
// Request the support Action Bar window feature
|
||||
dialog.supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
|
||||
dialog.setTitle(R.string.dialog_title);
|
||||
dialog.setContentView(R.layout.dialog_content);
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void showButtonBarDialog() {
|
||||
Dialog dialog = new AppCompatDialog(this);
|
||||
dialog.setTitle(R.string.dialog_title);
|
||||
|
||||
Reference in New Issue
Block a user