Search code examples
androidscrollbarscrollview

How to add scrollbar or scrollview in android with class java not xml?


How to add scrollbar or scrollview in android with class java not xml this code?

LayoutInflater inflater = (LayoutInflater)   getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.srvr, null);
layout = (RelativeLayout) view.findViewById(R.id.relativeLayout1);
RelativeLayout.LayoutParams labelLayoutParams = new  RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

layout.setLayoutParams(labelLayoutParams);

labelLayoutParams = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    labelLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    labelLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    labelLayoutParams.leftMargin=120;

   final EditText tt = new EditText(this);
      tt.setTag(i);
      tt.setText(DisplayName_list[i].toString());
      tt.setMinWidth(230);
      tt.setMaxWidth(230);
      tt.setMaxLines(1);

Solution

  • As I understood well you want to create a ScrollView programatically. You create it as any other views:

    ScrollView sc = new ScrollView(this);
    sc.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    sc.setFillViewport(true);
    

    And then you have to add to it your view. Make sure that your view you are adding to the ScrollView is a root view.

    sc.addView(ROOT_VIEW)
    

    In your case ROOT_VIEW is a layout which is RelativeLayout