0
votes

I'm trying to have a locked textField and a ScrollView in an Android app. I'm trying to do so at runtime but I am missing something. Whatever object I add to the LinearLayout first is the ONLY one that is shown. What am I missing? I am NOT defining anything through a xml layout file.

My main activity is:

package com.example.scrolltest;

import com.example.scrolltest.Draw; import android.widget.ScrollView; import android.widget.LinearLayout; import android.widget.TextView; import android.os.Bundle; import android.app.Activity; import android.graphics.Color;

public class MainActivity extends Activity { Draw draw;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
protected void onResume(){
    super.onResume();

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    int lHeight = LinearLayout.LayoutParams.MATCH_PARENT;
    int lWidth = LinearLayout.LayoutParams.MATCH_PARENT;        

    TextView tv = new TextView(this);
    tv.setText("Test text.  Boom.");

    draw = new Draw(this);
    draw.setBackgroundColor(Color.WHITE);
    ScrollView scrollView = new ScrollView(this);
    scrollView.addView(draw);

    // add the views to the layout
    ll.addView(scrollView, new LinearLayout.LayoutParams(lHeight, lWidth));

    ll.addView(tv, new LinearLayout.LayoutParams(lHeight, lWidth));

    setContentView(ll);

}

}

Just for grins as the content for the ScrollView is dynamic, and can change anytime, I'll show a static example of my Draw object:

package com.example.scrolltest;

import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View;

public class Draw extends View { Paint paint = new Paint();

public Draw(Context context) {
    super(context);            
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // Compute the height required to render the view
    // Assume Width will always be MATCH_PARENT.
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = 4000 + 50; // Since 3000 is bottom of last Rect to be drawn added and 50 for padding.
    setMeasuredDimension(width, height);
}

@Override
public void onDraw(Canvas canvas) {
    paint.setColor(Color.BLUE);

    canvas.drawLine(100, 20, 100, 1900, paint);
    paint.setColor(Color.BLACK);
       canvas.drawText("00:00", 10, 10, paint);
       int y = 0;
       int x = 200;
    for(int i = 100; i < 2900; i=i+10){

       paint.setColor(Color.GREEN);
       canvas.drawRect(x, i, x+50, i+10, paint);

    if(y == 0){
        y = 1;
        x = 200;
    } else
   {
        y = 0;
        x = 30;
    }
    }
}

}

1
Can you explain why you add the ScrollView to the linear layout and not the inverse,Hossam Oukli

1 Answers

1
votes

You are using LinearLayout.LayoutParams.MATCH_PARENT for the width and height for both TextView and ScrollView. Whichever view you add first will fill the entire LinearLayout leaving no room for the other. Also, I notice that you set the LinearLayout's orientation as horizontal. This will stack the child views side by side rather than on top of each other. I suggest the following changes:

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
int lHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
int lWidth = LinearLayout.LayoutParams.MATCH_PARENT;        

TextView tv = new TextView(this);
tv.setText("Test text.  Boom.");

draw = new Draw(this);
draw.setBackgroundColor(Color.WHITE);
ScrollView scrollView = new ScrollView(this);
scrollView.addView(draw);

ll.addView(tv, new LinearLayout.LayoutParams(lHeight, lWidth));

// add the views to the layout
ll.addView(scrollView, new LinearLayout.LayoutParams(lHeight, lWidth));

setContentView(ll);

This will give you the TextView on top and ScrollView underneath it.

Edit:

Another thing, LayoutParams() takes arguments in order: (width, height). You are supplying the arguments in the opposite order.