1. Download our Official Android App: Forums for Android!

setOnClickListener how to do it in Android Studio

Discussion in 'Android Apps & Games' started by DaoorSamer25, Jan 10, 2019 at 6:34 PM.

  1. DaoorSamer25

    DaoorSamer25 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Thursday

    Thursday
    4
    0
    5
    Male
    Egypt
    am having a simple programmatic problem with my Android studio
    the problem is that i can't type
    Code (Text):
    1. btn.setOnClickListener (new View.OnClickListener()
    2. ){
    3. [USER=1021285]@Override[/USER]
    4. onClick(
    5. //code here
    6. )
    7.  
    }
    can any cs student or fresh Graduate reply me with what needs to be said to android studio in order to execute that code i know the code but cant write it as it give me cannot resolve symbol 'setOnClickListener'
     

    Advertisement

    #1 DaoorSamer25, Jan 10, 2019 at 6:34 PM
    Last edited: Jan 10, 2019 at 10:27 PM
  2. GameTheory

    GameTheory Android Expert
    Rank:
     #50
    Points:
    433
    Posts:
    3,525
    Joined:
    Jul 23, 2012

    Jul 23, 2012
    3,525
    3,058
    433
    Male
    ???
    @DaoorSamer25
    Should be like this...

    Code (Java):
    1. btn.setOnClickListener(new View.OnClickListener() {
    2.     @Override
    3.     public void onClick(View view) {
    4.         //executing code goes here
    5.     }
    6. });
     
  3. DaoorSamer25

    DaoorSamer25 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Thursday

    Thursday
    4
    0
    5
    Male
    Egypt
    you are true it should be like so exactly but when i type btn.setOnClickListener it tells me cannot resolve symbol setOnClickListener my question is what is required to be done in order to type this snippet you typed
     
  4. GameTheory

    GameTheory Android Expert
    Rank:
     #50
    Points:
    433
    Posts:
    3,525
    Joined:
    Jul 23, 2012

    Jul 23, 2012
    3,525
    3,058
    433
    Male
    ???
    Did you create a layout for your button in the layout xml file for the activity it is in?
    Did you define your button in java with the id of your button from the layout xml file like this...

    Code (Java):
    1. Button btn = findViewById(R.id.YOUR_BUTTON_ID_HERE);
    It's hard to help without seeing any code.
     
  5. DaoorSamer25

    DaoorSamer25 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Thursday

    Thursday
    4
    0
    5
    Male
    Egypt
    Code (Text):
    1.  Button bNext=(Button)findViewById(R.id.btn);
    2.     bNext.setOnClickListener(new View.OnClickListener(){
    3.             @Override
    4.             public void onClick(View v){
    5.  
    6.             )
    this is the code am working on but can't recognize setOnClickListener and can't recognize Overrides also
     
    #5 DaoorSamer25, Jan 10, 2019 at 10:13 PM
    Last edited: Jan 10, 2019 at 10:30 PM
  6. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    8,096
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    8,096
    11,912
    1,988
    Male
    Software developer
    South West of England
    Please post the whole class, including any import statements.
     

Share This Page

Loading...