Help on Bucky's tutorial number 21&22

0 Jo Gold · April 22, 2015
Hey.
I watched Bucky's tutorials, and in the end he gives an assignment.
(its in the end of the vid right here)
https://www.youtube.com/watch?v=V_KbEfhf1qc.

So i tryed to do it, but i ran into few problems.
First of all, i took the final code from the tutorial, and i tryed to add a button, 
that will chane the text in addition to the scroll.
BUT- when i tryed to do that- if i clicked, and then scrolled, the app run just fine.
But when i scroll first- it crashes.

package com.jogame.swiperdiaper;

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

public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener
{

   private TextView josText;
   private GestureDetectorCompat gestureDetector;

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

        setContentView(R.layout.activity_main);

        Button josButton = (Button)findViewById(R.id.josButton);

        josButton.setOnClickListener(
                new Button.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        josText = (TextView)findViewById(R.id.josText);
                        josText.setText("Nice Click");
                    }
                }
        );

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





    }

    ///////////GESTURES/////////////////
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {

        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {

        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {

        return true;
    }

    @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) {
        josText.setText("Nice Scroll");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        return true;
    }

    ////////////END////////////





    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.gestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}




Besides that problem i had few more problems-
I tryed to change the background color of the pick.
I know i can do it on the activity_main.xml file, but i wanted to practice doing it on the java file.
So i tryed to follow one of Bucky's previos tutorials, but i didnt succseed.
I tryed to add a relative layot, add the button and the text, like in tutorial number 14-15 i think-
but when i tryed to add the scroll it did not work.

So i had the final code from tutorial 15 (onlt that i had text instead of edit text)
and i tryed to copy the parts that are relevent to the scroll from tutorials number 21-22, but it didnt work.
I couldnt connect the onScroll method to the text.

During all the of my struggling some qustions poped into my head:
1.I have seen two ways of adding widgets such as buttons-
  the first one is that: thats when i add the button on the activity_main.xml file.


Button josButton = (Button)findViewById(R.id.josButton);


and the other one- when i add it manually:
Button josButton = new Button(this);

what is the difference between the two?

2. How do i change the background color with out doing it on the xml file?


THANKS A LOT GUYS <3

Post a Reply

Replies

Oldest  Newest  Rating
0 Nicholas Eason · April 23, 2015
Adding it manually means you have to set it's parent (the "this" in parenthesis that you pass to it) and it also comes with no parameters attached (location, size, background, etc.). This becomes more of a pain, but in the long run (assuming that you ever make a custom view) you'll probably have to do it this way.

Doing it through XML means that you can set all of it's parameters outside of the java code, and it's less of a pain in the butt.

To answer the second question:
Button.setBackgroundColor(Color.COLOR);


A few notes about the above code:

Button is the name of the variable, Color is a class, it stays Color, and COLOR is the actual name of the color i.e:
myButton.setBackgroundColor(Color.BLUE);
  • 1

Java / Android Development

107,221 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator