Multiple Buttons

0 Radomir Kolev · May 6, 2015
Hello there. I am trying to add 2 different buttons and the one must have OnClick/OnLongClick function and another is just OnLongClick. I have troubles with the second button: Thanks in advance.




 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Button starts here

         Button buttonAnotherActivity = (Button)findViewById(R.id.buttonAnotherActivity);
         Button longClicker = (Button)findViewById(R.id.longClicker);


        buttonAnotherActivity.setOnClickListener(
                new Button.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        TextView LargeTextView =(TextView)findViewById(R.id.LargeTextView);
                        LargeTextView.setText("You just changed the text!");
                    }
                }
        );




        //Button ends here

        //Long click button start
        buttonAnotherActivity.setOnLongClickListener(
                new Button.OnLongClickListener()
                {
                    public boolean onLongClick(View v)
                    {
                        TextView LargeTextView =(TextView)findViewById(R.id.LargeTextView);
                        LargeTextView.setText("You just activated the long click!");
                        return true;
                    }
                }
        );

        //long click button end

        //Another long clicker

        longClicker.setOnLongClickListener(
            new Button.OnLongClickListener() //<<<<Error is here
            {
              public void OnLongClick(View  v)
              {
                  TextView LargeTextView =(TextView)findViewById(R.id.LargeTextView);
                  LargeTextView.setText("Get your dirty hands away from my phone");
                  
              }
            }
        );

    }


Post a Reply

Replies

Oldest  Newest  Rating
0 Radomir Kolev · May 6, 2015
Just added this lines of code which worked. Someone can explain if I should always do that ovverride method ?


 longClicker.setOnLongClickListener(
            new OnLongClickListener()            {

                @Override
                public boolean onLongClick(View v) {
                    TextView LargeTextView =(TextView)findViewById(R.id.LargeTextView);
                    LargeTextView.setText("Get your dirty hands away from my phone");
                    return true;
                }
            }
        );

+1 Nicholas Eason · May 6, 2015
You should. It tells the program that you're overriding a method, versus creating your own.
  • 1

Java / Android Development

107,325 followers
About

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

Links
Moderators
Bucky Roberts Administrator