Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,772,899 Members 48,890 Now Online
XDA Developers Android and Mobile Development Forum

[Q]Dealing with fragments

Tip us?
 
arpitkh96
Old
#1  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 376
Posts: 1,087
Join Date: Feb 2012
Location: panipat
Default [Q]Dealing with fragments

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
Old
(Last edited by danelab; 29th March 2014 at 09:11 PM.)
#2  
Junior Member
Thanks Meter 5
Posts: 15
Join Date: Mar 2014
I think your need to call getActivity() first and after this perform activity.findViewById(R.id.prog):
HTML Code:
Activity a = getActivity();
TextView prog=(TextView) a.findViewById(R.id.prog);
The Following User Says Thank You to danelab For This Useful Post: [ Click to Expand ]
 
deanwray
Old
#3  
deanwray's Avatar
Senior Member
Thanks Meter 417
Posts: 1,120
Join Date: Apr 2006

 
DONATE TO ME
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 ?
The Following User Says Thank You to deanwray For This Useful Post: [ Click to Expand ]
 
arpitkh96
Old
#5  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 376
Posts: 1,087
Join Date: Feb 2012
Location: panipat
Quote:
Originally Posted by danelab View Post
I think your need to call getActivity() first and after this perform activity.findViewById(R.id.prog):
HTML Code:
Activity a = getActivity();
TextView prog=(TextView) a.findViewById(R.id.prog);
Thanks.it worked





Quote:
Originally Posted by nikwen View Post
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes