I'm trying to get a text view to link to a site, but it won't find the id from xml in android. Any help would be great. Here's the code
It wont find the R.id.textviewlink
public class Fragment_2 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_2, null);
TextView t2 = (TextView) getView().findViewById(R.id.textviewlink);
t2.setMovementMethod(LinkMovementMethod.getInstance());
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:background="@drawable/backgroundw">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textviewlink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Free PDF Download"
android:linksClickable="true"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
That is because you have already returned your inflated layout before finding the view item. There are two options.
First, the inflate() method always requires three arguments:
inflater.inflate(R.layout.file_name, container, false);
Put the following in onStart
:
TextView t2 = (TextView) getView().findViewById(R.id.textviewlink);
t2.setMovementMethod(LinkMovementMethod.getInstance());
Or just use this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_2, container, false); //don't forget the third argument here
TextView t2 = (TextView) v.findViewById(R.id.textviewlink);
t2.setMovementMethod(LinkMovementMethod.getInstance());
return v;
}