-Edited to clarify duplication-
Start a new android studio project, empty activity.
copy the layout default xml to a question.
mark it as code.
get the error, even though the code seems to be properly formatted. I've read the rules, the advanced help, the "how to post" etc. What is wrong with my question?
This is the text I'm trying to post:
How to programmatically add ImageView to -ROOT- default layout?
(Without creating a new one)
Explanation : When starting a new project, you get the a default XML.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
To create a new ImageView I use
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.somePicture);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
Now.. in order to apply it to the Layout, i.e.
someLayout.addView(imageView);
I need to create that layout, and set it as
someActivity.setContentView(someLayout);
Problem is - I DO NOT want to create a NEW layout.
In the GUI editor, I can just drag the imageView to the default layout.
So, how can I programmatically access that default layout, and than use the addView on it?
P.S. -
Tried naming the layout and accessing with R.id,
Tried "getWindow().getDecorView().getRootView();"
I really feel like I'm missing something very basic in my understanding, but, what ?