Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.16/5 (4 votes)
I'm trying to make an app with Android Studio that displays a message,in this case manusText, when scrolled. I'm clearly do something wrong as this is not the case. Here is my code:

First I import all the packages

package com.sceptech.scrolly;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.graphics.Color;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;

Then I follow up by implementing the listener so that the code registers the desired interaction, in this case the scroll.

public class scrolly_menu extends AppCompatActivity implements GestureDetector.OnGestureListener {

private TextView manusText;
private GestureDetectorCompat gestureDetector;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Set the background

RelativeLayout manusLayout = new RelativeLayout(this);
manusLayout.setBackgroundColor(Color.parseColor("#2BABD6"));

// Create a title and button

final TextView manusText = new TextView(this);
manusText.setText("Hello!");

Button manusButton = new Button(this);
manusButton.setText("Click Me");
manusButton.setBackgroundColor(Color.parseColor("#2056E8"));

manusButton.setId(1);
manusText.setId(2);


// Set layout parameters for title and button

RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);

RelativeLayout.LayoutParams textDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);

buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);
buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);

textDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
textDetails.addRule(RelativeLayout.ABOVE, manusButton.getId());
textDetails.setMargins(0, 0, 0, 50);


manusLayout.addView(manusButton, buttonDetails);
manusLayout.addView(manusText, textDetails);


setContentView(manusLayout);

Create a click listener such that when the user click on the button the text displayed changes.(This works)

manusButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View V) {
manusText.setText("Good Job Manu!");
}
}
);

this.gestureDetector = new GestureDetectorCompat(this, this);

}

Finally implement the code.

@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {

return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
manusText.setText("Scroller");
return true;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
Posted
Comments
Alcamech 23-Oct-15 17:03pm    
Could you please put the code in a code block to make it easier to read. I am having trouble understanding what exactly is your problem. Are you trying to make it so the text that is displayed stays on the screen while you scroll ?
Member 12082587 23-Oct-15 17:09pm    
No I'm not actually trying to scroll anything up or down. I just want the text to change as a responsive to a scroll. So the app has a text and a button. First the text says "hello". If I click the button the text will display "good job manu" which works. I want it to display "Scroller" if I scroll which it does not. Does that clarify?
Alcamech 23-Oct-15 17:20pm    
Yes that clarifies a lot thanks. Have you tried implementing a .OnScrollListener ?
Member 12082587 23-Oct-15 17:25pm    
I tried looking at it but I was unable to implement. I just started coding so I think it's possible, I just don't know how to. I have been able to do this exercise but not coding it completely with java. I just followed the this tutorial from min:48. of https://www.youtube.com/watch?v=3ChYHpI2hHA.
Alcamech 23-Oct-15 17:28pm    
I honestly think your best bet would probably be to make the message appear when the screen is touched ( when you go to scroll ). I myself am quite unsure how to implement a .OnScrollLister as I do not have that much experience with android studio itself.

1 solution

Try implementing a .OnScrollListener

or make it respond to touch so that when you begin scrolling the text "Scoller" appears

C#
myLayout.setOnTouchListener(
       new RelativeLayout.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent m) {
            // Perform tasks here
            return true;
        }
       }
);
 
Share this answer
 
Comments
ridoy 25-Oct-15 14:55pm    
a 4!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900