I need to implement a multiple choice question. I have to add radio buttons and text view for multiple choice. I need to implement it dynamically corresponding to the number of choices. Can someone help me
for (Answer answer : answers) {
LinearLayout linearLayoutRw2 = new LinearLayout(this);
linearLayoutRw2
.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
RadioGroup radioGroup = new RadioGroup(
ShowQuestionsActivity.this);
linearLayoutRw2.addView(radioGroup);
RadioButton rb = new RadioButton(ShowQuestionsActivity.this);
radioGroup.addView(rb);
TextView ansText = new TextView(ShowQuestionsActivity.this);
ansText.setText(Html.fromHtml(answer.getAnswerText()));
linearLayoutRw2.addView(ansText);
linearLayoutShowQues.addView(linearLayoutRw2);
}
Try Like this,
First create a ListView dynamically.
ListView choicelist = new ListView(this);
choicelist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
choicelist.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
android.R.id.text1, your_answer_list));
choicelist.setSelector(new ColorDrawable(0x0));
choicelist.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
choicelist.setCacheColorHint(0);
choicelist.setVerticalFadingEdgeEnabled(false);
And Finally add this List to your Layout
linearLayoutShowQues.addView(choicelist);
For Customisation you can use Custom Adapter.
Hope this will help you.
Related
I am trying to shuffle radio button in android, here's is the code what i have done till now, but as I proceed further I am not getting what should be the code or how to do that.
Please correct if I am wrong, or doing some silly mistake as I am new to it.
ArrayList<RadioButton> arrayText = new ArrayList<>();
arrayText.add(ropt0);
arrayText.add(ropt1);
arrayText.add(ropt2);
arrayText.add(ranswer);
Collections.shuffle(arrayText);
I am not getting what next after Collections.shuffle(arrayText);
should be the code
Please suggest
I think you don't quite understand what are you doing and what do you want. In your code you just adding all your RadioButtons to ArrayList and then shuffle this list. This is same as:
ArrayList<Stirng> list = new ArrayList<String>();
list.add("a1");
list.add("a2");
Collections.shuffle(list);
You see, this isn't connected to UI in any way. If you want to shuffle buttons in UI (so user will see it) you have 2 approaches:
Create and add your radio buttons in the code. Define some RadioGroup in view XML file and then create and add your RadioButtons
RadioGroup lay = (RadioGroup) findViewById(R.id.my_radio_group);
List<RadioButton> buttons = new ArrayList<>();
RadioButton rb1 = new RadioButton(this);
RadioButton rb2 = new RadioButton(this);
buttons.add(rb1);
buttons.add(rb2);
Collections.shuffle(buttons);
for(RadioButton rb:
buttons){
lay.addView(rb,new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
Shuffle just a right button position. Create N buttons in XML. And then set all texts in your code, so you can decide which button is the right one.
List<String> answers = new ArrayList<>(4); // 4 - button number
//Fill the list here
int rightAnswerPosition = ...; //Decide it when you fill your list
rb1.setText(answers.get(0));
//.. and so on
I have some code like this:
l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(l);
TextView ch = new TextView(this);
Button ndda = new Button(this);
ndda.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button nddb = new Button(this);
Button nddc = new Button(this);
Button nddd = new Button(this);
ch.setText("ndch");
ndda.setText("da");
nddb.setText("db");
nddc.setText("dc");
nddd.setText("dd");
l.addView(ch);
l.addView(ndda);
l.addView(nddb);
l.addView(nddc);
l.addView(nddd);
now i want to add lot of linear layout( may be 15) in to scroll view. How can I do that with shortly code? and the Button in each linear layout i want to setOnClickListener on them to do some thing. I try to do this with listview but it's alway refresh when scroll, i can't disable refresh. I'm a newbie so pls show me detail. Thank for all
Some guys already mentioned it before in the comments... use a ListView or even better ... a RecyclerView.
Here's a good tutorial for the RecylcerView.
Nevertheless a ScrollViewcan have only 1 child (in your case a LinearLayout), which again can contain multiple layouts within.
i have a problem with my application. Basically what it does, is when i click a button, it creates 3 fields (2 editText and 1 spinner) on a scrollView. The thing works well, the only problem that im having, is related with the style, the activity bgColor is white(as the rest of the app) but, when i create elements programmatically, these elements doesnt have the look of the rest of my app. The editTexts are white with white letters (impossible to read since my bgColor is white as well) and its the same thing with the spinner. What can i do? Here is a snipet of code so you can see what im doing here.
public class AddIngredients extends Activity {
public int Count = 0;
public String[] spinnerArray = {"Gr", "kg", "Cups", "ml", "L", "oz"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addingredients);
final TableLayout lm = (TableLayout) findViewById(R.id.TableMain);
TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button addMore = (Button)findViewById(R.id.addmore);
addMore.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TableRow ll = new TableRow(getApplicationContext());
//ll.setOrientation(LinearLayout.HORIZONTAL);
EditText product = new EditText(getApplicationContext());
product.setHint(" Ingredient "+Count +" ");
// Create Button
EditText amount = new EditText(getApplicationContext());
// Give button an ID
amount.setId(Count);
amount.setHint("Quantity");
final Button btn2 = new Button(getApplicationContext());
btn2.setId(Count);
btn2.setText("Remove " + Count);
Spinner spinner = new Spinner(getApplicationContext());
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
ll.addView(product);
ll.addView(amount);
ll.addView(spinner);
lm.addView(ll);
Count = Count + 1;
I know my XML is working well because if i create the 3 views on my xml, they look great. PD: Thx in advance for any help! Greetings.
you can use
amount.setTextColor(Color.BLACK);
to set colour of text to black or any other colour
same can be used for spinner
Here's how I set the colors of my edit text lines and other theme-related android views.
http://android-holo-colors.com/
I just picked the color and views I wanted, then unzipped them in my res folder, then set the theme according the android tutorials.
I recommend backing up your res folder first, in case you don't like the results.
Garret
I had a error in my code. When creating the fields, i was using
(getApplicationContext());. I fixed it using MyApplicationName.this.
This is the layout I want to create but dynamically through java code.
Here parent layout is LinearLayout.
The tableLayout created through java code consists of 5 radio buttons which are created dynamically.
I need to place these radio button in radio group.
How to do that.
So far i tried this which is working fine. But am not able to add all the 5 radio buttons in 1 radio group.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (LinearLayout) findViewById(R.id.mainl); //parent layout
t1 = new TableLayout(this);
rb1 = new RadioButton(this);
rb2 = new RadioButton(this);
rb3 = new RadioButton(this);
t1.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(this);
TableRow row1 = new TableRow(this);
row.addView(rb1);
row.addView(rb2);
row1.addView(rb3);
t1.addView(row);
t1.addView(row1);
l.addView(t1);
}
I can't find you'r declaration of a RadioGroup.
You need something like this:
...
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
rg.addView(rb1);
rg.addView(rb2);
rg.addView(rb3);
row.addView(rg);
...
RadioGroup inherits from LinearLayout. You'll have to create your own implementation of RadioGroup that inherits from TableLayout.
If you examine the source code of RadioGroup, you'll see it just keep track of its child views which are RadioButton's and makes sure only one of them is checked. Apart from that it provides a listener if checked RadioButton changes.
You can try modifying the source to make it extend from TableLayout etc.
I am using RadioGroup, added RadioButton rdbut to RadioGroup rdgrp like rdgrp.addView(rdbut).
for(int j=0;j<3;j++)
{
RadioGroup rdgrp = new RadioGroup;
for(int i=0;i<=10;i++)
{
RadioButton rdbut = new RadioButton(this);
rdbut.setText("RadioButtion"+i);
rdbut.setId(i);
rdbut.setTag("somename");
rdgrp.addView(rdbut);
}
}
the above code shows how I initialize the radiogroup and radio button. after I run the this code, in emulator/mobile , i am able to check 2 radio buttons at a time.
What could be the problem?
Change your code like this.
RadioGroup rdgrp[] = new RadioGroup[3];
For(int j=0;j<3;j++)
{
RadioButton rdbut[] = new RadioButton[10];
For(int i=0;i<=10;i++)
{
rdbut[i].setText("RadioButtion"+i);
rdbut[i].setId(j*100+i);
rdbut[i].setTag("somename");
rdgrp[j].addView(rdbut[i]);
}
}
You have created three different Radio Group. You can select only one radio button from a single group.So from three groups you can select three buttons But there is no inter-group relationship. You can select radio buttons from different group simultaneously. In your case you can select three buttons at max.
Thanks
Sunil
Use Some thing like this xml design in user layout file.
<TableLayout
android:id="#+id/tbl_layoutchoice"
style="#style/InfoTableView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip" >
<RadioGroup
android:id="#+id/SelectLayout_Group"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</RadioGroup>
</TableLayout>
and For Use this RadioGroup in Activity 's OnCreate() Method and findView like this
mRadioGroup = (RadioGroup) this.findViewById(R.id.SelectLayout_Group);
and Then Use Below Code With Your Require Change To Add Radio Button in One RadioGroup.Use Also Below Require Declaration for Create Radio Button Dynamically.
ArrayList<String> layoutlist = new ArrayList<String>(3);
int index = -1;
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
for (String layout : layoutlist) {
RadioButton r = new RadioButton(this);
index++;
r.setText(layout);
r.setId(index);
r.setLayoutParams(lp);
r.setTextAppearance(this, R.style.TextBase);
mRadioGroup.addView(r);
}
So don't forget to add your String Value in layoutlist before for loop .and R.style is some Require Style for Text Show in RadioButton.