Sithira Pathirana
Sithira Pathirana

Reputation: 608

Button click in Android Fragment does not work

activity_main.xml

<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="wrap_content"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/fragment1"
    android:name="sithi.test.fragmenttest.Fragment1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:onClick="btnClick1" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

ActivityMain.java

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

Fragment1.java

 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
 public class Fragment1 extends Fragment
 {
    TextView tv;
    @Override
    public void onStart() 
    {
    // TODO Auto-generated method stub
    super.onStart();
    tv=(TextView)getView().findViewById(R.id.textView1);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
    // TODO Auto-generated method stub
    //inflater.inflate(resource, root, attachToRoot);
    return inflater.inflate(R.layout.fragment1, container, false);
    }

    public void btnClick1(View view)
    {
    tv.setText("dsdsdasda");
    }


   }

I created xml files and classes like this but btnClick1() does not work in Android Fragment. It will getting an error when i clicking that button in the fragment. I have written that button click function inside the Fragment class.

Upvotes: 2

Views: 8809

Answers (3)

Engr. Meshach Koech
Engr. Meshach Koech

Reputation: 420

Since others have addressed setting onClick listener, let me add that if you have done so, and still getting the error check permissions required. For my case, the item on fragment was meant to open chat which requires microphone so it was not working until I added appropriate permissions in Manifest file.

Upvotes: 0

MaciejG&#243;rski
MaciejG&#243;rski

Reputation: 22232

You need to assign OnClickListener in fragment code to make it work. See Snicolas answer for the "why".

Upvotes: 2

Snicolas
Snicolas

Reputation: 38168

The way XML onClick is implemented is directed to Activities, not Fragments. The activity should own the btnClick1 method, not a fragment.

Upvotes: 10

Related Questions