Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: make a scrollable custom view

Tags:

android

view

I've rolled my own custom view and can draw to the screen alright, but what I'd really like to do is set the measuredHeigh of the screen to, say, 1000px and let the user scroll on the Y axis, but I'm having problems doing this. Can anyone help?

Here's some code:

public class TestScreen extends Activity  {
     CustomDrawableView mCustomDrawableView;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);    
         mCustomDrawableView = new CustomDrawableView(this);
         setContentView(mCustomDrawableView);
     }
 }

and

public class CustomDrawableView extends View {

    public CustomDrawableView(Context context) {
        super(context);
        setVerticalScrollBarEnabled(true);
        setMinimumHeight(1000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawLine(...);
        // more drawing
    }
}

I've tried to override scrollTo, scrollBy, awakenScrollBars etc with a call to super but to no avail. Am I missing something silly, or am I making some fundamental mistake?

Thank you in advance,

Martyn

Addition:

I've tried to add this as a custom component with the below layout file and changed the code in TestScreen to point at the correct resource with setContentView(R.layout.exampleLayout), but this causes the emulator to crash. I tried commenting the code down to the bare minimum and it still crashes, so there's something fundamentally wrong that I'm doing but I'm not sure what it is :

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
            <com.martyn.testApp.CustomDrawableView
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
             />
       </ScrollView>
</LinearLayout>
like image 262
Martyn Avatar asked May 09 '10 20:05

Martyn


People also ask

How do you make a scrollable view?

In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it. A ScrollView supports Vertical scrolling only, so in order to create a horizontally scrollable view, HorizontalScrollView is used.

Is GridView scrollable Android?

Grid view requires an adapter to fetch data from the resources. This view can be scrolled both horizontally and vertically. The scrolling ability of the GridView by default is set to enabled.

How do I make my recycler view scrollable?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .


1 Answers

Just put your view in a ScrollView!

Note that the ScrollWiew should be the root node (here it's your LinearLayout)

like image 148
MounirReg Avatar answered Oct 13 '22 06:10

MounirReg