Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[Q]Dealing with fragments

OP arpitkh96

29th March 2014, 12:43 PM   |  #1  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,127 posts
Join Date:Joined: Feb 2012
Donate to Me
More
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
29th March 2014, 10:01 PM   |  #2  
Junior Member
Thanks Meter: 5
 
15 posts
Join Date:Joined: 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);
Last edited by danelab; 29th March 2014 at 10:11 PM.
The Following User Says Thank You to danelab For This Useful Post: [ View ]
29th March 2014, 11:23 PM   |  #3  
deanwray's Avatar
Senior Member
Thanks Meter: 429
 
1,145 posts
Join Date:Joined: 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: [ View ]
30th March 2014, 08:43 AM   |  #5  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,127 posts
Join Date:Joined: Feb 2012
Donate to Me
More
Quote:
Originally Posted by danelab

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

Have you tried this one? http://stackoverflow.com/questions/6...agment-android

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
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes