Android Radio Button Group with EditText imbedded - java

my problem is that I want a Radio Group that has 3 Radio Buttons, using the scheme below.
The three choices are:
1. [] Male
2. [] Female
3. [] Custom: (self-described identity)
However, the problem is that I want the user to type in their self-described identity into an EditText for me to retrieve.
So the following code is from my XML page, with some elements blocked out by "####".
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="####"
android:id="#+id/male_female_custom_choice"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_male"
android:checked="true" />
<RadioButton android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_female"
android:checked="false" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="####"
android:weightSum="1">
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_custom"
android:checked="false" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="####"
android:hint="####"
android:focusableInTouchMode="true"
android:gravity="center"
android:layout_weight="1.05"
android:textSize="14sp" />
<TextView
android:layout_width="42dp"
android:layout_height="43dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="####"
android:id="####"
android:singleLine="true"
android:gravity="center"
android:layout_marginLeft="0dp"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
As you can see, I have tried to use a LinearLayout to isolate the custom option.
However, there are unintended and undesired side effects.
1. The custom option can be selected in addition to the other 2 predefined genders.
2. The custom option cannot be selected on its own.
In the actual Java file for the activity, I have the following code:
// button, radio button, editText, and Spinner fields
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
// get selected radio button from RadioGroup
int selectedId = rSexGroup.getCheckedRadioButtonId();
// find radio button by returned id
rButton = (RadioButton)findViewById(selectedId);
// assign gender based on id of radio button
if (selectedId == 1) {
pat.gender = "male";
}
if (selectedId == 2) {
pat.gender = "female";
}
if (selectedId == 3) {
mEdit = (EditText)findViewById(R.id.####);
pat.gender = (mEdit.getText().toString());
}
Since I am a bit rusty with Java, it may be possible that I have some really newbish errors. Please advise.
Once again, I am looking for a way to get a set of 3 RadioButtons, each on an individual line, with the last RadioButton with an EditText adjacent to it from which I obtain the desired information.
EDIT: Here's a picture of what I want it to look like:
(http://i68.tinypic.com/ao2oow.png)
Unfortunately I need 10 reputation to post images. :(
Mohit's answer gives the EditText on a different line than the custom input.
(http://i63.tinypic.com/68u88x.png)
Please note that the orientation of the EditText is adjacent to the custom, and not below. I apologize for not clearly specifying enough what I wanted.

Because selectedId will not be 1,2 or 3....debug it you will get value..
The custom option cannot be selected on its own.
Remove your 3rd RadioButton from LinearLayout and replace below 2nd RadioButton and put your EditText and TextView inside LinearLayout..
On you listener get getCheckedRadioButtonId and getText() of that RadioButton and check it accordingly...
I dont no what is your task but here is how can get all three RadioButton working and get custom text too....
xml...
UPDATE
<RelativeLayout 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"
tools:context="com.ex.MainActivity" >
<RadioGroup
android:id="#+id/male_female_custom_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male" />
<RadioButton
android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="FeMale" />
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Custom" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/male_female_custom_choice"
android:layout_toRightOf="#+id/male_female_custom_choice"
android:orientation="horizontal"
android:weightSum="1" >
<EditText
android:id="#+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8"
android:ems="10"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="aa"
android:inputType="text"
android:textSize="14sp" />
<TextView
android:id="#+id/text"
android:layout_width="42dp"
android:layout_height="43dp"
android:layout_marginLeft="0dp"
android:layout_weight=".2"
android:gravity="center"
android:singleLine="true"
android:text="aaa"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</LinearLayout>
<Button
android:id="#+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/male_female_custom_choice"
android:text="Get" />
</RelativeLayout>
.java file..
public class MainActivity extends Activity {
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
public Button but;
public String str = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
but= (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = rSexGroup.getCheckedRadioButtonId();
rButton = (RadioButton)findViewById(selectedId);
if (rButton.getText().toString().equals("Male")) {
str = "Male";
}
if (rButton.getText().toString().equals("FeMale")) {
str = "FeMale";
}
if (rButton.getText().toString().equals("Custom")) {
mEdit = (EditText)findViewById(R.id.edit);
str = mEdit.getText().toString();
}
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
}
you can also set visibility of LinearLayout so that it only visible when custom in checked....
Hope it help..

put radio group in RelativeLayout
set third radiobutton text as empty/null
add EditText as layout_alignParentBottom="true"
now programmatically call EditText's onFocusChangeListener
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b)
thirdRadio.setCheched(true);
}
});

Related

getCheckedRadioButtonId() returns -1 even after the radio button is checked

I'm trying to get text from selected radiobutton. this code was working fine yesterday but now it's returning null and I don't understand why or what's suddenly causing it.
Renaming variable, rewriting the code, re define variable, re initializing it does nothing to solve this.
the XML
<RadioGroup
android:id="#+id/radioGroupMurid"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtKelaminMurid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Jenis Kelamin"
android:textSize="20sp" />
<RadioButton
android:id="#+id/radioMuridLaki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Laki-Laki" />
<RadioButton
android:id="#+id/radioMuridPerempuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Perempuan" />
</LinearLayout>
</RadioGroup>
Activity
private RadioGroup radioGroup;
protected void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
setContentView(R.layout.murid_detail_activity);
radioGroup = (RadioGroup) findViewById(R.id.radioGroupMurid);
getRadioChecked();
}
public void getRadioChecked(){
int sexMurid = radioGroup.getCheckedRadioButtonId();
RadioButton rbSelected = (RadioButton) findViewById(sexMurid);
jenisKelamin = rbSelected.getText().toString();
}
This is really fine yesterday, I just don't know what's causing it? can anyone help?
This occurs when RadioButton is not a direct child of RadioGroup. In your code the direct child of RadioGroup is LinearLayout. Try with the RadioGroup tag inside of LinearLayout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtKelaminMurid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Jenis Kelamin"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/radioGroupMurid"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="#+id/radioMuridLaki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Laki-Laki" />
<RadioButton
android:id="#+id/radioMuridPerempuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Perempuan" />
</RadioGroup>
</LinearLayout>
The return type of radioGroup.getCheckedRadioButtonId() (as in the docs) is an int. An int cannot be null, so I think you should rely on -1.
Edit: If you're not sure how an Android component works, just check the source code :) For the RadioGroup, see for example: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/RadioGroup.java#RadioGroup.clearCheck%28%29
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroupMurid);
RadioButton rbSelected = (RadioButton) findViewById(sexMurid);
RadioButton radioMuridPerempuan = (RadioButton) findViewById(radioMuridPerempuan);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.sexMurid) {
jenisKelamin = rbSelected.getText().toString();
else if (checkedId == R.id.radioMuridPerempuan) {
jenisKelamin = radioMuridPerempuan.getText().toString();
}
}
});

Keyboard next button when using include tag

I have seen/tried the following questions, but mine is not the same:
Android keyboard next button issue on EditText
Move to another EditText when Soft Keyboard Next is clicked on Android
First, I have a CardView wherein I have a EditText, like below:
<RelativeLayout
android:id="#+id/topCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#26c2ef">
</RelativeLayout>
<RelativeLayout
android:id="#+id/cardTopTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp">
<TextView
android:id="#+id/lessonTitleHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Please provide a name:"
android:textColor="#000000"
android:textSize="18sp" />
<ImageView
android:id="#+id/horiLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/lessonTitleHeading"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:background="#60000000" />
<EditText
android:id="#+id/noteEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/horiLine"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:hint="Enter name here..."
android:imeOptions="actionNext" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
I then inflate this layout in another by using include like this:
<include
android:id="#+id/lessonTitle"
layout="#layout/activity_notes_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
and in my Activity class I do the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_leason);
View tvOne = findViewById(R.id.tvOne);
View tvTwo = findViewById(R.id.tvTwo);
View tvThree = findViewById(R.id.tvThree);
View tvFour = findViewById(R.id.tvFour);
final TextView tvOne = (TextView)vLessonTitle.findViewById(R.id.lessonTitleHeading);
final EditText etOne = (EditText) vLessonTitle.findViewById(R.id.noteEditText);
TextView tvTwo = (TextView)vlessonProbStu.findViewById(R.id.lessonTitleHeading);
final EditText etTwo = (EditText) vlessonProbStu.findViewById(R.id.noteEditText);
TextView tvThree = (TextView)vLessonWorkedOn.findViewById(R.id.lessonTitleHeading);
final EditText etThree = (EditText) vLessonWorkedOn.findViewById(R.id.noteEditText);
TextView tvFour = (TextView)vlessonWhatStudShouldWorkOn.findViewById(R.id.lessonTitleHeading);
final EditText etFour = (EditText)vlessonWhatStudShouldWorkOn.findViewById(R.id.noteEditText);
tvOne.setText("This is the first CardView:");
etOne.setHint("Enter Title Here...");
tvTwo.setText("This is the second CardView:");
etTwo.setHint("Enter Text Here...");
tvThree.setText("This is the third CardView:");
etThree.setHint("Enter Text Here...");
tvFour.setText("This is the fourth CardView:");
etFour.setHint("Enter Text Here...");
//I tried this, but it didn't work...
etOne.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
etLessonTitle.clearFocus();
etLessonProbStu.requestFocus();
return true;
}
return false;
}
});
}
The Layout now looks like this:
At this point I have a bunch of CardViews with EditTexts underneath each other like above. I would like to use the next button in the keyboard to go to the next CardsViews EditText.
If you have a look at my Activity class above, I tried using setOnKeyListener and clearFocus from the current EditText then requestFocus on the next EditText, but that didn't work.
Any ideas how I can achieve this?
Try this you need to set android:inputType="" to your Editext with android:imeOptions="actionNext"
<EditText
android:id="#+id/noteEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/horiLine"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:hint="Enter name here..."
android:inputType="text" // specifies your input type here
android:imeOptions="actionNext" />

How to get values of EditText And RadioButton and send it via HTTP Post

I am new in Android programming. Could any tell me way to solve the following issue?
I make a XML file with following codes:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_splash_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
tools:context="com.royalrandhawa.xemp.SplashScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="150dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true>
<EditText
android:id="#+id/mainSearch"
android:layout_width="300dp"
android:textColor="#373737"
android:textColorHint="#bfbfbf"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="This is your field...."
android:background="#drawable/mainsearch"
/>
<Button
android:text="Surf Now!"
android:id="#+id/surfNowBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="send"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:text="Filter Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/filtersearch"
android:layout_weight="1"
android:checked="false"/>
<RadioButton
android:text="Trace my activities"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/traceme"
android:layout_weight="1" />
</RadioGroup>
</LinearLayout>
</RelativeLayout>
Layout details are:
user enter his query in EditText and there is some other option like Filter Search and Trace Activities in RadioButtons.
I want these:
After Click on Search button, get value of EditText and check if either or both radiobuttons are selected then make two variable with fsVr and tmVr and set there values to true. and start new activity name Result
After that send these values to a URL with HTTP POST method.
and show the server response in TextView in Result (Activity)
Thank You!
First your xml is wrong. It should be like that
You need to create two different RadioGroup and both have only one RadioButton because of your requirement is either or both radiobuttons are selected
And java code is here
private void initview1() {
RadioButton filtersearch = (RadioButton) findViewById(R.id.filtersearch);
RadioButton traceme = (RadioButton) findViewById(R.id.traceme);
Button Search_Buttom = (Button) findViewById(R.id.Search_Buttom);
EditText mainSearch = (EditText) findViewById(R.id.mainSearch);
Search_Buttom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Boolean fsVr = false, tmVr = false;
String search_data = mainSearch.getText().toString();
fsVr = filtersearch.isChecked();
tmVr = traceme.isChecked();
// go to your new activity with required data..
}
});
}

Button View is not Visible even after setting its visibility true

I am making a quiz app hence I wanted my MenuButton and NextButton view Invisible at the beginning but after satisfying a particular condition I wanted the View to be visible again hence I used findViewById(R.id.MenuButton).setVisibility(View.VISIBLE); and same for the NextButton but still these view were Invisible.What am I doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_gravity="center">
<TextView
android:id="#+id/Question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="TextView"
android:gravity="center"
android:textSize="25dp"
android:textColor="#000000"/>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:text="RadioButton1"
android:textSize="20sp"
android:textColor="#000000"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:layout_marginTop="54dp"
android:layout_below="#+id/Question"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="On_RadioButton1_Click"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="horizontal"
android:layout_marginTop="55dp"
android:layout_below="#+id/radioButton4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:id="#+id/MenuButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:text="menu" />
<Button
android:id="#+id/NextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginLeft="125dp"
android:text="next" />
</LinearLayout>
</RelativeLayout>
//Java file
private int Question_no=0;
private Boolean Boolean_Var=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
String[] Question_Array = getResources().getStringArray(R.array.Question1);
TextView Questions = (TextView) findViewById(R.id.Question);
Questions.setText(Question_Array[Question_no]);
String[] Radio_Button1_Array = getResources().getStringArray(R.array.Option_1);
RadioButton Radio_Button1 = (RadioButton) findViewById(R.id.radioButton1);
Radio_Button1.setText(Radio_Button1_Array[Question_no]);
findViewById(R.id.MenuButton).setVisibility(View.INVISIBLE);
findViewById(R.id.NextButton).setVisibility(View.INVISIBLE);
}
public void On_RadioButton1_Click(View view)
{
if(Boolean_Var==false)
{
String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1);
String CorrectAns = CorrectAns_Array[Question_no];
String[] Answer_Array = getResources().getStringArray(R.array.Option_1);
String Answer = Answer_Array[Question_no];
if(Answer.equals(CorrectAns))
{
RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton1);
Right_Ans.setTextColor(Color.GREEN);
AnswerSubmitted();
}
else
{
RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton1);
Wrong_Ans.setTextColor(Color.RED);
GreenTick();
AnswerSubmitted();
}
}
Boolean_Var=true;
}
public void AnswerSubmitted()
{
findViewById(R.id.MenuButton).setVisibility(View.VISIBLE);
findViewById(R.id.NextButton).setVisibility(View.VISIBLE);
}
You should get a reference to your buttons and call setVisibility on the reference. What you're currently doing is setting visibility INVISIBLE on one reference and setting it VISIBLE on another.
Button nextButton;
//in onCreate
nextButton = (Button)findViewById(R.id.nextButton);
nextButton.setVisibility(View.INVISIBLE);
//in AnswerSubmitted
nextButton.setVisibility(View.VISIBLE);
I think you should tell android that you want to execute that method when your radiobutton is clicked. by adding :
Radio_Button1.setOnClickListener(new OnClickListener (){
public void onClick(View v) {
On_RadioButton1_Click(v);
}
});
So you can get that method executed everytime you click.
Add it at the end of OnCreate

Cannot resolve method getText for a RadioGroup

I am trying to have the user fill out a form then those fields auto-populate into an email. I was able to convert the EditText to strings as well as the spinner, but I am confused as to how to do it with RadioGroup. I want the text from the button which is selected to appear in the email. I know that getText won't work, but what will. Thank you
JAVA
//This happens when you press the submit button at the bottom of the form.
public void submit(View button) {
// Do click handling here
//What happens here is the input from each field is taken in and converted into
//Strings and placed into their correct variables.
final EditText FirstNameField = (EditText) findViewById(R.id.EditTextFirstName);
fName = FirstNameField.getText().toString();
final EditText LastNameField = (EditText) findViewById(R.id.EditTextLastName);
lName = LastNameField.getText().toString();
final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
email = emailField.getText().toString();
final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
feedback = feedbackField.getText().toString();
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
feedbackType = feedbackSpinner.getSelectedItem().toString();
final RadioGroup ApproveorReject = (RadioGroup) findViewById(R.id.radiochoice);
fAnswer = ApproveorReject.getText().toString();
//calls the sendEmail() from below to pull up a list of apps installed
//on the phone that can handle emailing.
sendEmail();
}
//This bit of code pulls up a list of apps that can handle emailing.
private void sendEmail() {
//When the user chooses an app of the list that pops up, these lines below
//will auto-populate the fields of the email with the input from above.
//The user can take one last look at the email before hitting that send button
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, feedbackType);
i.putExtra(Intent.EXTRA_TEXT, lName + ", " + fName + "\n" + email + "\n" + feedback + fAnswer );
startActivity(Intent.createChooser(i, "Send mail..."));
}
}
XML
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/feedbacktitle"
android:textSize="10pt">
</TextView>
<!--First Name-->
<EditText
android:id="#+id/EditTextFirstName"
android:layout_height="wrap_content"
android:hint="#string/feedbackfirst"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:layout_below="#+id/TextViewTitle">
</EditText>
<!--Last Name-->
<EditText
android:id="#+id/EditTextLastName"
android:layout_height="wrap_content"
android:hint="#string/feedbacklast"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:layout_below="#id/EditTextFirstName">
</EditText>
<!-- Email -->
<EditText
android:id="#+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="#string/feedbackemail"
android:inputType="textEmailAddress"
android:layout_width="fill_parent"
android:layout_below="#id/EditTextLastName">
</EditText>
<Spinner
android:id="#+id/SpinnerFeedbackType"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:entries="#array/Types_of_Errors"
android:layout_below="#+id/EditTextEmail">
</Spinner>
<EditText
android:id="#+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="#string/feedbackbody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent"
android:layout_below="#+id/SpinnerFeedbackType">
</EditText>
<RadioGroup
android:id="#+id/radiochoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/EditTextFeedbackBody"
android:orientation="horizontal">
<RadioButton
android:id="#+id/AcceptRadio"
android:layout_width="wrap_content"
android:layout_marginStart="50dp"
android:layout_height="wrap_content"
android:text="#string/acceptbutton" />
<RadioButton
android:id="#+id/RejectRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rejectbutton"
android:layout_marginStart="115dp" />
</RadioGroup>
<EditText
android:id="#+id/RejectResponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/rejectreason"
android:layout_marginTop="410dp"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:inputType="text"/>
<Button
android:id="#+id/ButtonSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sendform"
android:layout_centerHorizontal="true"
android:layout_marginTop="590dp"
android:onClick="submit"/>
</RelativeLayout>
Try this
RadioButton btn=(RadioButton)findViewById(ApproveorReject.getCheckedRadioButtonId());
String selectedText=btn.getText().toString();
Try
ApproveorReject.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb=(RadioButton)findViewById(checkedId);
String param = rb.getText();
}
});

Categories