3

I'm having a hard time with Android as a pretty newbie in the platform, and as I learnt, many people too. I don't know how many hours I have lost so far - it's terrifying even to count them.

I want basically to add a new TextView (or any other View) to LinearLayout after clicking the button. Here's this part of the code:

public void btnClick(View view) {

    final LinearLayout ll = (LinearLayout) findViewById(R.id.test_story_screen_layout);

    //checking if child is being added - it is, this value is increase with every button click
    android.util.Log.d("child: ", Integer.toString(ll.getChildCount()));
    android.util.Log.d("height: ", Integer.toString(ll.getMeasuredHeight()));

    ll.post(new Runnable() {

        public void run() {
            final TextView tv = new TextView(MainActivity.this);
            tv.setText("new one " + (new Random()).nextInt());
            tv.setTextColor(Color.YELLOW);
            LayoutParams lp = new LayoutParams(200, 500);
            tv.setLayoutParams(lp);
            tv.setVisibility(View.VISIBLE);
            ll.addView(tv, ll.getChildCount());

            //checking if the run() is called - it is
            ll.setBackgroundColor(Color.GREEN);
        }
    });

    ll.requestLayout(); //invalidate() and postInvalidate() also not working
}

But the newly added View is not visible (but is added as a child to LinearLayout).

After hours of checking what's wrong, I only found out that when I replace this line:

            ll.addView(tv, ll.getChildCount());

with this:

            ll.addView(tv, ll.getChildCount() - 1);

Then new View is visible, but it replaces the previous one - which is not desired.

I already checked some solutions, like this: Refreshing a LinearLayout after adding a view

They didn't help me with the issue.

UPDATE:

Linear Layout looks just fine with predefined in XML two Views (as in the code below). Here's intersting part of the XML:

<LinearLayout
    android:id="@+id/test_story_screen_layout"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_margin="16dp" >
    <ImageView
        android:id="@+id/imageView1"
        android:src="@drawable/example"
        android:layout_width="16dp"
        android:layout_height="16dp" />
    <TextView
        android:id="@+id/textView1"
        android:text="Hello World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Nothing unusual I think. So these two Views (Image and Text) are visible of course. Then any new TextView I add to this LinearLayout dynamically (via the btnClick() as in the code above) is not visible, but it is indeed added to Layout, as the ll.getChildCount() is increased every time when the child view is added (= button clicked).

In order to test this further, I added these two lines at the end of the btnClick() method:

    android.util.Log.d("tv.getMeasuredWidth: ", Integer.toString(tv.getMeasuredWidth()));
    android.util.Log.d("tv.getMeasuredHeight: ", Integer.toString(tv.getMeasuredHeight()));

I'm guessing the problem is the tv (TextView) rather than ll (LinearLayout), as tv gets width and height both 0.

Community
  • 1
  • 1
forsberg
  • 1,681
  • 1
  • 21
  • 27

5 Answers5

0

Your layout horizontal by default, When you add new view it set it up out from right border of screen. Just set orientation vertical in layout for you linear layout

Amarjit
  • 4,327
  • 2
  • 34
  • 51
0

Issue is you are initializing your linearlayout ll with what defined in your xml every time on button click and due to this your linearlayout is getting renewed. Move

final LinearLayout ll = (LinearLayout) findViewById(R.id.test_story_screen_layout);

in your onCreate if using Activity, onCreateView if using Fragment.

Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
0

The linearLayout orientation is horizontal by default set orientation to vertical.

Amarjit
  • 4,327
  • 2
  • 34
  • 51
0

if you want to add View as last element in Layout you can just do this: ll.addView(tv);

  • thx, this was my first attempt to use just 1 parameter to addView, then I delved further and checked other ways, so that eventually I digged out the fact that the new TextView becomes only visible when it replaces the existing one. In other case, when it's added as a last element, then it gets Width and Height as 0 (zero), which might be the issue. I don't know how to overcome this size issue anyway. – forsberg Feb 23 '16 at 14:50
  • why your layout has android:layout_width="0dp"? – Konstantin Volkov Feb 23 '16 at 15:07
  • This is recommended by Google when using android:layout_weight="1", which automatically manages then width of the View. But it's not a problem, I used this in other apps. – forsberg Feb 23 '16 at 15:19
0

There were number of things related in this Android issue, some of this were:

  • ll.post() made sense and appared to be required in my case
  • UI in Android should be updated in a special, not any, way, and it refers to e.g. Async Tasks
  • visibility of an item (item should be visible)
  • proper margins (when a screen e.g. is rotated to landscape, margin appeared to be too big)

Damn it. I find Android to be one of least thought-through platforms I've seen.

forsberg
  • 1,681
  • 1
  • 21
  • 27