Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOnClickListener(new OnClickListener() not working in Fragment

I am working on fragements of NAvigation Drawer .Andi copy the code of Activity to fragments to execute in fragment . I have done that , layout is visible , but none of the button action works under that Fragment Activity .

Can you tell me what i did wrong ?

FragmentTwo.java

public class FragmentTwo   extends Fragment {

    Button blog,fb,twitter,mail;
    ImageView dateimg;
    private boolean snet;


    public FragmentTwo()
    {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.contact_lay,container, false);


        System.out.println("Kaks7");
        dateimg = (ImageView) getActivity().findViewById(R.id.img);
        blog= (Button) getActivity().findViewById(R.id.btnGplus);
        fb = (Button) getActivity().findViewById(R.id.btnFb);
        twitter = (Button)getActivity().findViewById(R.id.btnTwitter);
        mail = (Button)getActivity().findViewById(R.id.btnMail);
        System.out.println("K17");
        new LongOperation().execute();


        return view;
    }



    private class LongOperation extends AsyncTask<Void, Void, Void> {



        protected void onPreExecute() {



        }


        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub


            return null;
        }


        @Override
        protected void onPostExecute(Void result) {

            try{

                 fb.setOnClickListener(new OnClickListener()
                    {

                            public void onClick(View vb) 
                             {  
                                try{
                                    System.out.println("7");
                                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/10000003242252"));
                                    startActivity(intent);

                                    }catch(Exception e){

                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://m.facebook.com/facebook")));
                                    }

                                }

                    });

                twitter.setOnClickListener(new OnClickListener()
                {
                        public void onClick(View vc)
                         {  
                            try {

                                  Intent intent = new Intent(Intent.ACTION_VIEW,
                                        Uri.parse("twitter://user?screen_name=dhoom"));
                                    startActivity(intent);

                                    }catch (Exception e) {
                                        startActivity(new Intent(Intent.ACTION_VIEW,
                                             Uri.parse("https://mobile.twitter.com/#!/dhoom"))); 
                                    } 

                            }

                });

                blog.setOnClickListener(new OnClickListener()
                {
                        public void onClick(View va)
                         {  System.out.println("7s");
                            try {
                                System.out.println("5s");
                                String url = "http://google.com";
                                Intent i = new Intent(Intent.ACTION_VIEW);
                                i.setData(Uri.parse(url));
                                startActivity(i);


                            }

                            catch(Exception e){ 
                                System.out.println("6s");
                                            startActivity(new Intent(Intent.ACTION_VIEW,
                                                 Uri.parse("http://www.google.com"))); 
                            } 

                        }

                });


                mail.setOnClickListener(new OnClickListener()
                {
                        public void onClick(View va)
                         {  
                            try {
                                Intent send = new Intent(Intent.ACTION_SENDTO);
                                String uriText = "mailto:" + Uri.encode("[email protected]") + 
                                          "?subject=" + Uri.encode("the subject") + 
                                          "&body=" + Uri.encode("the body of the message");
                                Uri uri = Uri.parse(uriText);

                                send.setData(uri);
                                startActivity(Intent.createChooser(send, "Send mail..."));
                            }

                            catch(Exception e){ 
                                Toast.makeText(getActivity().getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                            //  this.progressDialog.dismiss();
                            } 
                        }

                });

                dateimg.setOnClickListener(new OnClickListener()
                {
                        public void onClick(View va)
                         {  
                            if(snet==false)
                            {
                            soundstart();
                            snet=true;
                            }
                        }

                });





            }

            catch(Exception e){ 
                //Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();

            } 
        }



    }


    public void soundstart()
    {
        // TODO Auto-generated method stub
        MediaPlayer mplayer =MediaPlayer.create(getActivity(),R.raw.contact_sound);
        mplayer.start();

    }

}

Activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:background="@drawable/abc">

    <RelativeLayout 
    android:layout_width="wrap_content"
    android:id="@+id/text"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:paddingTop="-30dp"
            android:text="© iamrajkaran || Raj Karan"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:textStyle="bold" />

    </RelativeLayout> 
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/linearlayout_bg"
        android:orientation="vertical"
        android:layout_marginTop="30dp"
        android:padding="7dp" >

        <Button
            android:id="@+id/btnGplus"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="48dp"
            android:layout_margin="4dp"
            android:background="@drawable/layers_blog_button_bg"
            android:padding="10dp"
            android:text="Know me on my blog" />
        <Button
            android:id="@+id/btnFb"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:background="@drawable/layers_fb_button_bg"
            android:padding="10dp"
            android:text="Join me on facebook" />
        <Button
            android:id="@+id/btnTwitter"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:background="@drawable/layers_twitter_button_bg"
            android:padding="10dp"
            android:text="Follow me on twitter" />
        <Button
            android:id="@+id/btnMail"
            style="@style/ButtonText"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_margin="4dp"
            android:background="@drawable/layers_mail_button_bg"
            android:padding="10dp"
            android:text="Mail Your Feedback" />


        </LinearLayout>


    <RelativeLayout
    android:layout_width="fill_parent"
    android:id="@+id/texwt"
    android:orientation="vertical"
    android:gravity="bottom"
    android:layout_gravity="center"
    android:layout_marginTop="240dp"
    android:layout_height="wrap_content"  >
        <ImageView
            android:id="@+id/img"
            android:layout_width="180dp"
            android:layout_height="180dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:paddingLeft="30dp"
              android:paddingRight="0dp"
            android:src="@drawable/ic_form" />

    </RelativeLayout>
    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:layout_marginTop="40dp"
        android:text="Touch Me"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF"
        android:textSize="22sp"
        android:textStyle="bold" />

</RelativeLayout>**
like image 805
raj Avatar asked Dec 17 '14 12:12

raj


People also ask

What is setOnClickListener new view OnClickListener ()?

setOnClickListener(this); means that you want to assign listener for your Button “on this instance” this instance represents OnClickListener and for this reason your class have to implement that interface. If you have more than one button click event, you can use switch case to identify which button is clicked.

Why my setOnClickListener is not working?

You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener() . Save this answer.

What does set OnClickListener do?

OnClickListener and wires the listener to the button using setOnClickListener(View. OnClickListener) . As a result, the system executes the code you write in onClick(View) after the user presses the button. The system executes the code in onClick on the main thread.

Can we set OnClickListener on TextView?

In Android, TextView is a child class of View, and hence can use the method setOnClickListener() on the object of TextView. In this tutorial, we will learn how to set OnClickListener for TextView in Kotlin file, with the help of an example.


1 Answers

use the view instead of using the getactivity(),your get the view from inflate view,so use the view

dateimg = (ImageView) view.findViewById(R.id.img);

instead of using this,

dateimg = (ImageView) getActivity().findViewById(R.id.img);

change likewise for all widgets view.

like image 107
prakash Avatar answered Nov 15 '22 12:11

prakash