-1

I have read several SO posts that has the same questions as I do:

  • When I add a OnClickListener for a View (in this case an EditText) inside the Fragment class, the click event is never fired.
  • If I move the code away from the Fragment class and move it into the Activity instead, the click event is fired correctly.

The other SO questions ask the same question, but the solutions they have (if any) does not apply to my situation, from what I can tell. Therefore, I am asking this question "again".

First, the fragment_main.xml:

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="se.snapcode.lanstrafiken.MainActivityFragment">

    <LinearLayout
        android:focusable="true" android:focusableInTouchMode="true"
        android:layout_width="0px" android:layout_height="0px"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPostalAddress"
        android:ems="15"
        android:id="@+id/editTextFrom"
        android:hint="From"
        android:layout_alignParentEnd="true"
        android:focusableInTouchMode="false"
        android:clickable="true"
        android:layout_marginBottom="20sp"
        />
</LinearLayout>

And now the MainActivityFragment.java:

public class MainActivityFragment extends Fragment {

    public MainActivityFragment()  {    }

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

        EditText ed1 = (EditText) myView.findViewById(R.id.editTextFrom);
        ed1.setText("This is a test");
        ed1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Snapcode", "Test 1 2 3");
                Toast.makeText(v.getContext(), "test", Toast.LENGTH_LONG).show();
            }
        });

        EditText ed2 = (EditText)  myView.findViewById(R.id.editTextTo);
        ed1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        return myView;
    }
}

When I click the EditText, the Log.d(...) does not show in the logcat at all, and the Toast isn't displayed.

Now, if I remove everything that has to do with the EditText from the Fragment, and place it in the Activity instead, it works:

public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);

        EditText ed1 = (EditText) findViewById(R.id.editTextFrom);
        ed1.setText("Test again");
        ed1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Snapcode", "Test from Activity");
                Toast.makeText(MainActivity.this, "test", Toast.LENGTH_LONG).show();
            }
        });
    }

    // ...
}

Any ideas?

Sidenote

I think its really bad design that Android code isn't "encapsulated". As you can see above, I can access controls inside the Fragment from outside the Fragment, as if the controls are all in the same scope. The R.id.editTextFrom is available everywhere, which I find disturbing. It should only be available inside the Fragment, unless you explicitly make it "public". Compare with UserControls in .NET - there the UserControls are truly stand-alone.

I haven't tried, but what happens if two separate fragments have the same id for a control? Smells like how the web currently works (and what WebComponents are trying to solve).

halfer
  • 19,824
  • 17
  • 99
  • 186
Ted
  • 19,727
  • 35
  • 96
  • 154
  • onClick does not usable at EditText or TextView. What do you want to do with onClick? If it necceassary you will use onTouch. – Metehan Toksoy Jul 20 '15 at 19:59
  • @Ted I'm not able to reproduce the problem, using your Fragment code and the EditText in the Fragment's layout xml, I get the Toast. – Daniel Nugent Jul 20 '15 at 20:09
  • it worked for me if i put the code in the onActivitycreated method – has19 Jul 20 '15 at 20:12
  • @MetehanToksoy Not usable? Well, it is actually, if I place the code in the Activity and not in the Fragment =) – Ted Jul 21 '15 at 06:25
  • @DanielNugent Thats weird. You used basically the same code as I have above? and you get the toast eh? Very strange. I am running it on a device with Android 5.1. You? – Ted Jul 21 '15 at 06:26
  • @has19 Did you try my code as its written above by any chance? Did that work of fail for you? In onActivityCreated - how did you get the main view for "findviewById"-call? – Ted Jul 21 '15 at 06:27
  • @ted on 4.4.4, but for this it shouldn't matter. Yep, same code, in a Fragment, worked just fine for me. – Daniel Nugent Jul 21 '15 at 06:35
  • Interesting. And weird. Dunno how to crack this one... – Ted Jul 21 '15 at 08:47

1 Answers1

0

I found the problem. A very easy one, completely my fault:

enter image description here

So, yeah... My bad, nothing to see here, move right along =)

Ted
  • 19,727
  • 35
  • 96
  • 154