Mahdi Jafarzadeh
Mahdi Jafarzadeh

Reputation: 138

How to disable scolling in a HorizontalScrollView?

I have a HorizontalScrollView and a Button in my project. I want this >>> when Button clicked the HorizontalScrollView don't scroll. Realy user can fix the view.

I use this code but it is not working.

    Button btn_s = (Button) findViewById(R.id.button999);

    final HorizontalScrollView   h_scroll = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);

    btn_s.setOnClickListener(new OnClickListener() {

        @Override
          public void onClick(View v) {


            h_scroll.setVisibility(0);


        }

          })

Upvotes: 3

Views: 987

Answers (2)

Mahdi Jafarzadeh
Mahdi Jafarzadeh

Reputation: 138

I solved this problem whit this code:

 Button btn_s = (Button) findViewById(R.id.button999);
 final HorizontalScrollView   h_scroll = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
 btn_s.setOnClickListener(new OnClickListener() {

@Override
  public void onClick(View v) {

       if(!return_h_scroll){

        return_h_scroll  = true;

       }   
       else
            return_h_scroll  = false;

}

  });
  h_scroll.setOnTouchListener(new OnTouchListener() {

 @Override
 public boolean onTouch(View v, MotionEvent event) {

     return return_h_scroll;

 }
 });

Upvotes: 0

Sahil Mahajan Mj
Sahil Mahajan Mj

Reputation: 11141

You can use,

h_scroll.setVisibility(View.INVISIBLE);
h_scroll.setVisibility(View.GONE);

Also, you can define your own behaviour for the scroll view using the function,

public boolean onInterceptTouchEvent(MotionEvent ev) { }

Have a look at here, Intercept Horizontal ScrollView

Upvotes: 3

Related Questions