Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Can't understand where should I use longpress in my code.

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't understand where should I use longpress in my code.

    Hi. basically I just made a drum pad app, but I can't figure out how to use a longpress or whatever to be able to turn on/off some buttons ( each is playing different music so all I need is to tap once ( to activate ) and the second tap should turn it off )


    package com.example.niko.drumpad;
     
    import android.media.AudioManager;
    import android.media.SoundPool;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
     
    public class MainActivity extends AppCompatActivity {
        private SoundPool sp;
        private int sound1;
        private int sound2;
        private int sound3;
        private int sound4;
        private int sound5;
        private int sound6;
        private int sound7;
        private int sound8;
        private int sound9;
        private int sound00;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            sp = new SoundPool(9, AudioManager.STREAM_MUSIC,0);
            sound1 = sp.load(getApplicationContext(),R.raw.sound1,1);
            sound2 = sp.load(getApplicationContext(),R.raw.sound2,1);
            sound3 = sp.load(getApplicationContext(),R.raw.sound3,1);
            sound4 = sp.load(getApplicationContext(),R.raw.sound4,1);
            sound5 = sp.load(getApplicationContext(),R.raw.sound5,1);
            sound6 = sp.load(getApplicationContext(),R.raw.sound6,1);
            sound7 = sp.load(getApplicationContext(),R.raw.sound7,1);
            sound8 = sp.load(getApplicationContext(),R.raw.sound8,1);
            sound9 = sp.load(getApplicationContext(),R.raw.sound9,1);
            sound00 = sp.load(getApplicationContext(),R.raw.sound00,1);
     
        }
     
        public void playsound1(View v){sp.play(sound1,1.0f,1.0f,0,0,1.0f);}
        public void playsound2(View v){sp.play(sound2,1.0f,1.0f,0,0,1.0f);}
        public void playsound3(View v){sp.play(sound3,1.0f,1.0f,0,0,1.0f);}
        public void playsound4(View v){sp.play(sound4,1.0f,1.0f,0,0,1.0f);}
        public void playsound5(View v){sp.play(sound5,1.0f,1.0f,0,0,1.0f);}
        public void playsound6(View v){sp.play(sound6,1.0f,1.0f,0,0,1.0f);}
        public void playsound7(View v){sp.play(sound7,1.0f,1.0f,0,0,1.0f);}
        public void playsound8(View v){sp.play(sound8,1.0f,1.0f,0,0,1.0f);}
        public void playsound9(View v){sp.play(sound9,1.0f,1.0f,0,0,1.0f);}
    }

    and

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:background="@color/colorPrimary"
        android:padding="10dp"
        android:gravity="center"
        android:orientation="vertical">
     
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:background="@drawable/blue_button"
                android:onClick="playsound1"
                android:layout_width="90dp"
                android:layout_height="90dp" />
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/blue_button"
                android:onClick="playsound2"
                android:layout_width="90dp"
                android:layout_height="90dp" />
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/blue_button"
                android:onClick="playsound3"
                android:layout_width="90dp"
                android:layout_height="90dp" />
     
        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:background="@drawable/purple_button"
                android:onClick="playsound4"
                android:layout_width="90dp"
                android:layout_height="90dp" />
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/orange_button"
                android:onClick="playsound5"
                android:layout_width="90dp"
                android:layout_height="90dp" />
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/pink_button"
                android:onClick="playsound6"
                android:layout_width="90dp"
                android:layout_height="100dp" />
     
        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:background="@drawable/green_button"
                android:onClick="playsound7"
                android:layout_width="90dp"
                android:layout_height="90dp" />
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/green_button"
                android:onClick="playsound8"
                android:layout_width="90dp"
                android:layout_height="90dp" />
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/pink_button"
                android:onClick="playsound9"
                android:layout_width="90dp"
                android:layout_height="90dp" />
     
        </LinearLayout>
     
     
    </LinearLayout>

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't understand where should I use longpress in my code.

    Look at the Android tutorial on how a program should handle screen presses. There are probably examples on how to do it.
    The API doc: http://developer.android.com/reference/packages.html and search for longpress
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2018
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't understand where should I use longpress in my code.

    Capture.jpg
    I have lots of errors now -_-

    package com.example.niko.drumpad;
     
    import android.media.AudioManager;
    import android.media.SoundPool;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
     
    public class MainActivity implements View.OnLongClickListener {
        Button button;
        private SoundPool sp;
        private int sound1;
        private int sound2;
        private int sound3;
        private int sound4;
        private int sound5;
        private int sound6;
        private int sound7;
        private int sound8;
        private int sound9;
        private int sound00;
     
        @Override
        public boolean onLongClick(View view) {
            return false;
        }
     
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            button.setOnLongClickListener(this);
            setContentView(R.layout.activity_main);
     
            sp = new SoundPool(9, AudioManager.STREAM_MUSIC,0);
            sound1 = sp.load(getApplicationContext(),R.raw.sound1,1);
            sound2 = sp.load(getApplicationContext(),R.raw.sound2,1);
            sound3 = sp.load(getApplicationContext(),R.raw.sound3,1);
            sound4 = sp.load(getApplicationContext(),R.raw.sound4,1);
            sound5 = sp.load(getApplicationContext(),R.raw.sound5,1);
            sound6 = sp.load(getApplicationContext(),R.raw.sound6,1);
            sound7 = sp.load(getApplicationContext(),R.raw.sound7,1);
            sound8 = sp.load(getApplicationContext(),R.raw.sound8,1);
            sound9 = sp.load(getApplicationContext(),R.raw.sound9,1);
            sound00 = sp.load(getApplicationContext(),R.raw.sound00,1);
     
        }
     
        public void playsound1(View v){sp.play(sound1,1.0f,1.0f,0,0,1.0f); }
        public void playsound2(View v){sp.play(sound2,1.0f,1.0f,0,0,1.0f);}
        public void playsound3(View v){sp.play(sound3,1.0f,1.0f,0,0,1.0f);}
        public void playsound4(View v){sp.play(sound4,1.0f,1.0f,0,0,1.0f);}
        public void playsound5(View v){sp.play(sound5,1.0f,1.0f,0,0,1.0f);}
        public void playsound6(View v){sp.play(sound6,1.0f,1.0f,0,0,1.0f);}
        public void playsound7(View v){sp.play(sound7,1.0f,1.0f,0,0,1.0f);}
        public void playsound8(View v){sp.play(sound8,1.0f,1.0f,0,0,1.0f);}
        public void playsound9(View v){sp.play(sound9,1.0f,1.0f,0,0,1.0f);}
    }
    Last edited by Norm; January 26th, 2019 at 12:07 PM. Reason: QUOTE changed to CODE tags

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't understand where should I use longpress in my code.

    I have lots of errors now
    Please copy the text of the error messages and paste it here. No images. Text can not be copied from images to include in a response.

    The click listener class can be an inner class to the Activity class.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2018
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't understand where should I use longpress in my code.

    error: cannot find symbol method onCreate(Bundle)
    error: cannot find symbol methon GetApplicationContext
    simply failed to add that longpress dammit..here is what one guy told me to do ( but I did not understand ) :
    You have to add onLongClickListener to your button and implement the method onLongClick in your main activity. for example:
    public class MainActivity implements View.OnLongClickListener
    after you implement the onLongClickListener you override the function onLongCLick

    @Override
    public boolean onLongClick(View view) {
    return false;
    }
    And finally you need to set onLongClickListener to your button

    btn.setOnLongClickListener(this);
    Last edited by fallen01; January 26th, 2019 at 12:19 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't understand where should I use longpress in my code.

    Do you know how to use listeners? Basically you define a class that implements the needed methods and then create an instance of the class and pass that to the add listener method for the object that creates the event that is being listened for.
    Often inner classes are used for that.

    Do a google for sample code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2018
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't understand where should I use longpress in my code.

    As a beginner - unfortunately no. There is no sample code for drum pad that includes longlick..

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't understand where should I use longpress in my code.

    Start with a project that uses an inner class.
    Define the inner class that implements the required listener.
    Then create an instance of the class and pass that instance to the add listener method of the object where the event (click) happens.

    The bits and pieces you need are in post#5. They need to be in the correct places.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2018
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't understand where should I use longpress in my code.

    What projects was you creating as a beginner ? Seems like I am choosing hard ones.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't understand where should I use longpress in my code.

    What projects was you creating as a beginner
    My first java projects were an Applet to manage and plot waypoints - I still use derivatives of that program
    and a desktop app to organize and display images in a slideshow - I still use that program.
    That was it the early 2000s so there wasn't as many samples on the internet. I don't remember if Google existed then.

    Note: I had been a programmer for a long time before that. Mostly in assembler.

    Seems like I am choosing hard ones.
    Don't try to do all of the project at one go. Break it up into small steps and do them one at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2018
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't understand where should I use longpress in my code.

    Alright thanks sir!

Similar Threads

  1. I dont understand this code ?
    By ammehmeti in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 22nd, 2014, 07:57 AM
  2. What do I need to learn to understand this code?
    By import.Snupas in forum Java Theory & Questions
    Replies: 6
    Last Post: December 6th, 2013, 02:14 PM
  3. Help me understand this code?
    By Farkuldi in forum Java Theory & Questions
    Replies: 11
    Last Post: November 12th, 2013, 03:37 PM
  4. Hi,Can any one help me to understand this code.Am a beginner.
    By Jawad24 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 23rd, 2013, 08:47 AM