0

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;

The next step is to set up the background, text, and button.

@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

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 so that the button is centered on the screen, and that the text lays on top.

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

Then we add the desired parameters of the layout we desire to the correspond button and text.

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

And then we use this to set the android app.

setContentView(manusLayout);

    }

Finally we add all of the gestures available.

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

    @Override
    public void onShowPress(MotionEvent e) {

    }

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

And for the scroll we change manustText the title of our app to "Scrolled".

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

    @Override
    public void onLongPress(MotionEvent e) {

    }

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

I'm learning how to code and have been trying to figure out what I have done wrong. But for the life of me I can't figure why the text wont change from "hello" to "scrolled" when I scroll on the AVD. Thank you very much ahead of time. -Manu

Manuty
  • 27
  • 1
  • 1
  • 7

0 Answers0