Skip to content

Most visited

Recently visited

navigation
AppRestrictionEnforcer / src / com.example.android.apprestrictionenforcer /

ItemAddFragment.java

1
/*
2
 * Copyright (C) 2015 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.apprestrictionenforcer;
18
 
19
import android.content.Context;
20
import android.os.Bundle;
21
import android.support.annotation.Nullable;
22
import android.support.v4.app.DialogFragment;
23
import android.support.v4.app.Fragment;
24
import android.text.TextUtils;
25
import android.view.LayoutInflater;
26
import android.view.View;
27
import android.view.ViewGroup;
28
import android.widget.EditText;
29
import android.widget.Toast;
30
 
31
/**
32
 * Provides a dialog to create a new restriction item for the sample bundle array.
33
 */
34
public class ItemAddFragment extends DialogFragment implements View.OnClickListener {
35
 
36
    public interface OnItemAddedListener {
37
        void onItemAdded(String key, String value);
38
    }
39
 
40
    private OnItemAddedListener mListener;
41
    private EditText mEditKey;
42
    private EditText mEditValue;
43
 
44
    @Override
45
    public void onAttach(Context context) {
46
        super.onAttach(context);
47
        Fragment parentFragment = getParentFragment();
48
        mListener = (OnItemAddedListener) (parentFragment == null ? context : parentFragment);
49
    }
50
 
51
    @Override
52
    public void onDetach() {
53
        mListener = null;
54
        super.onDetach();
55
    }
56
 
57
    @Nullable
58
    @Override
59
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
60
                             Bundle savedInstanceState) {
61
        getDialog().setTitle(R.string.add_item);
62
        return inflater.inflate(R.layout.fragment_item_add, container, false);
63
    }
64
 
65
    @Override
66
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
67
        mEditKey = (EditText) view.findViewById(R.id.key);
68
        mEditValue = (EditText) view.findViewById(R.id.value);
69
        view.findViewById(R.id.ok).setOnClickListener(this);
70
    }
71
 
72
    @Override
73
    public void onClick(View v) {
74
        switch (v.getId()) {
75
            case R.id.ok:
76
                if (addItem()) {
77
                    dismiss();
78
                }
79
                break;
80
        }
81
    }
82
 
83
    private boolean addItem() {
84
        String key = mEditKey.getText().toString();
85
        if (TextUtils.isEmpty(key)) {
86
            Toast.makeText(getActivity(), "Input the key.", Toast.LENGTH_SHORT).show();
87
            return false;
88
        }
89
        String value = mEditValue.getText().toString();
90
        if (TextUtils.isEmpty(value)) {
91
            Toast.makeText(getActivity(), "Input the value.", Toast.LENGTH_SHORT).show();
92
            return false;
93
        }
94
        if (mListener != null) {
95
            mListener.onItemAdded(key, value);
96
        }
97
        return true;
98
    }
99
 
100
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.