Adding ImageView to the left of the radio group? - java

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)

Related

How to fix right button border not showing in app?

I am trying to make a game with multiple levels. My plan is to print 15 levels on 1 page. I have attached a photo of the simple design I am currently using.
As you can see in the picture the right border of a button is not showing. For the parent I am using this java code:
LinearLayout horl = new LinearLayout(this);
horl.setId(worldVar);
horl.setOrientation(LinearLayout.HORIZONTAL);
horl.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
And to set 3 buttons horizontally in this LinearLayout I use this code for the buttons. This code just has a simple for loop around to put 3 buttons in this LinearLayout:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout
.LayoutParams(getMarginInDp(100), getMarginInDp(100));
params.setMargins(getMarginInDp(1), getMarginInDp(25), getMarginInDp(1), 0);
button.setLayoutParams(params);
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);horl.addView(button);
The function getMarginInDp looks like this:
public int getMarginInDp(int dp){
return (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
Does anybody have any idea how to make the border appear because I want to keep the buttons approximately this size and I may even slightly increase the margins?
You can set the weight to 0.33f for each button:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getMarginInDp(100), getMarginInDp(100), 0.33f);
so they are distributed equally in the width of the layout.

Adding View dynamically to RelativeLayout not working

I am working on an app where I need to extensively work with animations. My problem is, I added an some ImageViews to my RelativeLayout through Java. But next to that when I am trying to add another ImageView applying rules like BELOW,ALIGN_LEFT etc with respect to previously added views, those images don't show up. It's annoying :( primarily, my question is, do these rules(LEFT,RIGHT_OF,BELOW etc) work with respect to already dynamically added views?
Q : do these rules(LEFT,RIGHT_OF,BELOW etc) work with respect to already dynamically added view
Ans: Yes they were much work. Sample Code
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);
// second Button
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv2 = new Button(this);
tv1.setText("Hello2");
newParams.addRule(RelativeLayout.RIGHT_OF, 1);
tv2.setLayoutParams(newParams);
tv2.setId(2);
rLayout.addView(tv2);
You need to set Ids and then give Whatever RULE you want to add for that view.

Android set gravity of a textview dynamically

I dynamically added horizontal layouts to my activity, and I am trying to put textviews inside of them.
The problem is, I can't manage to put some textviews To the right and some to the left.
I tried textview.setgravity, but it just changed the place of the text inside the textview instead of moving the textview itself.
Edit : here is some of my code to help you get an idea :
for(int i=0;i<nbelem;i++){ //génération des boutons des commandes
layouts[i] = new RelativeLayout(this);
layouts[i].setClickable(true); //toute la zone est cliquable
layouts[i].setId(i);
layouts[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 150));
Info[i] = new TextView(this);
Info[i].setClickable(true);
Info[i].setId(i);
Info[i].setText("Pizza 4 fromages x2 \n Pizza chevre x2");
Info[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
layouts[i].addView(Info[i]);
Numcom[i] = new TextView(this);
Numcom[i].setClickable(true);
Numcom[i].setId(i);
Numcom[i].setText("n°1522");
Numcom[i].setGravity(Gravity.RIGHT | Gravity.TOP);
Numcom[i].setTextSize(12);
Numcom[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,50));
It sounds that RelativeLayout may suit your needs and save you lots of nested LinearLayouts which are not recommended (performance wise).
You have to set a layout gravity to the LinearLayout that contains your textview.
LayoutParams lp = new LayoutParams();
lp.gravity= Gravity.CENTER_HORIZONTAL;
textview1.setLayoutParams(lp);
Be sure that you use LinearLayout.LayoutParams

Issue with dynamically added android radiobutton

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.

Setting up RelativeLayout in java code

I'm having a hard time getting two text views to appear on top of each other in my java code. Here's the code I'm experimenting with:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
layout = new RelativeLayout(this);
text1 = new TextView(this);
text1.setText("1");
text2 = new TextView(this);
text2.setText("2");
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
q.addRule(RelativeLayout.BELOW, layout.getId());
text1.setLayoutParams(q);
layout.addView(text1);
p.addRule(RelativeLayout.BELOW,text1.getId());
text2.setLayoutParams(p);
layout.addView(text2);
setContentView(layout);
}
This stacks the two text views on the same line, but I want TextView text2, to appear below TextView text1, so in my app I want the following to appear as the output:
1
2
I've tried all sort of things with the "addRule" method, I'm not sure why this isn't working. I want to know how to do this without XML because I plan to build a library of methods that can build up a layout that is easily adjustable through editing an array.
Your TextViews don't have an id (by default the id is -1)... put this after their initialization:
text1.setId(1111); // 1111 is just an example,
text2.setId(2222); // just make sure the id are unique
I don't think you are looking to layout the text1 view below the RelativeLayout since you added all your views to it as children, right? Try removing the first rule; that rule is asking the text view to be below the same view it is in.
EDIT: Also a help is explicitly setting the id of the view you are laying out relative to.
So here:
text1.setId(2);
p.addRule(RelativeLayout.BELOW,2);
you can use xml layout for this :
in relative layout
u set the first textview and assign it some id
fot the next text view we can assign parameter
android:layout_below="id of above text view"
in this way we get 2nd text view below 1st text view

Categories

Resources