I have created following layout.which has two relative layouts and one scrollview ,following code contains headings of the layout.The I need to create sheet where i will use loop.
RelativeLayout inner=new RelativeLayout(this);
inner.setBackgroundColor(Color.RED);
outerLayout=new RelativeLayout(this);
ScrollView scroll=new ScrollView(this);
TextView qty=new TextView(this);
qty.setId(1111);
inner.addView(qty);
qty.setTextColor(Color.WHITE);
qty.setText("Qty");
setContentView(outerLayout);
RelativeLayout.LayoutParams p=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,30);
p.addRule(RelativeLayout.RIGHT_OF,qty.getId())
;
p.leftMargin=30;
TextView partNo=new TextView(this);
partNo.setText("Part no");
partNo.setId(2222);
partNo.setLayoutParams(p);
partNo.setTextColor(Color.WHITE);
inner.addView(partNo);
RelativeLayout.LayoutParams descLayout=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,30);
descLayout.addRule(RelativeLayout.RIGHT_OF,partNo.getId())
;
descLayout.leftMargin=30;
TextView desc=new TextView(this);
desc.setText("Description");
desc.setId(3333);
desc.setLayoutParams(descLayout);
desc.setTextColor(Color.WHITE);
inner.addView(desc);
RelativeLayout.LayoutParams commentLayout=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,30);
commentLayout.addRule(RelativeLayout.RIGHT_OF,desc.getId())
;
commentLayout.leftMargin=30;
TextView comment=new TextView(this);
comment.setText("Comments");
comment.setId(4444);
comment.setLayoutParams(commentLayout);
comment.setTextColor(Color.WHITE);
inner.addView(comment);
RelativeLayout.LayoutParams retailLayout=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,50);
retailLayout.addRule(RelativeLayout.RIGHT_OF,comment.getId())
;
retailLayout.leftMargin=30;
TextView retail=new TextView(this);
retail.setText("Retail \n Reference");
retail.setId(5555);
retail.setLayoutParams(retailLayout);
retail.setTextColor(Color.WHITE);
inner.addView(retail);
scroll.setVerticalScrollBarEnabled(true);
scroll.setHorizontalScrollBarEnabled(true);
scroll.addView(inner);
outerLayout.addView(scroll);
I tried to add scroll view to the layout but unfortunately i am unable to see the scrollview .Please help that how can i add the scrollview to above layout.
Not sure if its your problem but I don't see where you are setting layout params on the ScrollView.
Related
I don't know how I can set the TextView on the right side in my RelativeLayout. Adding gravity to the TextView didn't work. Thanks for helping me.
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.setGravity(Gravity.CENTER_HORIZONTAL); // center in linearLayout_power_stations_bar
textView = new TextView(context);
textView.setText(String.valueOf(nuke_count));
textView.setTextColor(activity.getResources().getColor(R.color.game_powerStationsBar_textOverIcon));
textView.setTextSize(16);
relativeLayout.addView(textView); // TextView over image
relativeLayout.addView(imageView); // adding image
linearLayout_power_stations_bar.addView(relativeLayout); // adding to other layout
You can set layout parameters to TextView and also set alignment of parent right.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textViewsetLayoutParams(params);
Now You can Use this code :)
I want to add Text-view at run time on Android to display the contents of my database on a activity,which can change every time.
Please Help
You could create a TextView dynamically like this for example:
//create a TextView with Layout parameters according to your needs
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//if your parent Layout is relativeLayout, just change the word LinearLayout with RelativeLayout
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
//get the parent layout for your new TextView and add the new TextView to it
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);
linearLayout.addView(tv);
And in the tv.setText(...); line you could set the text, that your got from your database.
I have an issue with the creation of a new RelativeLayout above an already existing TextView in my app.
My main layout is called contentLayout and inside it I have a TextView called addRoomButton :
RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.contentLayout);
TextView addRoomButton = (TextView) findViewById(R.id.addRoomText);
I just want to add a new RelativeLayout above my TextView so I wrote this :
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setId(R.id.roomRelativeLayout);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
100
);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp.addRule(RelativeLayout.ABOVE, addRoomButton.getId());
contentLayout.addView(relativeLayout, rlp);
The RelativeLayout is well created on the top of my contentLayout, but my TextView is not visible. I think it's just behind relativeLayout but I'm not sure...
Is there any solutons to put the TextView below the new RelativeLayout without creating LinearLayout or other Layouts?
Why wouldn't you do it by XML?
layout_below=""
try out your idea with xml. put relativelayout with relativelayout(A) in it and button(B). set A to layout_above B and see there is no success. Set B bellow A and you get the effect you want. So you need to getLayoutParams() of your button and adjust it to be below your newly created layout.
So I want to center a text View inside a linear layout where I have 2 other objects that populate the linear layout. I have a imageView and another textView. How would I go about centering the textView so the text is in the middle of the screen? This is what I have so far.
View linearLayout = findViewById(R.id.rockLayout);
ImageView mineralPicture = new ImageView(this);
TextView mineralName = new TextView(this);
TextView mineralProperties = new TextView(this);
mineralProperties.setText("The properties are: " + Statics.rockTable.get(rockName).getDistinctProp());
mineralProperties.setId(2);
mineralName.setText("This mineral is: " + rockName);
mineralName.setGravity(Gravity.CENTER);
mineralName.setId(1);
mineralName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mineralProperties.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mineralPicture.setId(3);
mineralPicture.setImageResource(R.drawable.rocks);
mineralPicture.setAdjustViewBounds(true);
mineralPicture.setMaxHeight(100);
mineralPicture.setMaxWidth(200);
mineralPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
((LinearLayout)linearLayout).addView(mineralName);
((LinearLayout)linearLayout).addView(mineralProperties);
((LinearLayout)linearLayout).addView(mineralPicture);
I should mention I've tried doing such things as mineralName.setGravtiy(Gravity.CENTER); and it hasn't worked.
a) let the textview match_parent on the width and use gravity center
b) on the textview layout params set the layout_gravity to center (sorry for the terms I normally do this in XML)!
I want a TextView on top and a VideoView below it. I want to have the VideoView as center verical and below the TextView. I am using RelativeLayout.
I am trying to do this in code:
RelativeLayout layout = new RelativeLayout(this);
TextView tv = new TextView(this);
tv.setText("Hello");
tv.setGravity(Gravity.RIGHT);
tv.setTextColor(Color.WHITE);
tv.setId(1);
RelativeLayout.LayoutParams one = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
one.addRule(RelativeLayout.ALIGN_PARENT_TOP);
one.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
RelativeLayout.LayoutParams two = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
two.addRule(RelativeLayout.CENTER_VERTICAL);
two.addRule(RelativeLayout.BELOW, 1);
VideoView mVideoView = new VideoView(this);
layout.addView(tv, one);
layout.addView(mVideoView, two);
setContentView(layout);
The VideoView gets placed below the TextView, but the VideoView is not center vertical.
You made your VideoView fill_parent/fill_parent, which gives it the same dimensions as its parent RelativeLayout. If it's as big as the RelativeLayout, centering cannot really work. Also, you cannot have the VideoView be both below the TextView and centered vertically in the parent; it's one or the other. Instead you could center the ViewView and place the TextView above it.