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.
Related
Hello I am trying to make 2 TextViews apear above each other through code.
My question is:
How can I stack them over each other?
Sample Code
private TextView DurrationView;
private TextView DurrationViewoverall;
DurrationView = new TextView(getContext());
DurrationView.setText("");
addView(DurrationView);
DurrationViewoverall = new TextView(getContext());
DurrationViewoverall.setPadding(5,5,5,5);
DurrationViewoverall.setText("");
addView(DurrationViewoverall);
I am trying to have DurrationView appear above DurrationViewoverall.
The class they are in extends Linear Layout.
Add LayoutParams to the textview and make your linear layout as orientation vertical
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
DurrationView = new TextView(getContext());
LinearLayout.LayoutParams durrationViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurrationView.setLayoutParams(durrationViewParams);
DurrationView.setText("Text1");
linearLayout.addView(DurrationView);
DurrationViewoverall = new TextView(getContext());
LinearLayout.LayoutParams durrationViewoverallParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurrationViewoverall.setLayoutParams(durrationViewoverallParams);
DurrationViewoverall.setPadding(5,5,5,5);
DurrationViewoverall.setText("Text2");
linearLayout.addView(DurrationViewoverall);
addView(linearLayout);
Check that the LinearLayout is set to have a vertical orientation. If that is set properly, you may need to set the LayoutParams of the TextViews programatically as well.
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 am adding an imageView to a Relative layout.It shows up at the beginning of layout
I have two buttons in the layout.
I button to the layout left and one to layout right. Now i want to place these under the button to right.
how can i do that. My code is
ImageView image = new ImageView(this);
image.setImageBitmap(imageBitmap);
layout.addView(image);
Try using the
android:Layout_below="#id/yourButtonId" or
android:LayoutBelow="#id/yourButtonId"
Programmatically it means :
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, yourButtonVariable.getId());
yourRelativeLayout.addView(yourImageView, rlp);
I' m at this point stuck : I want to have a scrollview + a fixed button at the bottom, but Programatically Way ! I can' t go with XML for some technical reason.
Actually i have this :
//Is it really usefull Relative View?
RelativeLayout layout = new RelativeLayout(this);
ScrollView sv = new ScrollView(this);
sv.setId(2);
// What is it? RelativeLayout.LayoutParams?
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, sv.getId());
sv.setLayoutParams(new ViewGroup.LayoutParams(480, 800));
layout.addView(saveButton, lp);
layout.addView(sv);
I do the first 3 page Google on "fixed button and scrollview Android programatically"
Im beginner on Android, so, don' t hesitate to comment on my code some hints ;)
Thx for your help.
try this
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(lp);
ScrollView scroll = new ScrollView(this);
LinearLayout.LayoutParams slp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,0, 1.0f);
scroll.setLayoutParams(slp);
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
layout.addView(scroll);
layout.addView(btn);
setContentView(layout);
If you are trying to create a view where the upper portion of the screen is a scrolling list, and the bottom portion is a button, put the scrollview inside a linearlayout, and put the button in the linearlayout below the scrollview.
http://developer.android.com/reference/android/widget/LinearLayout.html
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.