How to get the checkboxes dynamically in android? - android

In my project I need to display questions and its options. Here the options are at max 20 with multiple answers. The options are not fixed for each question. That means for each question the options may be 2 or 3 or 6 or 18 or 2o. Since the question contains multiple answers I need to create checkboxes to display options dynamically. Based on the number of options for the question we need to display the checkboxes. How can I do that? Please help me regarding this.
Thanks in Advance

You can create an empty LinearLayout and call its addView() function
to add the checkboxes dynamically in your code.
For example,
CheckBox[] cbs = new CheckBox[20]; // Number varies..
for(int i=0; i<20; i++){
cbs[i] = new CheckBox(this);
ll.addView(cb);
cbs.setText("Test");
}
Look at this turorial Android, Part III: Dynamic Layouts
EDIT:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
CheckBox[] cbs = new CheckBox[20];
for(x=1; x<numberofoptions; x++)
{
cbs[x] = new CheckBox(getContext());
ll.addView(cbs[x]);
}

Related

How can i write these items for constructor in a for-loop?

so I'm currently writing an app for android, and im still a noob in java/android.
Anyways i have this 2 strings, one with names and the other with emails, and i want to output them in a listview with a custom adapter.
It works fine so far but i dont know how to set the items dynamically (with a for loop).
To create the adapter and so on, I used this tutorial:
http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
I simply changed the ImageView to a second TextView.
In the tutorials code there are 5 items added to the list, but i need them dynamically, since Im not always having the same amount of name+emails to output
I already tried putting it in a for-loop by doing:
Weather weather_data[] = new Weather[names.length];
for(int z=0; z == names.length){
Weather[z]={new Weather(names[z], emails[z])};
}
I also tried it with adding "new" infront and trying to set everything null before, basically trial&error since i dont know much about it.
So can anyone tell me how I add the items dynamically?
(Ps: sorry if I used wrong names to describe anything)
This should work
Weather weather_data[] = new Weather[names.length];
for(int z=0; z < names.length; z++){
weather_data[z] = new Weather(names[z], emails[z]);
}
Give this a read to learn how for loops work
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
and this one for arrays
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
try this
ArrayList<Weather> weatherData = new ArrayList<>();
for (int i=0; i < names.length(); i++){
weatherData.add(new Weather(names[i], emails[i]));
}
Then when you need it as a Weather[] use weatherData.toArray()

Is a programmatically created View assigned an Integer ID in R.java?

Suppose I create an EditText using the following code and add it to a programmatically created LinearLayout, will it get assigned some ID or do I need to manually assign one using setId()?
I ask this question because there is no chance of Android assigning the same id to two different views whereas if we do it ourselves, something like that might happen.
LayoutParams fparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5.0f);
LayoutParams tvparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams btparams = new LayoutParams(25, 25);
first.setLayoutParams(fparams);
first.setOrientation(LinearLayout.HORIZONTAL);
EditText tv = new EditText(this);
tvparams.weight = 4.97f;
tv.setLayoutParams(tvparams);
tv.setHint("Destination Address / Postcode");
Button bt = new Button(this);
bt.setBackground(getResources().getDrawable(R.drawable.minus));
bt.setTextColor(Color.parseColor("#ffffff"));
bt.setTextSize(12f);
btparams.weight = 0.03f;
bt.setLayoutParams(btparams);
first.addView(bt);
first.addView(tv);
main.addView(first);
There is a constant for views to mark them with no id View.NO_ID.
I'm not sure what you want to achieve, but new views get assigned the id View.NO_ID.
However if you want to generate IDs you can use View.generateViewId().
Edit:
Based on your comment I'm editing my answer. View.generateViewId() does not exists below API level 17, this question "how to avoid ID conflicts?" contains an answer with code to generate ids below API 17.

How to dynamically add fields in table in Android?

I need the user to enter graph coordinates. Problem is, I don't know how many. So I want to have an "Add Point" button which inserts two fields (for x and y coordinates) into a new table row for the user to add more coordinates.
Also, how do I identify these new fields when I want to get data from them? Normally, I already know the ID of the field and call them using findViewById(R.id.ID_here); Now what do I do to identify them?
I'm writing all these coordinates into a file, so if there's a way to write them without identifying each one, please help.
EDIT:
I can't get the edittext fields to have these layout parameter properties:
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
Here's my JAVA code for the same:
TableLayout table = (TableLayout) findViewById(R.id.TableLayout1);
TableRow tr = new TableRow(this);
LinearLayout.LayoutParams trparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(trparams);
cg[i] = new EditText(this);
weight[i] = new EditText(this);
LinearLayout.LayoutParams fieldparams = new LinearLayout.LayoutParams(100, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
cg[i].setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
weight[i].setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
cg[i].setLayoutParams(fieldparams);
weight[i].setLayoutParams(fieldparams);
tr.addView(cg[i]);
tr.addView(weight[i]);
table.addView(tr);
Please help if you can.
You can create new rows (or any other kind of View) like this:
TableRow tr = new TableRow(myContext); // usually myContext is 'this'
you then add the tr to whatever the parent view is
TableLayout myTable = findViewById(R.id.TableLayout1);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
myTable.addView(tr);
You'll need to add layoutParams to the view before you add it since all Views have to, at minimum, specify their layout width and layout height.
If you need to add children to your row (obviously you will, what use is it otherwise) you just repeat the process except now you create an EditText. Once you do, you automatically have a reference to it, since you created it! :)
I'm not clear on what you mean by the last part of your question, where you're writing them to a file. Please elaborate.
Oh, and welcome to Stack... if you find answers useful, don't forget to up-vote them and/or mark them as correct.

numbering the textviews

I am new in android programming.
I am creating a app where I supposed to use multiple textviews. Number of TextViews go on changing according to number of records fetched from database.
Here, i was trying to number textviews as textView1, textView2, textView3,...
but as i am unaware about the number of records, i can not define them statically
is there any way to do so dynamically
like we do in PHP
e.g
$count = 1;
if(condition)
{
textView.$count;
$count++;
}
Thanx in advance.
You should create a List of TextViews and populate it dynamically
List<TextView> list = new LinkedList<TextView>();
Find the total number of record at runtime and create the TextView in a loop for each textView and finally ad it to the current layout.
For example:
for(int i=0;i<data.size;i++){
TextView tv=new TextView(this);
tv.setText(data.get(i));
currentLayput.addView(tv);
}
where data is some vector or Arraylist in which you can store the data from database.

Display 20 random images over UI screen at a time in Android

HI,
I want to display 20 random images at a time over Activity screen in android. I'm using this snippet:-
ImageView imageArr[] = new ImageView[25];
int id[]={ R.drawable.letter1,R.drawable.letter2,R.drawable.letter3,R.drawable.letter4,
R.drawable.letter5,R.drawable.letter6,R.drawable.letter7,R.drawable.letter8,
R.drawable.letter9,R.drawable.letter10,R.drawable.letter11,R.drawable.letter12,
R.drawable.letter13,R.drawable.letter14,R.drawable.letter15,R.drawable.letter16,
R.drawable.letter17,R.drawable.letter18,R.drawable.letter19,R.drawable.letter20};
ArrayList<Integer> randomArr = new ArrayList<Integer>();
for(int i=0; i<20;i++) {
randomArr.add(i);
}
Collections.shuffle(randomArr);
for(int i=0; i<20; i++){
//ImageView mImageplay = (ImageView)findViewById(R.id.image_play);
imageArr[i]=(ImageView)findViewById(id[randomArr.get(i)]);
//mImageplay.setImageResource(id[randomArr.get(i)]);
}
Here I'm having 20 custom images of size 49x49 pixels(small sizes)in drawable folder which i want to dispaly over UI at a time in random fashion with no alignment.
The problem is in imageArr[i] is showing NULL value for all the 20 images,whereas "randomArr" is having the correct shuffled data.
Also, Is there any way to display it in randomized look in Layout Area as i was not able to find out solution for this.
I am struck in this and not able to resolve it. please help me out for this.
thanks in advance.
-pk
Your array id[] contains R.drawable. values (id of Drawables in your app) instead of R.id. (id of your ImageViews in your layout).
Set the android:id attributes in your layout, and use them in your id[] array.

Categories

Resources