Reputation: 45
Sorry for my english but i'm new in this, and have a problem with my code cause i want add a button in my fragment for go to activity o another fragment, i dont know that if this is possible (fragment -> Activity) Please i need know what's the method for add this button.
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
public void IniciarH(View v)
{
Intent intent=new Intent(getActivity(), IniciarHome.class);
startActivity(intent);
}
}
and my .xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="159dp"
android:onClick="IniciarH"
android:src="@drawable/ic_home" />
Upvotes: 1
Views: 45
Reputation: 1062
add a clickListener to your Button
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
ImageButton imageButton = (ImageButton)rootView.findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(), IniciarHome.class);
startActivity(intent);
}
});
return rootView;
}
Upvotes: 1