82327

How to get a view from a fragment layout in android?

Question:

I have an xml like this

activity_loginscreen.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="android.arin.LoginScreen" tools:ignore="MergeRootFrame" />

and then gragment_loginscreen.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" style="@style/LoginView" tools:context="android.arin.LoginScreen$PlaceholderFragment" > <ImageView android:id="@+id/bubbles" android:contentDescription="@string/bubbles_cd" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:alpha=".75" android:src="@drawable/bubbles" /> </RelativeLayout>

In my java file I have

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_screen); if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } ImageView bubbles = (ImageView) findViewById(R.id.bubbles); }

But bubbles ends up being null because it can't find it because its looking in the activity xml one, but really the imageview is in the fragment one, how can I get it to look there?

Thanks

Answer1:

Make your PlaceholderFragment fragment onCreateView(....) like

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.gragment_loginscreen, container, false); ImageView bubbles = (ImageView)view.findViewById(R.id.bubbles); return view; }

and used getActivity() as a Context in Fragment like

Animation animContentUp = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up_service);

Answer2:

Place the imageView in your PlaceholderFragment onCreateView and in the PlaceholderFragment inflate the layout gragment_loginscreen which will generate a view..

<strong>example:</strong>

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.gragment_loginscreen, container, false); ImageView bubbles = (ImageView)view.findViewById(R.id.bubbles); return view; }

Recommend

  • How to get a view from a fragment layout in android?
  • nav header not displaying textvalues from MainActivity in kotlin
  • Layout Weight warning Nested weight bad performance
  • Android - Center Textview in ImageView of Relative Layout
  • Unable to load url in webview (android)
  • Android Swipe View help needed
  • Android TalkBack reading phone numbers in Webview incorrectly
  • highcharts v3.0Beta bubblechart not rendering. Possible bug?
  • ExpandableList View not using ExpandableListActivity
  • ion-infinite-scroll fires when scrolling up - Ionic
  • JS Events that don't bubble (progress, loadedmetadata, etc)
  • No implementation of GestureDetector will work on android app
  • No implementation of GestureDetector will work on android app
  • Find and Replace using patterns in Xcode 8
  • No resource found that matches the given name
  • TextView not updating when deleting record in custom adapter
  • Knowing if a previous handler for an event returned false. defaultPrevented in IE < 9?
  • how to passing listview images passing another activity [closed]
  • didSelectRowAtIndexPath method at PFQueryTableViewController
  • Facebook SDK for iOS: FBSDKShareDialog is not shown
  • Android how to set a transparent image resource of a view? or Remove the image?
  • How to get a android ListView item selector to use state_pressed
  • Picasso Taking time to load images
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • Android Heatmap on canvas or ImageView