Dynamic relativelayout inside scrollview - android

I am adding dynamic content via Java to a relativelayout which sits inside a ScrollView:
<ScrollView
android:layout_below="#id/btn_draw_route"
android:id="#+id/layout_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_alignParentBottom="true">
<RelativeLayout
android:id="#+id/layout_route"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>
For some reason, the content inside the scrollview is not scrollable.
I am adding the content like this:
Button mButton = new Button(this);
mButton.setText(String.valueOf(singleStep.getId()));
mButton.setId(View.generateViewId());
mButton.setOnClickListener(new MyClickListener(singleStep.getId()));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp.bottomMargin = 10;
lp.topMargin = 10;
lp.leftMargin = 10;
lp.rightMargin = 10;
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
// Add to relativeLayout
mRouteLayout.addView(mButton, lp);
I already tried calling mScrollLayout.invalidate(); after adding content, but it does not help. What I noticed is that if I do not set android:fillViewport="true" in the scrollview, its height is very low and only shows a part of the relative layout.
If I set the height of the relativeLayout to for example 2000dp, the container is scrollable, but only shows a small part of the actual content of the relativeLayout.
Edit: Might be important: The contents are added from the bottom to the top, each with an attribute like
lp.addRule(RelativeLayout.ABOVE, button2.getId(););

If I remember correctly, if you are adding your views programmatically, you have to call the requestLayout() method to refresh the view for the wrap_content to work
just do:
mRouteLayout.requestLayout();

Related

Android- setWidth() of Button doesn't work, if ViewGroup's width is 'match_parent'/'fill_parent' in xml?

I defined a LinearLayout as root element in activity_main.xml.
CASE 1: From onCreate() I tried to add Button in this Vertical LinearLayout, what confused me is, as per Google's API, I tried to call setWidth(20) on button before adding it in ViewGroup, but Button occupied width 'match_parent' rather than 20dp.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:id="#+id/first_layout">
</LinearLayout>
//Inside onCreate() of activity..
LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
Button button = new Button(this);
button.setText(R.string.click_on_me);
button.setWidth(20);
firstLayout.addView(button);
CASE 2:on setting layout_width of LinearLayout to 'wrap_content', and calling setWidth(20), It now considered given explicit width value i.e 20dp.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:id="#+id/first_layout">
</LinearLayout>
//Inside onCreate() method
LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
Button button = new Button(this);
button.setText(R.string.click_on_me);
button.setWidth(20);//In this case, its working
firstLayout.addView(button);
CASE 3: Finally, removing my custom call to setWidth(20), Button acquired width as 'wrap_content'.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:id="#+id/first_layout">
</LinearLayout>
//Inside onCreate() method.
LinearLayout firstLayout = (LinearLayout) findViewById(R.id.first_layout);
Button button = new Button(this);
button.setText(R.string.click_on_me);
firstLayout.addView(button);
Ques : So it is clear by case-2, that using LayoutParams is not necessary, if I wish to explicitly use setWidth() method.Then In Case 4: i.e LinearLayout's width set as 'match_parent' and button.setWidth(20) is also called.
But why Button is still not taking explicitly given width value, again output is exactly same as CASE 1.
Thanks in advance.
You need to define an appropriate LayoutParams for your button view. Then add it to your firstLayout.
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.height = XX;
params.width = XX;
button.setLayoutParams(params);
You have to understand why using Layoutparams is necessary when you dynamically create Button or any component.
Suppose if your using LayoutParams to Button to give width. Then When we set the layout params of the Button, we are telling the parent layout (that is LinearLayout in your case) rendering the Button, to set the specified height and width for the view. Therefore, it works fine while rendering.
In case 1 as you told it is not affecting setwidth of 20 that is because the Button has a minimum width of 64dip by default. Set it to 0 before setting the width.
btn.setMinimumWidth(0);
This link here could give you some help.

Adding a widget in scrollView programmatically

I am having trouble adding a view in the scrollview. The structure of my xml is
// xml_layout.xml
<ScrollView>
<LinearLayout
android:id="#+id/container"
>
<LinearLayout>
<ListView
...
/>
</LinearLayout>
******Here I am trying to add an item *********
</LinearLayout>
</ScrollView>
I am trying to add a textview at the bottom programmatically as follows.
TextView v = new TextView(getActivity());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setText("TEST View");
v.setLayoutParams(params);
LinearLayout container = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.xml_layout, null).findViewById(R.id.container);
container.addView(v, 1);
The goal I am trying to achieve is making a tablelayout that contains two columns with the title and listview in the second column. Before I do this, I tried to display a simple textview so that I know I can add a widget. I have tried many different ways but did not work. Is there something wrong with my code??
you must first verify that your LinearLayout 'container' is 'vertical ' orientation, and...
LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(v);

create and set margin programmatic for relative layout android

Hi I am developing android application in which I am creating relative layout programmatic and tried to set margin for that and Added it into linear layout which have orientation linear.
So here is my code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/screen_background"
tools:context=".ChooseChannelsFragment" >
<LinearLayout
android:id="#+id/main_outer_llt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
</LinearLayout>
</RelativeLayout>
and inside fragment I am adding relative layout like this
RelativeLayout relativeLayout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(200, 80);
relativeParams.setMargins(20, 20, 20, 20);
relativeLayout.setLayoutParams(relativeParams);
relativeLayout.setBackgroundColor(getResources().getColor(R.color.green_color));
linearLayout.addView(relativeLayout);
It create layout with given color and size but not accepting margins. Am I doing something wrong? How to do this? Need Help. Thank you.
The LayoutParams type you use on a view should actually be from its parent.
So, if you're adding a RelativeLayout to a LinearLayout, the LayoutParams you set to your RelativeLayout should actually be a LinearLayout.LayourParams, and not a RelativeLayout.LayoutParams.
As others suggested, layout_margin# is the space between the parent's # edge and your view.
# replaces "Left", "Right", "Top" or "Bottom"
Getting/setting margins worked for me with:
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mView.getLayoutParams();
params.topMargin += 20;
mView.requestLayout(); // important
Of course, my View was indeed a ViewGroup and the parent was a ViewGroup as well. In most cases, you should cast your layout params to the parent's View class LayoutParams (in this case it's ViewGroup and RelativeLayout)
In this case, the father is LinearLayout. So you should use:
TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 20, 20, 20);

TextView write text vertically when setting its layoutparams to fill_parent

I am trying to add TextView into a view which extends LinearLayout. When I set its width of layoutparams to fill_parent or wrap_content, the text inside TextView will display vertically such like the following.
e.g.
T
E
X
T
However, when I set the width to some fixed number, it will display normally or as I expected.
e.g. TEXT
My question is why this happens and how to solve it, i.e. how I should set programmatically so that TextView can write text horizontally without setting a fixed width to it such as setting fill_parent or wrap_content?
Here is setting of XML code of the parent ilGallery which extends LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.illib.ilgallery.view.ILGallery
android:id="#+id/galleryLayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
The following is the code how i initialize the children inside it:
ilViewPager = new ILViewPager(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
ilViewPager.setLayoutParams(params);
ilgallery = this;
//initialize default header and footer view
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
headerView = new TextView(context);
headerView.setLayoutParams(params2);
((TextView)headerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)headerView).setText("hello");
footerView = new TextView(context);
footerView.setLayoutParams(params2);
((TextView)footerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)footerView).setText("hello2");
There appears to be only a single item inside your LinearLayout. Please set the orientation of the LinearLayout to "horizontal", like this:
android:orientation="horizontal"
Also, whenever possible instead of creating new LayoutParams objects from scratch, you should get the LayoutParams from the current layout and modify that, like this:
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)ilViewPager.getLayoutParams();
lp.<change-any-values>;
ilViewPager.setLayoutParams(lp);

Relative layout change through java code

I went through several links yet i am not able to find if it is possible to set other attributes of relative layout, like android:gravity,android:orientation through java code.
I want to change the positions of the some of the views through java code and not through xml.
e.g, i want to set these attributes through java code for a particular view. android:gravity="center_vertical" android:orientation="vertical"
how can i do so? please help.
as Daan said you can use Layout params for relative layout in android
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.alignWithParent = true;
params.bottomMargin = 5;
params.height = 50;
params.leftMargin = 50;
But as you said you want to set following attributes for particular view, you cannot assign these attributes to RelativeLayout as these are belongs to LinearLayout and other views
android:gravity="center_vertical" android:orientation="vertical"
For setting gravity for any view please follow the code
tv = (TextView)findViewById(R.id.tv01);
tv.setGravity(Gravity.CENTER_VERTICAL);
You cannot set Orientation of any view.. instead you can set orientation to LinearLayout
like this
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Hope this will help you
Within a relative layout you have to use other layout params:
Layout params within relative layout

Categories

Resources