Reputation: 3783
I encounter some issue with getting the reference to a Texview inside a Fragment, and I really have no idea why.
Here is my layout.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">
<TextView
android:id="@+id/tvSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvLogout"
android:layout_below="@+id/tvSettings"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
And here is the Java Class
public class TabPersonnalSpace extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_personnal_space, container, false);
TextView tvSettings = (TextView)rootView.findViewById(R.id.tvSettings);
Log.d("Debug", tvSettings.getText().toString());
tvSettings.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Settings", Toast.LENGTH_SHORT);
}
});
return rootView;
}
}
Thing is : I get the text printed to the logcat when swipe
to the Fragment, and nothing happened when I click on the TextView.
Does anyone have a clue for me?
Upvotes: 0
Views: 88
Reputation: 644
implement onViewCreated like this :
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//instantiate tvSettings
TextView tvSettings = (TextView)rootView.findViewById(R.id.tvSettings);
}
Upvotes: 0
Reputation: 47807
You forget .show()
to display Toast
Toast.makeText(getActivity(), "Settings", Toast.LENGTH_SHORT).show();
Upvotes: 2