how to display a edittext when radiobutton is clicked in android - android

I have a program which computes perimeter, circumference, an area of different shapes like, square, circle, rectangle and soon. Any help with enabling the radiobutton to display an edittext to write the measurements.

check below code don't use as it is, its just reference
inyourxml.xml
<RadioGroup
android:id="#+id/groupRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/app_big_margin"
android:layout_marginRight="#dimen/app_normal_margin"
android:gravity="center"
android:minHeight="30dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/circumferenceRadio"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/tab_selector_background"
android:button="#null"
android:checked="true"
android:gravity="center"
android:text="#string/choice_yes"
android:textColor="#drawable/tab_selector_text" />
<RadioButton
android:id="#+id/perimeterRadio"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/tab_selector_background"
android:button="#null"
android:checked="false"
android:gravity="center"
android:text="#string/choice_no"
android:textColor="#drawable/tab_selector_text" />
</RadioGroup>
<EditText
android:id="#+id/perimeterEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/app_normal_margin"
android:layout_marginRight="#dimen/app_normal_margin"
android:background="#drawable/action_bar_background"
android:inputType="textPassword" />
<EditText
android:id="#+id/circumferenceEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/app_normal_margin"
android:layout_marginRight="#dimen/app_normal_margin"
android:background="#drawable/action_bar_background"
android:inputType="textEmailAddress" />
inyourjava.java
RadioGroup groupRadio=(RadioGroup)rootView.findViewById(R.id.groupRadio);
EditText perimeterEditText=(EditText)rootView.findViewById(R.id.perimeterEditText);
EditText circumferenceEditText=(EditText)rootView.findViewById(R.id.circumferenceEditText);
groupRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.circumferenceRadio)
{
circumferenceEditText.setVisibility(View.VISIBLE);
perimeterEditText.setVisibility(View.INVISIBLE);
}
else if(checkedId==R.id.perimeterRadio)
{
perimeterEditText.setVisibility(View.VISIBLE);
circumferenceEditText.setVisibility(View.INVISIBLE);
}
}
});

Hide a EditText & make it visible by clicking a menu
in case of menu ... you can declare setvisibility in xml and make it hide... then programatically make it visible...hope this helps

Related

Disable Click on RadioGroup inside layout

I want to disable the clicks on the RadioGroup placed inside the Relative Layout! In a way that my only Relative Layout should be clickable. I want this on some certain condition my XML is as follow:
<RelativeLayout
android:id="#+id/shouldNotPlayLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_marginLeft="44dp"
android:layout_marginRight="44dp"
android:layout_marginTop="40dp">
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:saveEnabled="false">
<RadioButton
android:id="#+id/radio_option_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/radio_option_selector"
android:button="#null"
android:padding="15dp"
android:textColor="#161743"
android:textSize="14sp"
android:textStyle="normal"
tools:text="First option"/>
<RadioButton
android:id="#+id/radio_option_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/radio_option_selector"
android:button="#null"
android:padding="15dp"
android:textColor="#161743"
android:textSize="14sp"
android:textStyle="normal"
tools:text="First option"/>
<RadioButton
android:id="#+id/radio_option_c"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/radio_option_selector"
android:button="#null"
android:padding="15dp"
android:textColor="#161743"
android:textSize="14sp"
android:textStyle="normal"
tools:text="First option"/>
</RadioGroup>
</RelativeLayout>
I am stopping the clicks on the radio group as:
binding.questionLayout.radioOptionA.setEnabled(false);
binding.questionLayout.radioOptionB.setEnabled(false);
binding.questionLayout.radioOptionC.setEnabled(false);
binding.questionLayout.radioOptionA.setFocusable(false);
binding.questionLayout.radioOptionB.setFocusable(false);
binding.questionLayout.radioOptionC.setFocusable(false);
binding.questionLayout.shouldNotPlayLayout.requestFocus();
binding.questionLayout.shouldNotPlayLayout.setClickable(true);
binding.questionLayout.shouldNotPlayLayout.setFocusable(true);
The Problem I am facing is that sometimes any of these three radio button can still be clickable and the user can click any radio button. But I want to disable the click on the radio group/button and get the click done on the relative layout!
Can somebody please figure any blunders done by me?
Add binding.questionLayout.radio_group.setEnabled(false)
I have made a demo with your requirement & here's what you need,
private void manageEnableState(boolean isEnable) {
radioOptionA.setEnabled(isEnable);
radioOptionB.setEnabled(isEnable);
radioOptionC.setEnabled(isEnable);
radioOptionA.setFocusable(isEnable);
radioOptionB.setFocusable(isEnable);
radioOptionC.setFocusable(isEnable);
shouldNotPlayLayout.setClickable(!isEnable);
shouldNotPlayLayout.setFocusable(!isEnable);
}
Where isEnable is used to set your RadioButton enable state..
Please I giving what you have done but then also please try following the way it will help,
private void enableDisbleRadioGroup(RadioGroup groupId, boolean isEnable) {
for (int i = 0; i < groupId.getChildCount(); i++) {
groupId.getChildAt(i).setEnabled(isEnable);
}
}
In above method pass your radio group id and enable disable mode what you want,

android layout radiogroup position

I have radiogroup :
<RadioGroup android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#drawable/overlay_2"
android:id="#+id/rgSettings">`
<RadioButton android:layout_width="140dp"
android:layout_height="100dp"
android:id="#+id/s_mph"
android:background="#drawable/settings_speed_mph"
android:button="#android:color/transparent"
android:layout_gravity="center_vertical|left"/>
<RadioButton android:layout_width="140dp"
android:layout_height="100dp"
android:id="#+id/s_kph"
android:checked="false"
android:background="#drawable/settings_speed_kph"
android:button="#android:color/transparent"
android:layout_gravity="center"
android:gravity="right"/>
</RadioGroup>
Link in screenshot : http://joxi.ru/A5GhUxjKTJBQH7uRaK4
(1) radiogroup
(2) - (3) its radio buttons.
I need radibutton (2) set in position - left, radiobutton (3) set in right position. What do I need for this?
Add:
i need set first button set left position in radiogroup, secondary button set in right position radiogroup. Like This: joxi.ru/FqKhUxjKTJBsH5OWteQ
Why You are setting the button that You need on the left side as first? Just switch it:
<RadioGroup android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#drawable/overlay_2"
android:id="#+id/rgSettings">
<RadioButton android:layout_width="140dp"
android:layout_height="100dp"
android:id="#+id/s_kph"
android:checked="false"
android:background="#drawable/settings_speed_kph"
android:button="#android:color/transparent"
android:layout_gravity="center_vertical|left"
android:gravity="right"/>
<RadioButton android:layout_width="140dp"
android:layout_height="100dp"
android:id="#+id/s_mph"
android:background="#drawable/settings_speed_mph"
android:button="#android:color/transparent"
android:layout_gravity="center_vertical|right"/>
</RadioGroup>
Also, You can play a little bit with the layout_margin attributes to make space between the buttons.

Change layout_bellow attribute field to another field programmatically

Given the following code:
<RelativeLayout
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textPersonalInfoEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:hint="Edit to set your personal info"
android:textSize="16sp"
android:visibility="gone" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="15dp"
android:hint="Name"
android:visibility="gone"
android:textSize="16sp" />
<Button
android:id="#+id/insertGameId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editName"
android:layout_marginTop="20dp"
android:text="Insert" />
</RelativeLayout>
The TextView and the EditText are visible depending the user interaction and both will never be visible at the same time. One per time only!
The problem is the third component(Button) need to change the attribute android:layout_below depending on which component is visible.
I need to this programmatically.
Any help?
Use Relativelayout for your textview and edittext. And relativelayout's height:wrap-content
Then give an id to your relativelayout
and then add this line your xml file for your button
<Button
android:layout_below="#id/RealtiveLayoutid" />
Thats all
First, use a ToggleButton instead of Button. It can have all the attributes that you assigned to it.
Next add this to the ToggleButton:
<ToggleButton
....
android:textOn="#string/ifTextView"
android:textOff="#string/ifTextBox"
android:onClick="onToggleClicked"
/>
Then this for the activity
public void onToggleClicked(View v) {
boolean on = ((ToggleButton) v).isChecked();
if(on) {
theTextView.setVisibility(View.VISIBLE);
theTextBox.setVisibility(View.INVISIBLE);
} else {
theTextBox.setVisibility(View.VISIBLE);
theTextView.setVisibility(View.INVISIBLE);
}
}
Remember to import the required classes.

How to change Switch text to right side in android?

Android default Switch have text and then Switch on/off. I'm Expecting Switch on/off and then text. Don't use another widgets like TextView. How can i set Switch right side text.
Wrap it in another layout with no text value and add a textview. see my code:
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_height="wrap_content">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/reminder" />
<TextView
android:layout_width="wrap_content"
android:text="Meeting Reminders"
android:layout_height="wrap_content" />
</LinearLayout>
Use
android:layoutDirection="rtl"
Then
app:switchPadding="8dp"
Add this code on Xml file:
<Switch
android:id="#+id/simpleSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:checked="false"
android:textColor="#color/black"
android:textOff="#string/no"
android:textOn="#string/yes"
android:theme="#style/SCBSwitch" />
<TextView
android:id="#+id/switchBtn_txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/simpleSwitch"
android:gravity="center_horizontal" />
In activity Or Fragment :
Switch simpleSwitchBtn = (Switch) findViewById(R.id.simpleSwitch);
final TextView switchBtn_txtView = (TextView) findViewById(R.id.switchBtn_txtView);
simpleSwitchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
switchBtn_txtView.setText("Yes");
}
else {
switchBtn_txtView.setText("No");
}
}
});
I think Its helpfull to you....
Thank you
I was able to achieve the look I wanted with a combination of:
width (120dp)
padding (75dp)
and switchpadding (-90dp)

Only one radio button selected at a time

I have two radio button in a radio group. I also have 2 androd:button checkbox- for when the radio button is deselected and checkbox_v for when he user selects the checkbox. I also implemnted that a method onRadioButtonClick in order to make sure that only one radio button had drawable:checkbox and the other has checkbox_v . How can I implemnt onRadioClick to do this? any idea?
public void onRadioButtonClick(View v)
{
RadioButton button = (RadioButton) v;
boolean checkBox1Selected;
boolean checkBox2Selected = false;
Toast.makeText(MainActivity.this,
button.getId()+ " was chosen."+R.id.wificheckBox,
Toast.LENGTH_SHORT).show();
if ( button.getId() ==R.id.wificheckBox) {
// Toggle status of checkbox selection
checkBox1Selected = radiowifiButton.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox2Selected) {
radiomobileButton.setChecked(false);
checkBox2Selected = false;
}
else if (button.getId() ==R.id.wifimobilecheckBox) {
// Toggle status of checkbox selection
checkBox2Selected = radiomobileButton.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox1Selected) {
radiowifiButton.setChecked(false);
checkBox1Selected = false;
}
}
}
main xml
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_1"
android:layout_marginLeft="20dp">
<LinearLayout
android:id="#+id/ll_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_1"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/wifimobilecheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/button_radio"
android:checked="true"
android:onClick="onRadioButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/wificheckBox"
android:layout_toRightOf="#+id/wificheckBox"
android:paddingLeft="15dp"
android:text="WiFi or mobile network"
android:textColor="#333333"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_2"
android:paddingTop="20dp"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/wificheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/button_radio"
android:onClick="onRadioButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/wificheckBox"
android:layout_toRightOf="#+id/wificheckBox"
android:paddingLeft="15dp"
android:text="WiFi "
android:textColor="#333333"
android:textSize="20dp" />
</LinearLayout>
</RadioGroup>
Drawabe- button_radio
<item android:state_checked="true"android:state_pressed="false" android:drawable="#drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="#drawable/checkbox"/>
If they are in a radiogroup, only one can be selected at a time. If you want both to be able to be selected, remove them from the radiogroup.
As the top voted answer didn't work for me as for many others. I am sharing the method that i used and its working perfectly.
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/rb_female"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:button="#drawable/selector_radio_button"
android:text="Female" />
<RadioButton
android:id="#+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/selector_radio_button"
android:text="Male" />
</RadioGroup>
Setting the android:checkedButton="#+id/rb_female" worked for me in RadioGroup.
You should remove your onRadioButtonClick method and add OnCheckedChangeListener to your radiogroup. http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
I guess by implementing your own click handler your are overwriting some functionality of the radiogroup.
Your requirement is not clear. Would you mind to make it simpler. I tried my best to understand your requirements and these are the findings given below :(.
A radio button can have text. So you don't have to add another TextView in your layout. Please remove them and add a simple android:text="blah.. blah.." to your radiobutton.
With my understanding you have two doubts.
How to customize RadioButtons with custom drawables??
Checkout the answer here.
How to deselect the previously selected radiobutton??
The truth is you don't have to do it manually as you are keeping those radiobuttons into a single radiogroup.
Hope this will help you.
To check/uncheck radio buttons not part of a RadioGroup,
add a method in the android GUI designer under the
RadioButton Properties 'onClick' (In this example radio1_onClick).
In your activity.xml under the xml for the RadioButton
RadioButton
[blah blah blah]
android:id="#+id/radio1"
android:onClick="radio1_onClick"
In your activity_main.xml you would write the following method.
private void radio1_onClick(View v)
{
CompoundButton b=(CompoundButton)findViewById(R.id.radio1);
b.setChecked(true); /* light the button */
//b.setChecked(false); /* unlight the button */
}

Categories

Resources