Search code examples
androidandroid-scrollbar

Android Add horizontalscrollbar and verticalscrollbar to a view dynamically


HI,
I have a problem in dynamically adding horizontal and vertical scrollbar to a view . Basically the application zoom in and zoom out an image. I want to put horizontal and vertical scrollbar dynamically whenever view is out bounds. i.e. when zoom in is click and view is gonna out of bond, then it automatically add scrollbar. I am using these two methods but they didn't work correctly.
        setVerticalScrollBarEnabled(true);
        setHorizontalScrollBarEnabled(true);
Does i have to put these methods inside onDraw(Canvas canvas) method of the view?
Note: I am not creating view using xml file. I am creating view using a class which is inherited from View.
Thank you!!


Solution

  • not quite the same thing but this may give you a clue:

        mTextView = new TextView(this);
        mTextView.setGravity(Gravity.CENTER_VERTICAL);       
        mTextView.setText(R.string.instructions);
        mTextView.setTextColor(0xFF000000);
        mTextView.setPadding(20, 8, 8, 20);
        //mTextView.setBackgroundColor(0xCCFFFFFF);
        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);
    
        mScroll = new ScrollView(this);
        mScroll.setScrollbarFadingEnabled(false);
        mTextPane = new RelativeLayout(this);
        mTextPane.setVisibility(View.GONE);
        //mScroll.setVisibility(View.GONE);
    
        mScroll.addView(mTextView);
        mTextPane.addView(mScroll);
    
        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.text_pane_feather2);
        mTextPane.setBackgroundDrawable(drawable);
    
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 420, 420 );
        //mBackground.setImageBitmap(R.drawable.text_pane);
        lp.setMargins(0,0,0,30);
        lp.addRule(RelativeLayout.CENTER_HORIZONTAL );
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    
        layout.addView(mTextPane, lp);