LaraFlow
LaraFlow

Reputation: 392

onClick inside class that implements View.OnclickListener Never being Called

I have read answers that are similar to this one. But that not solved the problem. Here is problem scenario. MainActivity contains this Fragment

public class HomeFragment extends Fragment implements View.OnClickListener{  

  @Override  
  public void onClick(View view){
      Log.v("TAG",""+view.getId());  //No log message shown
      switch(view.getId()){ 
         .....   
      }  
   }   
}

Fragment XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/coordinatorLayout"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">

   <RelativeLayout
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:background="@drawable/app_background">
             <ImageView
                   android:id="@+id/imageview"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:src="@drawable/ic_man" />
            <TextView
                   android:id="@+id/textview"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:layout_marginLeft="5.0dip"
                   android:layout_toRightOf="@id/imageview"
                   android:text="@string/personname"
                   android:textColor="@color/textcolor"
                   android:textSize="25.0sp" />
    </RelativeLayout>
</LinearLayout>

According to my understanding for Onclicklistener implementation any click on anywhere within view should out log message(here).No nested view on scrollview used.

Upvotes: 0

Views: 433

Answers (2)

Satan Pandeya
Satan Pandeya

Reputation: 3815

I think you forget to use: myButton.setOnClickListener(this);

public class fragmentOne extends Fragment implements OnClickListener {
    Button myButton;

    @Override
    public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
        View myView = inflater.inflate(R.layout.fragment_home, container, false);
        myButton = (Button) myView.findViewById(R.id.myButton);
        myButton.setOnClickListener(this);
        return myView;
    }

    @Override
    public void onClick(View v) {
        // implements your things
    }
}

The button is inside this layout fragment_home.xml.

<Button
      android:id="@+id/myButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />

Upvotes: 2

Sagar Aghara
Sagar Aghara

Reputation: 640

Add below line in your onCreateView()

YourButtonName = (Button) myView.findViewById(R.id.YourButtonName);
YourButtonName.setOnClickListener(this);

and your onClick() like this.

    @Override  
      public void onClick(View view){
          Log.v("TAG",""+view.getId());  //No log message shown
          switch(view.getId()){ 
// Add your View ID here.
            case R.id.textview:
    Toast.makeText(this, "TextView Clicked", Toast.LENGTH_SHORT).show();
    break;   
          }  
       }

hope this will help you..:)

Upvotes: 1

Related Questions