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);
}
}