[source code] Android Development Tutorial - 20

+3 Bucky Roberts · December 23, 2014
package com.thenewboston.smartbutton;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {

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

                new Button.OnClickListener(){
                    public void onClick(View v){
                        TextView buckysText = (TextView)findViewById(R.id.buckysText);
                        buckysText.setText("Good job Hoss!");

                new Button.OnLongClickListener(){
                    public boolean onLongClick(View v){
                        TextView buckysText = (TextView)findViewById(R.id.buckysText);
                        buckysText.setText("Holy carp, that was a long one!");
                        return true;


    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;

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

Post a Reply


Oldest  Newest  Rating
0 Richardo Petri · May 31, 2015
I got the same results as the previous posters for the different axioms of False/true.

I Also made a note of the axiomatic structure for both.


It seems to me that the 

Public void

command can not distinguish between other values then I/O so that after the "long click" with the "false" setting the listener is still listening and at it is after the long click button is released that the I/O can actually register a reading of one therefore it replaces the text value with "Good job" or whatever one has coded.
is this correct?
Really good tutorials and I appreciate the fact that you ask us to do this exercise it forces one to think for oneself and therefore it makes one learn better.
I should admit I'm somewhat familiar with JAVA so i have some idea of syntax and what a Boolean is, but it was after the axiomatic structuring I remembered this.
0 Steven the awesome · December 23, 2014
return false: When you click on the OnLongClickListener and you release, You automatically goes back to the Good job Hoss text.

return true:   When you click on the OnLongClickListener and you release you keep staying on the Holy carp, that was a long one! text.
0 Mark Jayson Sison · September 11, 2015
Im following your tutorial and when i hold the button with a return false it came back to the first event which is just clicking it.
Thank you very much for your tutorial bucky  :D
0 venkata sai rohit kumar danda · December 25, 2014
return false : it return to onClickListener automatically and display the message of onClickListener and when we return true it remain same in the onLongClickListener.
0 servet portakal · January 1, 2015
I understand that when we have multiple listeners, the app will be waiting for all of them, and since we don't want all of the callback methods to be triggered at the same time or one right after another, we have that boolean return value that  tells the program that the events is handled, relax, don't go try to do something else by checking other listerners.

But, why do we not have a boolean return value for the normal click?  because it is a tap, meaning it is shorter than a long click. For that reason It'll never trigger the long click event no matter what...?

As for the homework :) when we set the return value to false, on long click it shows the message "a long one..." but the moment you stop holding down the button, it goes back to displaying "hello". 
( In my program, first message is "hello", on click it says "goodbye" and if it already clicked and displaying anything other than hello, next time goes back to hello. On long click it says a long one.)
  • 1

Java / Android Development


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

Bucky Roberts Administrator