Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2007 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.apis.view;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.Activity;
     22 import android.os.Bundle;
     23 import android.view.View;
     24 import android.widget.TextView;
     25 import android.widget.RadioGroup;
     26 import android.widget.Button;
     27 import android.widget.RadioButton;
     28 import android.widget.LinearLayout;
     29 
     30 
     31 public class RadioGroup1 extends Activity implements RadioGroup.OnCheckedChangeListener,
     32         View.OnClickListener {
     33 
     34     private TextView mChoice;
     35     private RadioGroup mRadioGroup;
     36 
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40 
     41         setContentView(R.layout.radio_group_1);
     42         mRadioGroup = (RadioGroup) findViewById(R.id.menu);
     43 
     44         // test adding a radio button programmatically
     45         RadioButton newRadioButton = new RadioButton(this);
     46         newRadioButton.setText(R.string.radio_group_snack);
     47         newRadioButton.setId(R.id.snack);
     48         LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
     49                 RadioGroup.LayoutParams.WRAP_CONTENT,
     50                 RadioGroup.LayoutParams.WRAP_CONTENT);
     51         mRadioGroup.addView(newRadioButton, 0, layoutParams);
     52 
     53         // test listening to checked change events
     54         String selection = getString(R.string.radio_group_selection);
     55         mRadioGroup.setOnCheckedChangeListener(this);
     56         mChoice = (TextView) findViewById(R.id.choice);
     57         mChoice.setText(selection + mRadioGroup.getCheckedRadioButtonId());
     58 
     59         // test clearing the selection
     60         Button clearButton = (Button) findViewById(R.id.clear);
     61         clearButton.setOnClickListener(this);
     62     }
     63 
     64     public void onCheckedChanged(RadioGroup group, int checkedId) {
     65         String selection = getString(R.string.radio_group_selection);
     66         String none = getString(R.string.radio_group_none);
     67         mChoice.setText(selection +
     68                 (checkedId == View.NO_ID ? none : checkedId));
     69     }
     70 
     71     public void onClick(View v) {
     72         mRadioGroup.clearCheck();
     73     }
     74 }
     75