0

I have set the onClick property of an EditText which is located in a fragment:

           <EditText android:id="@+id/edittext1" android:layout_width="wrap_content"
                android:layout_height="wrap_content"  android:inputType="none" android:maxLines="1"
                android:singleLine="true" android:layout_marginTop="16dp"
                android:focusable="false"
                android:longClickable="false"
                android:clickable="true"
                android:onClick="doSomething"
                android:cursorVisible="false"
                android:editable="false">

Then in the fragment class I have:

public void doSomething(View view) {
    //show dialogfragment...
}

But the method doSomething is grayed out and I get the warning 'Method doSomething is never used'.

Note: This code was originally in an activity and was working fine.

Is there another way to handle onClick in fragments?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263

4 Answers4

1

first initialize an EditText instance in the top of your fragment

EditText et;

then in your onCreateView() add:

 etEmployee=(EditText)rootView.findViewById(R.id.edittext1);

then implement your onClickListener()

    et.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       //what ever you need to do goes here
    }

});

Note: in fragments you have to refer to the inflatedView to be able to access your editText in this case rootView.

also the code you are using doesn't work becouse here you are using fragments and using onClick attribute in your xml would only make you able to use it in the MainActivity that contain your fragment.

hope this will help.

Ahmad Sanie
  • 3,678
  • 2
  • 21
  • 56
0

You can always try to add a OnClickListener to the EditText if it does not matter to you in which way the function gets called.

edittext1 = (EditText) view.findViewById(R.id.edittext1);
edittext1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        doSomething();
    }

});
jbrulmans
  • 975
  • 1
  • 11
  • 32
  • 1
    if he is initializing the onClickListener in the fragment this won't work becouse you are not refering to the view, you have to use the returned view. findViewById – Ahmad Sanie Jun 13 '15 at 11:55
0

If you add android:onClick="doSomething" to your fragment then method will be invoked in your activity but not in Activity. If you want the callback in your fragment add through pragmatically.

   edittext.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Your code.
    }

});
Kartheek
  • 7,104
  • 3
  • 30
  • 44
0

In your layout XML try specifying a tools:context="com.mypackage.path.MyFragment". Don't know if it'll fix it for 'onClick', but it has fixed similar issues for me in the past.

This goes in the top most 'ViewGroup' in your layout file, example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.mypackage.path.MyFragment" />
Ali
  • 12,354
  • 9
  • 54
  • 83
  • this is new, i've been developing android apps for almost 3 years and never used this! so could you please give an example "when to use" tools:context and what does it actually do? would really appreciated it. thanks – Ahmad Sanie Jun 13 '15 at 12:14
  • From [most of what I read](http://stackoverflow.com/a/11110969/611600), it should only be used by the tools and not actually used programmatically in this manner. However, it seems similar stuff is planned for the future. It's not really new though, if you start a blank project you'll see the tools context included. – Ali Jun 14 '15 at 02:43