[Q]Dealing with fragments

Search This thread

arpitkh96

Senior Member
Feb 17, 2012
1,156
645
27
panipat
Here is the xml
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
android:layout_weight="1"
/>
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="40.0dip"
android:background="#ffffffff"
android:id="@+id/progaction"
android:layout_weight="0.2"
android:orientation="horizontal" 
android:gravity="center_vertical"
android:layout_below="@id/container"
android:layout_alignParentBottom="true">
<TextView
android:gravity="center"
android:text="Level"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<SeekBar
android:gravity="center"
android:layout_weight="1"
android:id="@+id/seek"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
/><TextView
android:gravity="center"
android:text="1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/prog"
/>
</LinearLayout>
</LinearLayout>
Framelayout with id=container contains the fragment used in the activity.
Now the problem is that how to can i get the value of the textView(id=prog) in a particular fragment.i am not able to do it
Error says that "this method cant be used for static context" for the following line
Code:
TextView prog=(TextView) findViewById(R.id.prog);
So please help me to get the value of textView in a fragment

Sent from my GT-S5570 using XDA Premium 4 mobile app
 

danelab

Member
Mar 29, 2014
15
5
I think your need to call getActivity() first and after this perform activity.findViewById(R.id.prog):
HTML:
Activity a = getActivity();
TextView prog=(TextView) a.findViewById(R.id.prog);
 
Last edited:
  • Like
Reactions: arpitkh96

deanwray

Senior Member
Apr 2, 2006
1,145
427
www.deanwray.co.uk
well maybe the easiest way is to hold an instance of the textView in the fragment code, then have a public method to return that instance, so that in the main fragment activity you simply get an instance to the fragment then call the method ?
 
  • Like
Reactions: arpitkh96

arpitkh96

Senior Member
Feb 17, 2012
1,156
645
27
panipat
I think your need to call getActivity() first and after this perform activity.findViewById(R.id.prog):
HTML:
Activity a = getActivity();
TextView prog=(TextView) a.findViewById(R.id.prog);

Thanks.it worked






This is to be used if textView is present in the XML inflated by the fragment itself .but my textView is in XML of mainactivity.
Anyways thanks for the answer.I got the solution

Sent from my GT-S5570 using XDA Premium 4 mobile app
 

Top Liked Posts