How can I place an EditText and TextView in a RadioButton? - java

I have a RadioGroup inside of a RelativeLayout. I've got a few RadioButtons for different options, like "Bob", "Joe", and "Fred". However, I need to add an "Other..." option with an EditText right next to a RadioButton in case a user wants to enter "Steve". Is there a way to do this?

Just one idea that comes to mind is to use android:layout_marginLeft and android:layout_marginTop tags to position the EditText field where you need it to be. Be sure to use dip to support multiple densities across devices.
<EditText
android:layout_marginLeft="65dip"
android:layout_marginTop="100dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="" />
Then, if the user selects your Other radio button, just grab the value from EditText.

Related

Can't focus on EditText after setting focus to true

Im having trouble undestanding what is happening here.
If I don't mess with "setFocusable" on EditText it works like I excpect (when you click on it the keyboard pops up and you can edit it as normal), if I set it to true it also works like before:
holder.editTextExercise.setFocusable(true);
But if I set it to false, and than back to true, I can no longer see a keyboard pop up, so I can't edit it
holder.editTextExercise.setFocusable(false);
holder.editTextExercise.setFocusable(true);
Here is the EditText
<EditText
android:id="#+id/recyclerExercise"
android:layout_width="0sp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#color/grey"
android:backgroundTint="#color/grey"
android:text="#string/app_name"
android:textSize="25sp"/>
Im putting different items with EditTexts in the recycler and sometimes I want to change them so they can/can't be editable.
What is going on here?
Instead of
holder.editTextExercise.setFocusable(false);
holder.editTextExercise.setFocusable(true);
Try
holder.editTextExercise.setEnabled(false);
holder.editTextExercise.setEnabled(true);

Cannot get editText to do a line break from enter (android)

So i have multiple edittext's in one constraint layout and i cannot get my multiline edittext to do a linebreak from enter, i have tried settings imeOptions to normal, actionUnspecified, actionNone and actionDone and no luck, it does not do a line break. Could not find anyone with this issue, only opposite =)
What am i doing wrong? here is the code for my edittext
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text|textImeMultiLine|textCapSentences"
android:ems="10"
android:singleLine="false"/>
have you tried android:maxLines, like android:maxLines="10"?

Android radio button style quiz, add value to correct/incorrect answers?

I'm working on a questions/answers app in android in the style of a quiz, and I haven't been able to come up with a method to properly implement a value to radio button. The value is determined by whether the answer is correct or wrong.
I feel like there's a few ways to go about it, but I'm not sure which ones might cause issues, or are simply bad programming practices. My issue comes from the fact that the questions and answers are randomized every time the activity is restarted. If it wasn't random, and I knew which answer would be correct, I think it'd be easier, but I want to go with a random feature.
To help clarify, I have radio buttons in an XML:
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioAnswerOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/radioAnswerTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/radioAnswerThree"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/radioAnswerFour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RadioGroup>
And I have the strings for the radio buttons in the .java file:
TextView q1A1 = (TextView) findViewById(R.id.radioAnswerOne);
q1A1.setText("This ANSWER is " + global.getA(0, 0));
TextView q1A2 = (TextView) findViewById(R.id.radioAnswerTwo);
q1A2.setText("This ANSWER is " + global.getA(0, 1));
TextView q1A3 = (TextView) findViewById(R.id.radioAnswerThree);
q1A3.setText("This ANSWER is " + global.getA(0, 2));
TextView q1A4 = (TextView) findViewById(R.id.radioAnswerFour);
q1A4.setText("This ANSWER is " + global.getA(0, 3));
The strings are pulled from a class. Each question has a class. That class has a question string, and an array of 4 answer classes. The answer class has a string and a status (true/correct or false/incorrect). I'm looking to implement the status portion now into the radio buttons to determine which one is correct so that if the user selects that radio button and clicks a button (to go to the next question), then the grade is calculated/adjusted accordingly.
I'm curious in how to do this. Seeing as the questions and answers are randomized everytime, I'll need to implement a function to get the status of each question and place that status as some sort of variable attached to the button. And whichever button is selected, that variable gets pulled and is added to an integer of "questions answered correctly" and the app continues. But I'm not sure of the java implementation to do such a thing. Should my radio buttons be in the Java file instead of XML to work best? Don't know the truly best way to do it (if there is one).

Android - Hidden but Select-able EditText

I would like to have a EditText in my Android app that you can not see, however you can select and type text into. In other words I would like it so that it can not be seen at all however other than that it behaves completely normally.
Another option is to have an image or button on the screen which the user presses to enter text into the EditText which is hidden behind another EditText.
Thanks for the help, however it is looking more likely that I will have to use the second option. To elaborate on it, for example I have a TextView that says 'Welcome'. I would like the user to be able to click on this text to bring up the keyboard and edit in the EditText field. The reason for having the EditText field hidden behind another is to cover up the cursor while making it seem the user is typing the text which appears on the screen.
If you would like to make the EditText fully invisible (even what the user types in) , but still be able to retrieve the entered data through myEditText.getText().toString(),
you could add:
android:textColor="#android:color/transparent"
android:background="#00000000"
android:cursorVisible="false"
if I'm getting you correctly. Here is how you can accomplish the first option of yours by setting the background to transparent and cursor visibility to false.
To check this you have to click at the center of screen.
e.g. code snippet:
<EditText
android:id="#+id/eT1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#00000000"
android:cursorVisible="false"
android:ems="10" >
</EditText>
Set transparent Background to EditText.
android:background="#00000000"
Sorry but I didn't understand your another question.

Creating a button from an event for android

I have looked for ways to make a button/layoutchange that appears depending on the choice made by the user. It is a dice simulator so if you pick you need a 4 sided die or 6 sided die a different look will pop out. I am looking to get some directions. Should I premake them in html or java? Or should I use another approach? The decision of how many sides gets asked through an alertdialog. Thank you for any help.
If you want to change layout dynamically you can use generic View to hold the space and inflate appropriate layout into them.
http://developer.android.com/reference/android/view/LayoutInflater.html
You can place the button, that should appear in your XML-layout-file and set its visibility attribute to gone. Like so:
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:visibility="gone"
/>
Then you could change the visibility programmatically (for example in the OnTouchListener of another button or so).
//retrieve the button created in the xml-layout by its id
mButton= (Button) findViewById(R.id.button_1);
//change its visibility
mButton.setVisibility(View.VISIBLE);
mButton.setVisibility(View.GONE);
I hope, I could help you.
Best regards,
G_J

Categories