I have RadioGroup called statusGroup that contain two radio button , first one called statusDone the other called statusNotDone
I make the default check radio button is statusNotDone in my xml file android:checked="true" , I have reset button , I need to reinitialize my radio button variable to default.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/statusGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/status"
android:orientation="horizontal"
android:layout_marginTop="12dp" >
<RadioButton
android:id="#+id/statusDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/done_string" />
<RadioButton
android:id="#+id/statusNotDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/not_done_string" />
</RadioGroup>
</RelativeLayout>
my java file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_todo);
mDefaultStatusButton = (RadioButton) findViewById(R.id.statusNotDone);
mDefaultPriorityButton = (RadioButton) findViewById(R.id.medPriority);
mPriorityRadioGroup = (RadioGroup) findViewById(R.id.priorityGroup);
mStatusRadioGroup = (RadioGroup) findViewById(R.id.statusGroup);
}
// TODO - Set up OnClickListener for the Reset Button
final Button resetButton = (Button) findViewById(R.id.resetButton);
resetButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "Entered resetButton.OnClickListener.onClick()");
// TODO - Reset data to default values
mTitleText.setText("");
// how to reinitialize to the default radio buttons
// reset date and time
setDefaultDateTime();
}
});
// Please clear radio group using clearCheck() function
// Check default radio button using check() function
statusGroup.clearCheck();
statusGroup.check(R.id.statusNotDone);
Related
I have many buttons with different color names on them.
yellow - red - blue
I want, when user tap on one it creates a border around it (select the button) and in the end of my activity I have another button to SAVE the color user selected.
<Button
android:text="Yellow"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:id="#+id/button1" />
<Button
android:text="Red"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:id="#+id/button2" />
<Button
android:text="SAVE"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:id="#+id/buttonsave" />
java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color);
Button btnYellow;
btnYellow = (Button) findViewById(R.id.button1);
Button btnRed;
btnRed = (Button) findViewById(R.id.button2);
Intent intent = getIntent();
String url2 = intent.getStringExtra("image");
btnYellow.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
how can I selected a button when user click on it and get a value (red,green, red1) when user click in save?
Place each button in a FrameLayout. This will give the button a border. Changing the background color on the FrameLayout will change the border of the button.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnYellow"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:text="Yellow" />
</FrameLayout>
Set an onClickListener for the buttons that looks something like the following, but don't use the hard-coded colors - this is just an example. mLastClicked is a member variable defined as Button mLastClicked.
#Override
public void onClick(View view) {
if (mLastClicked !=null) {
((FrameLayout) mLastClicked.getParent()).setBackgroundColor(0xFFFFFFFF);
}
mLastClicked = (Button) view;
switch (view.getId()) {
case R.id.btnYellow:
((FrameLayout) view.getParent()).setBackgroundColor(0xFFFFFF00);
break;
case R.id.btnRed:
// Similar to yellow
break;
case R.id.btnSave:
// Do something with mLastClicked to save it
break;
}
}
You can define your button as a shape To give it a border, use the element (name the file your.xml and place it in res/drawables):
and Refer this link
I'm trying to do in android a group of buttons that can be selected and activate only one of them. I need to work with the same logic of a radiogroup and radiobuttons.
I tried many alternatives but I want the most effective way. How can I do it?
You can use this simple way :
1.activity_button_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:id="#+id/btn0"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:id="#+id/btn1"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="#+id/btn2"
android:layout_gravity="center_vertical" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D"
android:id="#+id/btn3"
android:layout_gravity="center_vertical" />
</LinearLayout>
2.ButtonGroupActivity.java
public class ButtonGroupActivity extends Activity implements View.OnClickListener{
private Button[] btn = new Button[4];
private Button btn_unfocus;
private int[] btn_id = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_group);
for(int i = 0; i < btn.length; i++){
btn[i] = (Button) findViewById(btn_id[i]);
btn[i].setBackgroundColor(Color.rgb(207, 207, 207));
btn[i].setOnClickListener(this);
}
btn_unfocus = btn[0];
}
#Override
public void onClick(View v) {
//setForcus(btn_unfocus, (Button) findViewById(v.getId()));
//Or use switch
switch (v.getId()){
case R.id.btn0 :
setFocus(btn_unfocus, btn[0]);
break;
case R.id.btn1 :
setFocus(btn_unfocus, btn[1]);
break;
case R.id.btn2 :
setFocus(btn_unfocus, btn[2]);
break;
case R.id.btn3 :
setFocus(btn_unfocus, btn[3]);
break;
}
}
private void setFocus(Button btn_unfocus, Button btn_focus){
btn_unfocus.setTextColor(Color.rgb(49, 50, 51));
btn_unfocus.setBackgroundColor(Color.rgb(207, 207, 207));
btn_focus.setTextColor(Color.rgb(255, 255, 255));
btn_focus.setBackgroundColor(Color.rgb(3, 106, 150));
this.btn_unfocus = btn_focus;
}
}
You could still use Radio Buttons inside a Radio Group, and attributes to make each radio button look like a button.
In xml, for each radio button set android:button="#null", so that dots won't be visible. You could add some padding to make it look different.
In code, set a CheckedChangeListener to your radioGroup, then find the reference to the checkedView (RadioButton) with checkedId. In this example I've just changed the background color of the view, but you could add a different background too. If the radioButton is not null, it means it has already been changed, so I'll set its initial state again.
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (radioButton != null) {
radioButton.setBackgroundColor(Color.TRANSPARENT);
radioButton.setButtonDrawable(0); // removes the image
}
radioButton = (RadioButton) group.findViewById(checkedId);
radioButton.setBackgroundColor(Color.YELLOW);
radioButton.setButtonDrawable(R.drawable.icon); //sets the image
}
});
Hope this helps!
While you can set this in code as Denny Schuldt suggested, a "cleaner" way is to do it in xml (e.g. drawable/radio.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_checked" android:state_checked="true" />
<item android:drawable="#android:color/transparent" />
</selector>
And set it as button background: android:background="#drawable/radio"
This question already has answers here:
Android radio button uncheck
(6 answers)
Closed 2 years ago.
The application is a step sequencer application with 16 radio groups with 8 buttons in each group. It works perfectly except once a group has a button selected I cant turn it off unless I use the clear button I have created to clear all radiogroups. What I would like to add is some code that says when a selected radio button is selected again it simply turns off like a toggle. I tried using toggles but then other issues arose with that method. Below is an attempt at it but it is way off the mark Im guessing
final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup1);
RadioButton lC1 = (RadioButton)findViewById(R.id.RadioButtonlowC1);
Button D1 = (Button)findViewById(R.id.RadioButtonD1);
D1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PdBase.sendFloat("D1", 74);
int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
RadioButton D1 = (RadioButton) findViewById(selectedTypeId);
if(radioGroup1 != null) // This will be null if none of the radio buttons are selected
radioGroup1.clearCheck();
PdBase.sendFloat("D1", 0);
}
});
You need to put the button in a radio group, then clear the group. A radio button can't be unchecked directly, because the idea is that one option in a group is always checked.
Here is an example of how to create radiogroup, radiobutton and how to use in Java code. Hope it will give you complete picture
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/maptype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/line1"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:drawableRight="#drawable/ic_launcher"/>
<RadioButton
android:id="#+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/ic_launcher"/>
</RadioGroup>
</LinearLayout>
In Java Code
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup rgrpMapType = (RadioGroup) findViewById(R.id.maptype);
int selectedTypeId = rgrpMapType.getCheckedRadioButtonId();
RadioButton rbMapType = (RadioButton) findViewById(selectedTypeId);
if(rbMapType != null) // This will be null if none of the radio buttons are selected
rgrpMapType.clearCheck();
}
I have the following text with me.
"I drink tea and coffee". Now the requirement is to change this text to "I drink EditText AND EditText"....Here the EditText is a edit box where in the user can enter answers once it is clicked. I need to make this change pro grammatically.
Any suggestions on this, as to how this can be achieved????
You could use the following code in button's click event handler:
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
You can ctreate activity structure in your layout xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="#+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="#+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
And set listners to your buttons
private String[] answers = { "tea", "coffee", "juice", "compote" };
...
Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
firstAnswer.setOnClickListener(new OnClickListener() {
private int position = 0;
#Override
public void onClick(View view) {
((Button) view).setText(answers[position]);
position++;
if (position == answers.length)
position = 0;
}
});
By using getText()
Example
Button mButton;
EditText mEdit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}
after that
mEdit.setText("Tea");
EditText et=(EditText)findViewByID(R.id.yourId);
Button bt=(Button)findViewById(R.id.yourBtId);
bt.setOnClickListener(
new View.setOnClickListener()
{
public void onClick(View v)
{
et.setText("You Drink EditText");
}
});
Put These code in onCreate() method
Use ViewSwitcher. This widget displays one of two views it contains:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ViewSwitcher>
And then, when button is pressed, switch it and load text from EditText to TextView or vice versa:
editText.setText(textView.getText());
viewswitcher.showNext();
or
textView.setText(editText.getText());
viewswitcher.showPrevious();
Refer to this for details.
You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively.
On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.
Just Use this : yourTextField.setText("..."+yourEditText.getText().toString());
How to create radio button in radioGroup in file layout without knowing exactly count
Dose anyone has any ideas?
I request you to say thing clearly,but as i got ,you want many number of radio Button,
Take a loop and create dynamic RadioButton add these button to Radio Group
May be this is of any help;
public class RadDemoActivity extends Activity {
private RadioGroup radGroup;
private Button addButton;
private RadioButton radButton;
private static int no;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
no=0;
doInflateItems();
setAddButton();
}
private void doInflateItems() {
radGroup = (RadioGroup) findViewById(R.id.radGroup);
addButton = (Button) findViewById(R.id.addRad);
}
private void setAddButton() {
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
radButton = new RadioButton(getApplicationContext());
radButton.setText("Option " + no++);
radGroup.addView(radButton);
}
});
}
}
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/addRad" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add" />
<ScrollView android:id="#+id/vScroll" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioGroup android:id="#+id/radGroup"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
This code is adding radio buttons from a button click, you can do the same from a loop if you want to add multiple buttons at once. Hope this helps.