PhillipOReilly
PhillipOReilly

Reputation: 609

OnClickListener Does not Respond to Clicks in Fragment

I am transferring an old program over to a Tabs and Fragments implementation. The user inputs an Id for a search and then hits Submit. However my "Submit Button" does nothing.

I have tried several alternative coding variants but get same result - dead air!

Here is XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/setUpAthletesTab"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
     android:background="#CCFFFF" >

    <TextView
        android:id="@+id/instructionText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/instructionText"
    android:textColor="#FF0000"
    android:textAppearance="?android:attr/textAppearanceMedium"
     />

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:stretchColumns="*" >

        <EditText
            android:id="@+id/editTextAthlete0"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:hint="@string/hintText"
            android:inputType="number" />

        <TextView
            android:id="@+id/textAthleteName0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:hint="@string/athleteHintName0"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

Here is my Tab / Fragment

public class Tab3Fragment extends Fragment {

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /* Begin Insertion */
        LayoutInflater myInflater = inflater;
        ViewGroup myContainer = container;
        if(inflater == null) myInflater = inflater;
        if(container == null) myContainer = container;
        Log.d(APP_TAG,"At line 50 of Tab3Fragment myInflater = " + myInflater + " and myContainer = " + myContainer);
        getAthleteInfoFromExistingDatabase(myInflater,myContainer);

        LinearLayout tab3LinearLayout = (LinearLayout) inflater.inflate(R.layout.tab_frag3_layout, container, false);

        View mySubmitButton = tab3LinearLayout.findViewById(R.id.submitButton);
        ((Button) mySubmitButton).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(Tab3Fragment.this.getActivity().getBaseContext(),
                        "clicked on Submit Button", Toast.LENGTH_LONG).show();

                Intent intent = new Intent();
                intent.setClass(getActivity(), RetrieveAthleteData.class);
                startActivity(intent);
            }
        });

        return (LinearLayout)inflater.inflate(R.layout.tab_frag3_layout, container, false);
    }

Neither the Toast box nor the Intent fire off when I hit submit.

Thanks for looking at my question.

Problem solved! Thanks Dirk for suggesting name change to all lower case.

Upvotes: 3

Views: 7045

Answers (2)

Graeme
Graeme

Reputation: 25864

You're inflating and returning a brand spanking new set of views here:

return (LinearLayout)inflater.inflate(R.layout.tab_frag3_layout, container, false);

you should be returning the inflated view's that you acted upon:

return tab3LinearLayout;

Upvotes: 1

Dirk J&#228;ckel
Dirk J&#228;ckel

Reputation: 3025

In your xml is no Button with the id "submitButton".

You should only use lowercase and therefore give it the id "@+id/submit_button"

Although I could not find a source that camel case ids are not allowed. Even Google uses camel case ids in their examples sometimes.

Upvotes: 1

Related Questions