(android) an scrollview that gets programmatically filled loses title - android

I have this layout:
<?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:layout_centerInParent="true"
android:layout_centerVertical="true"
android:background="#000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal|center_vertical">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/play"
android:gravity="center_horizontal"
android:scaleType="centerInside"
android:text="#string/highScores"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#fff" />
<View
android:layout_width="1dp"
android:layout_height="20dp"></View>
<TableLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal">
</TableLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I fill the 'container' TableLayout with elements like this:
layout.addView(buildTitleRow());
for (int i = 0; i < users.length(); i++) {
JSONObject parent = (JSONObject) users.get(i);
layout.addView(buildRowFrom(parent, i + 1));
}
buildTitleRow:
private TableRow buildTitleRow(){
TableRow result = new TableRow(this);
int color = Color.parseColor("#ffff00");
View view = new View(this);
view.setMinimumWidth(10);
result.addView(view);
view = new View(this);
view.setMinimumWidth(10);
result.addView(view);
//the same for other views
return result;
}
buildRowFrom:
private TableRow buildRowFrom(final JSONObject element, int counter) throws JSONException {
TableRow result = new TableRow(this);
String rowName = element.getString("name");
int color = Color.parseColor("#00ff00");
if(rowName.equals(userName)){
color = Color.parseColor("#ffffff");
}
TextView textView = new TextView(this);
textView.setText(counter + ".");
textView.setTextColor(color);
result.addView(textView);
//some other views
return result;
}
this really works well just until the scrolling needs to take place...
Once there are more elements than the height of the screen the table remains scrollable (luckily) but the original 'title' Textview doesn't show up anymore (you can't scroll up towards it anymore)
Does anyone know how to tweak the layout to keep it visible once more elements are added than the size of the screen?
Thanks a lot,
S.

I've tried your code and found find something wrong in your xml layout.
The attribute android:layout_gravity will change the layout's own gravity. So if set this attribute to a LinearLayout with center and its android:layout_height is wrap_content(means that the real height of the 'LinearLayout' will exceed the screen's height) and fill ScrollView(its height is the screen's height) with it, the LinearLayout will align to center of its parent ScrollView.
If you want to keep the content of 'LinearLayout' centered, you can set android:fillViewport="true" to ScrollView and then the LinearLayout will fill the parent and you can use android:gravity="center" on the LinearLayout. Reference
But there is still some thing need to be changed. In your java code, View view = new View(this) this line will new a View and then you add it into TableLayout. But this TableLayout will be full of View because of the description of View below.
* The base class implementation of measure defaults to the background size,
* unless a larger size is allowed by the MeasureSpec. Subclasses should
* override {#link #onMeasure(int, int)} to provide better measurements of
* their content.
You can use a concrete view like TextView instead of it.
I don't know if I have clarified it. I think if you remove the attribute android:layout_gravity of LinearLayout, it will work.
I'm glad if it would help.

I did the adaptations Paul suggested + change android:layout_height of the ScrollView to "wrap_content" to keep the inner LinearLayout centered when the android:layout_gravity="center" was removed from it (see complete layout below).
I wouldn't have found it without Paul's suggestion and explanation so I'll accept his answer as answer, this answer is just for the sake of completeness...
I'm not expecting this question to get that much of attention anyways ;-)
Thanks Paul :-D
<?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:layout_centerInParent="true"
android:layout_centerVertical="true"
android:background="#000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/play"
android:gravity="center_horizontal"
android:scaleType="centerInside"
android:text="#string/highScores"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#fff" />
<View
android:layout_width="1dp"
android:layout_height="20dp"></View>
<TableLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal">
</TableLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>

Related

ScrollView not sizing itself correctly

I'm trying to get a ScrollView to take up as much screen space as it needs until it would start pushing items below (outside) it off the screen, then it needs to stop expanding and become scrolly.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable"
/>
<!--
android:text="just one line"
-->
</ScrollView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
As it is above the ScrollView fills the entire screen height and pushes the button off the bottom of the screen.
If I add android:layout_weight="1" to the ScrollView then it works for this case - the button is at the bottom and the ScrollView stops above it - but when the ScrollView doesn't have much content (replace the text with the one-liner) then the ScrollView doesn't shrink to fit around the content so is far too tall.
I've tried using RelativeLayout with no success - if the Button is android:layout_below the ScrollView then the ScrollView will push it off the bottom of the screen if it has a lot of content.
Here's what I want it to look like: in the first image the ScrollView has a lot of content and so expands to fill the available height but doesn't push the items below it (the button) offscreen, in the second image the ScrollView doesn't have much content so takes up just the height it needs allowing the items below it (the button) to move up the screen:
What you can do, is to correct the height in your code. It's a bit hacky and I would like to see another solution, but off the top of my head I do not know anything better.
What you would need is to add a OnGlobalLayoutListener and calculate within it the minimum of either the ScrollView height or the height of the container surrounding your ScrollView minus the height of your Button.
Then set the size with setLayoutParams() on your ScrollView.
And you have to remove the listener to avoid an endless loop :-)
final View scrollview = findViewById(R.id.scrollview);
final View container = findViewById(R.id.container);
final View button = findViewById(R.id.button);
scrollview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int height = scrollview.getHeight();
int heightButton = button.getHeight();
int heightContainer = container.getHeight();
int min = Math.min(heightContainer - heightButton, height);
int width = scrollview.getWidth();
Log.v("test", "min: " + min);
scrollview.setLayoutParams(new RelativeLayout.LayoutParams(width, min));
// do not forget to remove the listener
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
scrollview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
else {
scrollview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
For this to work you have to use wrap_content as the height of the ScrollView in your layout file. And the outer container has to be a RelativeLayout so that the Buttonis rendered and has a non-zero height!
If you use paddings or margins you would have to consider those values in the computation.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button"
android:background="#ff0000">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="taking\nup\lots\nof\nlines\nto\nmake\nthe\nscrollview"s\ncontent\ntaller\nthan\nthe\nspace\navailable\nto\nit\nwhich\nshould\nmake\nthe\nscrollview\nstop\nabove\nthe\nbutton\nand\nbecome\nscrollable" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
</RelativeLayout>
Just try adding fillViewport = "true" on your scroll view

RelativeLayout width and height fill_parent doesn't work

This is my layout which is an info window, and its shown where there is new info to show to the user.
The layout should take the whole screen with some transparent background color, and a small text layout:
<?xml version="1.0" encoding="utf-8"?>
<!-- info fragment - used by the fragment to provide info on a specific fragment -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_dark_green" >
//this is the only layout i see in the result. not it's parent's layout...
<RelativeLayout
android:id="#+id/fragment_info_relativelayout_info"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:background="#drawable/background_info_window"
android:clickable="true" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_margin="5dp" >
<!-- The info textView -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_info_textview_info"
style="#style/text_shadow_black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left"
android:text="Some Text "
android:textColor="#android:color/white"
app:isBold="true" />
</ScrollView>
<!-- Tap to close -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_info_textview_tap_to_close"
style="#style/text_shadow_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingRight="5dp"
android:text="#string/button_tap_to_close"
android:textColor="#android:color/white"
app:isBold="true" />
</RelativeLayout>
</RelativeLayout>
I'm adding this layout to another ViewGroup like this:
/**
* Show the info window
*/
public void show()
{
//if the window is not visible
if (!isVisible())
//add window's view
rootView.addView(infoWindowView);
}
this code add the info window into its root view container in the last index.
in result i only see the second RelativeLayout which contains the text but i don't see its parent root layout which supposed to be transparent...
Any ideas why ?
Update:
This is my container:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the main layout of the application -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_basic_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
I also had problems with a view in Relative layout that was dependent on the Relative layout's size.
I solved this problem problematically by updating the inner view size
private void update viewSize()
if(rootContainer == null || rootContainer.getLayoutParams() == null)
return;
rootContainer.measure(View.MeasureSpec.makeMeasureSpec(ScalingUtil.getScreenSize().getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY));
RelativeLayout.LayoutParams oldLayoutParams = (RelativeLayout.LayoutParams) rootContainer.findViewById(R.id.deals_card_center_bg).getLayoutParams();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(rootContainer.getMeasuredWidth(), rootContainer.getMeasuredHeight());
layoutParams.setMargins(oldLayoutParams.leftMargin, oldLayoutParams.topMargin, oldLayoutParams.rightMargin, oldLayoutParams.bottomMargin);
rootContainer.findViewById(R.id.deals_card_center_bg).setLayoutParams(layoutParams);
How to not have the parent expand
Instead of match_parent on the child, align it to the sibling that dictates the height.
<RelativeLayout ...>
<View
android:id="#+id/element_that_should_match_its_parents_height"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignBottom="#+id/element_that_dictates_height" />
<View
android:id="#+id/element_that_dictates_height"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Worked for me, hope it helps. The second View is usually some ViewGroup, that varies in height.
Try change
<RelativeLayout
android:id="#+id/fragment_info_relativelayout_info"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:background="#drawable/background_info_window"
android:clickable="true" >
to
<RelativeLayout
android:id="#+id/fragment_info_relativelayout_info"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#drawable/background_info_window"
android:clickable="true" >
Anyway, why the need of nested RelativeLayout ?
I see how do you add the view, so try also this:
infoWindowView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
rootView.addView(infoWindowView);

Linearlayout inside scrollview with even distriburtion

I have a data entry type activity and im using a linear layout to space out evenly the sets of textviews and edittexts. Then I have a scroll view that is supposed to make it so the user can scroll while the softkeyboard is up.
If I use android:fillViewport the linearlayout works properly and fills the screen and spreads each item out evenly but then when the keyboard comes up and stops each item being spread out evenly. If i use android:windowSoftInputMode="stateVisible|adjustPan" then the linearlayout remains spread out but the scroll view doesn't work anymore (from all the unsolved posts on this i don't think you can have a working scroll view with adjustPan)
is there any way to have a linearlayout inside a scrollview, with items spread out evenly and still work while the softkeyboard is up with out changing the linearlayout?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport
>
<LinearLayout
android:gravity="left"
android:orientation="vertical"
android:id="#id/tab2"
android:paddingLeft="40.0dip"
android:paddingTop="0.0dip"
android:paddingRight="40.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<TextView
android:id="#id/textViewBrand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/brand" />
<AutoCompleteTextView
android:id="#id/editTextBrand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</AutoCompleteTextView>
</LinearLayout>
...more linearlayouts with textview and edittext to be spaced out evenly
set the property android:fillViewport="true" in your scrollview it will work
Maybe the ScrollView isn't working because your
android:layout_height attribute
is defined to match_parent. You have to put
wrap_content
Follow This Code, I hope it resolves your Problem
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
//Your Main Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100">
// First Sub Layout Under Main Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="10"
android:weightSum="100" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TextView"
android:layout_weight="70" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="30" />
</LinearLayout>// Finishing First Sub layout
// Second Sub Layout Under Main Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="10"
android:weightSum="100" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TextView"
android:layout_weight="70" />
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="30" />
</LinearLayout>// Finishing Second Sub layout
similarly for 3rd,4rth,5th sub layouts and so on........
</LinearLayout> // Finishing Main Layout
</ScrollView> // Finishing ScrollView
you can go over the child views of the LinearLayout and set their size to a fixed size yourself in code using a ViewTreeObserver.OnGlobalLayoutListener.
the following code sets the sizes to not change, so you can use the android:fillViewport option with weights, and then essentially remove the weights, but keep the calculated sizes.
I used this code with some modifications to also make sure child views are distributed evenly in a LinearLayout that is inside a ScrollView, where the LinearLayout is too big for the android:fillViewport option to get the weights to actually work. this was done by doing 2 iterations over the children. first getting the max view's height, and then setting all child views to that max height.
private void distributeViewsEvenlyInLinearLayout() {
ViewTreeObserver observer = linearParentView.getViewTreeObserver();
final ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressWarnings("deprecation")
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
linearParentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
linearParentView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
for (int i = 0; i < linearParentView.getChildCount(); i++) {
View child = linearParentView.getChildAt(i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, child.getHeight());
child.setLayoutParams(layoutParams);
}
}
};
observer.addOnGlobalLayoutListener(globalLayoutListener);
}

how to avoid overlapping of dynamically positioned views

how to avoid overlapping of dynamically positioned views?
I have a RelativeLayout , and i am adding views dynamically(at runtime) at particular position(x,y coordinates) but the problem is the views are overlapping.
How to avoid this.
Thanks in advance.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rl_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/ll_mainBottom"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
javacode
ll_main = (RelativeLayout) findViewById(R.id.rl_main);
if (views[3].equals("textView")) {
TextView tv_new = new TextView(TenMinActivity.this);
// location
int x = Integer.parseInt(views[12]);
int y = Integer.parseInt(views[13]);
String bgColor = "#" + views[4];
String fgColor = "#" + Views[5];
tv_new.setBackgroundColor(Color.parseColor(bgColor)); // Bg Color
tv_new.setTextColor(Color.parseColor(fgColor)); // Text color
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = x;
params.topMargin = y;
ll_main.addView(tv_new, params);
}else if(views[3].equals("edittext")){
....
}
Give
android:paddingLeft=""
and to each element so that the ones to the left most of the screen will have bit of space. and the ones to the right,give
android:paddingBottom=""
First,check the first 2 elements that is the name text and your text field after that you can proceed to the rest.
I can guide you better,if you post your code
Check this,
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Name"
android:textSize="18sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_toRightOf="#+id/textView1"
android:ems="10"
android:padding="10dp"
android:paddingl="10dp" >
<requestFocus />
</EditText>
</RelativeLayout>
You need to learn how layouts work, first try to make the same layout in xml. Then you will learn that this kind of layout can easily be made using LinearLayout.
You can use parent LinearLayout and add sublayouts to it. Now you say if you use LinearLayout all subviews are shown vertically. It shows vertically because you are adding them one by one. If you take a RelativeLayout and add your TextView and EditText to it and then add that RelativeLayout to your LinearLayout, you will get the desired result.
<LinearLayout>
<RelativeLayout>
<TextView />
<EditText />//use layout_margin or layout_toLeftOf for moving to to right
</RelativeLayout>
<RelativeLayout>
<TextView/>
<EditText/>
</RelativeLayout>
</LinearLayout>
Please use Layout params like layout_below layout_above toRightof and ToLeftof when you add a view to container relative layout
https://stackoverflow.com/a/5191159/1911784
you an use below code to add views in your layout
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView text2 = new TextView(context);
newParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
newParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newParams.addRule(RelativeLayout.ALIGN_BOTTOM, text1.getId());
text2.setLayoutParams(newParams);
layout.addView(text2);
where layout is your main layout.
you can use other params as well - like rightof, leftof etc

Adding a view to a LinearLayout at a specified position

I have the following main.xml file with a LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1" android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
I add an image to the LinearLayout via code at runtime like so
ImageView image = new ImageView(this);
image.setImageBitmap(bmp);
LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
ll.addView(image);
However, I want to add the ImageView between the 2 TextViews in my LinearLayout. I can't seem to find a way in the android docs to add a view before another view, or after. How can I do this?
NB I call
setContentView(R.layout.main);
Before I add the ImageView to the LinearLayout.
When adding a View to a ViewGroup, you can specify an index which sets the position of the view in the parent.
You have two views and so (counting from zero) you would want to add at the 1st position; just call ll.addView(image, 1); to have it placed in between the two TextViews.
The docs state you can use the index to insert it where you want. I see you are using the signature of the view only, did you try the signature with the index parameter?
public void addView(View child, int index)
I faced a similar problem. In my case I wanted to add a LinearLayout at last position of another LinearLayout. To accomplish it, I did:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// add others views to your linear layout
parentLayout.addView(layout, parentLayout.getChildCount());
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.imagefield);
img.setImageResource(your_image_here);
and in the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<ImageView android:id="#+id/imagefield"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
Add an ImageView into the xml, and if its not being used, make it invisible (image.setVisibility(View.INVISIBLE)). It may not show anything anyway when no image is set.
To get position of view in a view group
val targetPosition = oldLL.indexOfChild(viewToAdd)
To add a view at a position in a view group
newLL.addView(viewToAdd,targetPosition)

Categories

Resources