0

I need something like this:

LinearLayout (fix)
- Button
LinearLayout
- ScrollView
- TextView
- TextView
- TextView

I want the first LinearLayout to be fix (like a header). When someone tries to scroll down, i want the TextView to go behind the fix LinearLayout. I want that button always to be visible. With this code, everything is scrolling down. Here is my code:

    ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);

    final LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(linearLayout.VERTICAL);
    scrollView.addView(linearLayout);

    final LinearLayout topLinearLayout = new LinearLayout(this);
    topLinearLayout.setOrientation(topLinearLayout.VERTICAL);
    linearLayout.addView(topLinearLayout);

    final Button button = new Button(this);
    button.setText(R.string.button);
    topLinearLayout.addView(button);

    TextView title = new TextView(this);
    title.setText(R.string.title);
    title.setGravity(Gravity.CENTER);
    linearLayout.addView(title);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • You can create it as layout xml and then inflate it. That would be much easier to handle. `LayoutInflater inflater = LayoutInflater.from(getContext()); View view = inflater.inflate(R.layout.yourLayout, yourContainer, attachToRootOrNot);` – mgokgoz Nov 17 '15 at 08:44
  • And how can i do that? Can you be more specific? – Alex Mamo Nov 17 '15 at 10:13
  • just create a layout for your view via layout editor or writing xml manually, then call it like I wrote up. Than call children views by for example `Button button = view.findViewById(R.id.btn);` Edit: You can check it out a video of my application at betone.co.uk . If one of the screens is like what you want to achieve I can share that part of code to you – mgokgoz Nov 17 '15 at 11:42

1 Answers1

0

You are adding your topLinearLayout into your linearLayout, which you are adding in your scrollView. this is not you want.

add scrollView and topLinearLayout in another LinearLayout

final LinearLayout mainlinearLayout = new LinearLayout(this);
mainlinearLayout.setOrientation(linearLayout.VERTICAL);

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);

final LinearLayout topLinearLayout = new LinearLayout(this);
topLinearLayout.setOrientation(topLinearLayout.VERTICAL);

mainlinearLayout.addView(topLinearLayout); 
mainlinearLayout.addView(scrollView);

final LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);

final Button button = new Button(this);
button.setText(R.string.button);
topLinearLayout.addView(button);

TextView title = new TextView(this);
title.setText(R.string.title);
title.setGravity(Gravity.CENTER);
linearLayout.addView(title);
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
  • Thanks you, but it doesn,t work. I have this error: `Unable to start activity ComponentInfo{com.example.alexm.iqtest/com.example.alexm.iqtest.IqTest}: java.lang.NullPointerException` – Alex Mamo Nov 17 '15 at 10:12
  • Problem solved with [this](http://stackoverflow.com/questions/33754968/how-to-have-dynamically-a-fixed-header-with-scrollable-content-in-android) – Alex Mamo Nov 17 '15 at 11:55