In an Android activity....how do I do the following using just code instead of XML?
<TextView android:background="#0000ff" android:layout_margin="2dip"/>
I also need text align right...
stuck on it for a while =( if someone could help
thanks
UPDATE
I tried the code below
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
//250);
lp.setMargins(5,5,5,5);
and i did
row.addView(t1, lp);
row.addView(t2);
row.addView(t3);
row.addView(t4);
I also tried LinearLayout
but t1 for some reason doesn't show at all anymore....
here is how to code a textView:
mTextView = new TextView(this);
mTextView.setGravity(Gravity.CENTER_VERTICAL);
mTextView.setText(R.string.instructions);
mTextView.setTextColor(0xFF000000);
mTextView.setPadding(20, 8, 8, 20);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);
mScroll = new ScrollView(this);
mScroll.setScrollbarFadingEnabled(false);
mTextPane = new RelativeLayout(this);
mTextPane.setVisibility(View.GONE);
//mScroll.setVisibility(View.GONE);
mScroll.addView(mTextView);
mTextPane.addView(mScroll);
Resources res = getResources();
//Drawable drawable = res.getDrawable(R.drawable.text_pane_feather2);
Drawable drawable = res.getDrawable(R.drawable.text);
mTextPane.setBackgroundDrawable(drawable);
//RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 420, 420 );
RelativeLayout.LayoutParams lp =
new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,
//RelativeLayout.LayoutParams.WRAP_CONTENT);
250);
lp.setMargins(0,0,0,30);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL );
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(mTextPane, lp);
Try the TextViews setBackgroundColour() method.
For setting the layout params, use the setMargins(int,int,int,int) from the MarginLayoutParams class.
See the documentation for more details at:
View.setBackgroundColour and
Layout Params
Related
GridLayout glSameLoactions;
#OnClick(R.id.tvAddLocation)
void addLocationOnClickListener(){
if(i==0) {
glSameLoactions = new GridLayout(this);
glSameLoactions.setColumnCount(3);
glSameLoactions.setBackgroundResource(R.drawable.shape_rounded_corner_blurfilled);
glSameLoactions.setPadding(16,16,16,16);
}
RelativeLayout rlLocation = new RelativeLayout(this);
rlLocation.setPadding(16, 16, 16, 16);
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(300,200);
relativeParams.setMargins(16, 16, 16, 16);//<<<<----- NOT WORKING
rlLocation.setLayoutParams(relativeParams);
rlLocation.requestLayout();
rlLocation.setBackgroundResource(R.drawable.shape_rounded_corner_blurfilled);
TextView tvLocationName = new TextView(this);
tvLocationName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tvLocationName.setId(R.id.tvLocationName);
tvLocationName.setText("amritsar,PB");
tvLocationName.setTextColor(getResources().getColor(R.color.white));
tvLocationName.setTextSize(14f);
ImageView ivRadiobtn = new ImageView(this);
tvLocationName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ivRadiobtn.setImageResource(R.drawable.ic_circle_empty);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rlLocation.addView(tvLocationName);
rlLocation.addView(ivRadiobtn,lp);
glSameLoactions.addView(rlLocation);
if (i==0) {
llLocations.addView(glSameLoactions);
llLocations.setVisibility(View.VISIBLE);
}
i++;
}
I am making a layout programmatically, in which on a click of button, a new relative layout is added in a grid layout. All my code is working fine, but the problem is that i am unable to set margins to the relative layout inside the grid layout. Please help!!
Thanks
You should use RelativeLayout.LayoutParams instead of LinearLayout.LayoutParams.
Try this:
RelativeLayout.LayoutParams relativeParams =new RelativeLayout.LayoutParams(300, 300);
relativeParams.setMargins(16, 16, 16, 16);
rlLocation.setLayoutParams(relativeParams);
I am trying to dynamically add 2 buttons to my relative layout. I want it to look like this:
but it currently looks like this:
With #Abdallah Alaraby's help:
myButton1 = new Button(this);
myButton1.setBackgroundResource(R.drawable.button);
myButton1.setText("bttn1");
myButton2 = new Button(this);
myButton2.setBackgroundResource(R.drawable.button);
myButton2.setText("bttn2");
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl_dynamic_bttn);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.RIGHT_OF, myButton2.getId());
rl.addView(myButton1, lp1);
rl.addView(myButton2, lp);
...
...
}
I tried all sort of different allignment options but nothing seems to be working. Anyone know how I can make this look like the first picture?
Is it possible that the issue is with my_button2.getId()? Maybe it's not recognized?
lp.addRule(RelativeLayout.RIGHT_OF, myButton2.getId());
rl.addView(myButton1, lp);
rl.addView(myButton2, lp);
You are using the same rule for both buttons, try the following:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myButton1.setId(1)
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.RIGHT_OF, myButton2.getId());
rl.addView(myButton1, lp1);
rl.addView(myButton2, lp);
If myButton2 does not have an assigned Id, the default Id would be -1 and it won't work. you have to use myButton2.setId(int) before using myButton2.getId()
I'm having some troubles creating a relativeLayout programmatically.For better understanding I attached a picture below. The things with plus and minus are buttons, which should also be dynamically created and added to the layout. The values of tv4 and tv5 should increase/decrease accordingly to button presses.
What I have done so far:
1) creating the layout:
RelativeLayout rl = new RelativeLayout(this);
rl.setId(i);
rl.setBackgroundResource(R.drawable.bg);
RelativeLayout.LayoutParams Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.BELOW, R.id.RL_default);
Lparams.setMargins(3, 5, 3, 0);
rl.setLayoutParams(Lparams);
2) adding the tv1:
Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Lparams.setMargins(10, 0, 0, 0);
TextView txt = new TextView(this);
txt.setTextColor(Color.parseColor("#FFFFFF"));
txt.setId(x);
txt.setTextSize(25);
txt.setLayoutParams(Lparams);
txt.setText(name);
rl.addView(txt);
3) adding the tv2:
Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Lparams.addRule(RelativeLayout.BELOW, txt.getId());
Lparams.setMargins(10, 0, 0, 0);
TextView txtS = new TextView(this);
txtS.setId(y);
txtS.setText("Test: ");
txtS.setTextSize(22);
txtS.setLayoutParams(Lparams);
txtS.setGravity(Gravity.BOTTOM);
txtS.setPadding(0, 0, 0, 20);
rl.addView(txtS);
4) now I want to create the first button:
Button btnSminus = new Button(this);
btnSminus.setId(btn1);
btnSminus.setText("<");
btnSminus.setTextSize(20);
Lparams= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.RIGHT_OF, txtS.getId());
btnSminus.setLayoutParams(Lparams);
rl.addView(btnSminus);
The problem is, the the button View seems to just disappear from the screen when the line Lparams.addRule(RelativeLayout.RIGHT_OF, txtS.getId()); is executed. What can be the reason?
My guess would be that it's because you set the width of txtS to match_parent, so the button gets pushed off the screen. You can fix that by changing that to wrap_content.
I have a image placed in the relative layout. How can i place a small image over the background image and two text view side by side next to the image. Any help will be really appreciated. The only thing is I want to do this programmatically. I have tried doing it the following way but for some reason the textview appears on the top left of the screen. I want it appear on the imagebutton.
LinearLayout LinearLayout = new LinearLayout(this);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams;
Button[] btn = new Button[1];
btn[0] = new Button(this);
btn[0].setId(1);
btn[0].setGravity(Gravity.CENTER);
btn[0].setText("text");
btn[0].setBackgroundResource(R.drawable.button_grey);
btn[0].setTextColor(Color.parseColor("#FFFFFF"));
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT | RelativeLayout.ALIGN_PARENT_TOP);
relativeLayoutParams.setMargins(20, 20, 15, 15);
relativeLayout.addView(btn[0], relativeLayoutParams);
ImageButton[] Imgbtn = new ImageButton[10];
Imgbtn[0] = new ImageButton(this);
Imgbtn[0].setId(2);
Imgbtn[0].setBackgroundResource(R.drawable.box_round_corners);
Imgbtn[0].setMaxHeight(200);
Imgbtn[0].setClickable(true);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.BELOW,btn[0].getId());
relativeLayoutParams.height = 100;
relativeLayoutParams.setMargins(15, 0, 15, 10);
Imgbtn[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(Imgbtn[0], relativeLayoutParams);
TextView[] txtview = new TextView[10];
txtview[0] = new TextView(this);
txtview[0].setId(3);
txtview[0].setTextColor(Color.parseColor("#000000"));
txtview[0].setText("text");
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT | RelativeLayout.ALIGN_TOP);
relativeLayoutParams.addRule(RelativeLayout.BELOW,Imgbtn[0].getId());
relativeLayoutParams.height = 20;
relativeLayoutParams.setMargins(5, 5, 5, 5);
txtview[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(txtview[0], relativeLayoutParams);
android.widget.ScrollView ScrollV = new ScrollView(this);
ScrollV.addView( relativeLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addContentView(ScrollV, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
try to set your views LayoutParams with setLayoutParams() method;
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(width,height);
lp.topMargin=//what is your desired y coordinate
lp.leftMargin=//what is your desired x coordinate
lp.gravity=Gravity.NO_GRAVITY;
view.setLayoutParams(lp);
FrameLayout is better choise for overlaying purposes in my opinion.
How to set margin in relative layout with code (not XML)?
I use this code but nothing happened:
RelativeLayout rlDetail = new RelativeLayout(context);
rlDetail.setBackgroundResource(R.drawable.bg_round);
RelativeLayout.LayoutParams rlDetailParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlDetailParams.setMargins(35, 10, 35, 0);
rlDetail.setLayoutParams(rlDetailParams);
try this answer set the absolute position of a view in Android
as mentioned in the link above you should use
TextView tv1 = new TextView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = needed_margin;
params.topMargin = needed_margin;
// add your textview like this
rl.addView(tv1, params);