Advertisement
Guest User

Razvan question

a guest
Nov 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. ---SOLVED---
  2. the problem was I was passing the Application context,
  3. instead of the activity context.
  4. --- Original question ---
  5. How to programmatically add ImageView to -ROOT- default layout?
  6. (Without creating a new one)
  7.  
  8. Explanation : When starting a new project, you get the a default XML.
  9.  
  10. <?xml version="1.0" encoding="utf-8"?>
  11. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  12. xmlns:app="http://schemas.android.com/apk/res-auto"
  13. xmlns:tools="http://schemas.android.com/tools"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. tools:context=".MainActivity">
  17.  
  18. <TextView
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="Hello World!" />
  22.  
  23. </RelativeLayout>
  24.  
  25. To create a new ImageView I use
  26. ImageView imageView = new ImageView(context);
  27. imageView.setImageResource(R.drawable.somePicture);
  28. imageView.setLayoutParams(new RelativeLayout.LayoutParams(
  29. RelativeLayout.LayoutParams.WRAP_CONTENT,
  30. RelativeLayout.LayoutParams.WRAP_CONTENT));
  31.  
  32. Now.. in order to apply it to the Layout, i.e.
  33.  
  34. someLayout.addView(imageView);
  35.  
  36. I need to create that layout, and set it as
  37.  
  38. someActivity.setContentView(someLayout);
  39.  
  40. Problem is - I DO NOT want to create a NEW layout.
  41. In the GUI editor, I can just drag the imageView to the default layout.
  42. So, how can I programmatically access that default layout, and than use the addView on it?
  43.  
  44. P.S. -
  45. Tried naming the layout and accessing with R.id,
  46. Tried "getWindow().getDecorView().getRootView();"
  47. I really feel like I'm missing something very basic in my understanding, but, what ?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement