Issue with dynamically added android radiobutton - java

I have an android app which asks a question followed by x number of options. Each option contains a textview, ImageView and a radio button.
The value of x (i.e. the number of options) is not constant. I want to dynamically add UI content to satisfy this requirement.
This I am doing using the following code
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayoutThis = (LinearLayout)findViewById(R.id.lladrList);
for (int i =0 ;i< options.length; i++){
LinearLayout lladroption=(LinearLayout)inflater.inflate(R.layout.adroption, null);
//get view id and set values
ImageView iv = (ImageView) lladroption.findViewById(R.id.ivadroption);
iv.setId(OPTIONIMAGE+i);
iv.setImageBitmap(downloadFile(options[i].getOptionData().getDataURL()));
((ViewGroup) iv.getParent()).removeView(iv);
iv.setOnClickListener(this);
linearLayoutThis.addView(iv);
RadioButton rb = (RadioButton) lladroption.findViewById(R.id.rbadroption);
rb.setOnClickListener(this);
rb.setId(OPTIONRADIOBUTTON+i);
rb.setText(options[i].getOptionString());
((ViewGroup) rb.getParent()).removeView(rb);
linearLayoutThis.addView(rb);
// rg.addView(rb);
}
I want the radiobuttons to belong to a particular radiogroup. But if I do a
rg.addView(rb) instead of linearLayoutThis.addView(rb); in the above code where rg is the radiogroup view ... all the radiobuttons are display in one go. Instead of displaying text,image,radiobutton,text,image,radiobutton,text,image,radiobutton I get text,image,text,image,text,image,radiobutton,radiobutton,radiobutton.
Does anyone know how to resolve this?

You dont have to add those dynamically. create a view group or layout with the textview image and the radio button you require. use listview to get the x number of options. If this is your requirment you can reply so that i can help you with it.

Related

How to overwrite previous TextView and EditText in android

I have a spinner with the values = {4,5,6,7} , initially spinner selected value is set to 4 , therefore 4 TextViews and 4 EditTexts showing in the activity.
Now I want if the user select value 5 from the spinner then these previous 4 TextViews and EditTexts will overwrite with new 5 TextViews and 5 EditTexts.
How can I do so ? any help?
For this you can manage through loop like...
for( int i = 0; i < spinner.getSelectedItem(); i++)
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}
For create view one after another create programmatically layout like this:
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL)
Like this you can manage programmatically as per your need.
You can write your TextView and EditText as an item of RecyclerView and every time you can have as much items as you want.
OR
every time add TextView and EditText programmatically from the java code.

Hindi kavita proper alignment/justification

I am developing an app related to hindi kavita(poems). I want the poems to be displayed in the way real poems are displayed like the image shown below
Now the problem is I dont know how to use a textview to show this kind of text
Use a linear layout with vertical orientation. Add a text view for each line, with layout_width="match_parent" and the appropriate gravity attribute.
https://developer.android.com/reference/android/widget/LinearLayout.html
EDIT:
If you have your poem as an arraylist of strings where each element is a line of your poem, you can do:
//Initialise your layout in your activity onCreate()
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);
layout.setOrientation(LinearLayout.VERTICAL);
//Start listening to your firebase data and put this somewhere in the callback:
// poemLines is a list of Strings you get from firebase
for(i=0; i<poemLines.size(); i++){
TextView view = new TextView(context);
view.setText(poemLines.get(i));
//set any other attributes to your textview that you want, width, height, font, etc
view.setGravity(i%2==0?END:START);
layout.add(view);
}

Adding ImageView to the left of the radio group?

I'm trying to add an ImageView to a RadioGroup. Preferably as part of the RadioGroup but to the left of all of the RadioButtons that are inside the group. I can make it appear on the bottom by simply adding it as the last item, but I would like to get it to appear to the left.
See my code and a picture below.
Code for getting it to appear on bottom:
RadioGroup rg = (RadioGroup) currentLayout.findViewWithTag("rg" + fld_id);
errorImage = new ErrorImageView(this);
errorImage.setBackgroundResource(R.mipmap.redxsmall);
errorImage.setScaleX(0.8f);
errorImage.setScaleY(0.8f);
rg.addView(errorImage);
Picture:
Any help would be appreciated. Thank you.
RadioGroup inherits from LinearLayout. That means it only supports placing views below or right of each other.
If you want both you have to wrap your RadioGroup inside another ViewGroup, e.g. a RelativeLayout.
RadioGroup rg = (RadioGroup) currentLayout.findViewWithTag("rg" + fld_id);
errorImage = new ErrorImageView(this);
errorImage.setBackgroundResource(R.mipmap.redxsmall);
errorImage.setScaleX(0.8f);
errorImage.setScaleY(0.8f);
rg.addView(0,errorImage);
errorImage.setTranslationY(100);
LayoutParams params = new Layoutparams(100,100);
params.gravity = Gravity.RIGHT;
rg.getChildAt(1).setLayoutParams(params)

Dynamic variables in a for loop?

I've got a layout which is called activity_main.xml which is my parent layout, and I am then inserting a child layout (activity_main_card.xml) within a for loop.
I also have an array, and what I am doing is using the length of the array to determine how many child elements it should create. All of my data to be used in the child element is stored in the array, so the idea is to loop through the array, create a child element for each loop and populate the data.
Instead, what is currently happening is that it is generating the 3 (length of array) child elements, but it is only populating the first one with the latest content in the array. This is because it keeps the variables the same.
What I need to do is somehow set dynamic variables that change as the loop iterates.
The code for my method is as follows:
for (int i = 0; i < cardArray.length; i++) {
LinearLayout item = (LinearLayout)findViewById(R.id.card_holder);
View child = getLayoutInflater().inflate(R.layout.activity_main_card, item, false);
item.addView(child);
//Set objects from array to variables
String cardTitle = cardArray[i][0];
String cardContent = cardArray[i][1];
String cardImage = cardArray[i][2];
//Set XML elements to variables
TextView title = (TextView) findViewById(R.id.card_title);
TextView content = (TextView) findViewById(R.id.card_content);
ImageView image = (ImageView) findViewById(R.id.card_image);
//Load variable content into card layout
title.setText(cardTitle);
content.setText(cardContent);
image.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + cardImage, "drawable", getPackageName())));
}
What I thought I could do was to set the views like:
TextView title[i] = (TextView) findViewById(R.id.card_title);
however this doesn't work. Does anyone know how I can acheive this?
You need to get a reference to the current View you just created and then populate the TextView elements on that View. This is achievable by doing this:
TextView title = (TextView) child.findViewById(R.id.card_title);
TextView content = (TextView) child.findViewById(R.id.card_content);
ImageView image = (ImageView) child.findViewById(R.id.card_image);
You might be better off approaching this problem differently however, as Jonathan suggests a ListView and adapter might suit better for this situation.
You aren't using the View created, you are using the root layout of the activity in every loop because you apply the findViewById in the parent view:
Change:
TextView title = (TextView) findViewById(R.id.card_title);
By;
TextView title = (TextView) child.findViewById(R.id.card_title);

Android instanceof detecting all widgets?

I'm trying to set a font across all TextView's by iterating through the LinearLayout's views and using instanceof.
The form currently consists of 4 TextView's and one Button.
The below code is detecting all Views, even the Button as a TextView?
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
If I log each view's class name it returns the TextView and the Button so I know the correct controls are being picked up.
The problem is the Button and TextView's fonts are being set, and I only want the TextView's.
I have found a work around and that is to do the following but am intrigued as to why the above code does not work as expected.
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v.getClass().getName().contains("TextView")) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
Is it because both Button and TextView are of type View? If so, what would be the correct way about doing this?
Any help appreciated, thanks. Ricky.
Actually, Button is a subclass of TextView! That's why you see it as a TextView (it is also a TextView).
http://developer.android.com/reference/android/widget/Button.html
public class Button extends TextView
You could make a second if instanceof to exclude Buttons or use
if (v.getClass() == TextView.class)
But this won't match any other subclasses of TextView (if you use them).
It is simple
Button Class extends TextView Class
Check the documentaion

Categories

Resources