Filip
Filip

Reputation: 52

Add imageview to layout

I want to create image view from code and add to layout...But l don't use in-code setup layout.I have RelativeLayout in xml and use that setContentView(R.layout.activity_game); How add imageview to layout?

Upvotes: 2

Views: 5249

Answers (2)

Stefano Munarini
Stefano Munarini

Reputation: 2717

The XML should be:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:id="@+id/relativeblablabla"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

...........YOUR CODE HERE............

</RelativeLayout>

The code (inside the onCreate):

RelativeLayout rl = (RelativeLayout)findViewById(R.id.relativeblablabla);
ImageView imageView = new ImageView (this);
iv.setImageResource(R.drawable.YOUR_IMAGE);
rl.addView(imageView);

Upvotes: 0

Boris Mocialov
Boris Mocialov

Reputation: 3489

RelativeLayout rl = (RelativeLayout)findViewById(R.id.id_of_your_relative_layout_here);
ImageView iv = new ImageView(this)
iv.setImageResource(R.drawable.some_drawable_of_yours); //or iv.setImageDrawable(getResources().getDrawable(R.drawable.some_drawable_of_yours));
rl.addView(iv);

Upvotes: 7

Related Questions