Imageview dynamically added to relativelayout

relativelayout android programmatically
dynamic imageview android

I have a simple Android application. I want to start the camera by creating an intent and then take photo and set the photo in an imageview which is dynamically added to the relative layout. The relative layout is static inside a layout.

I have been trying the following code, but it doesn't work. If I debug I can see the image taken from the camerai in the imageview.

public void setImage(Uri imgUri){

            DisplayMetrics metrics = new DisplayMetrics();

            int width = metrics.widthPixels;
            int height = metrics.heightPixels;

            ImageView imgView = new ImageView(this);


            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);


            Button btnTest = new Button(this);

            Toast.makeText(getApplicationContext(),"Picture will be shown now", Toast.LENGTH_LONG).show();

        }catch(Exception e){
            Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();

P.S. I can take the photo and save it to the picture folder (there is no R.drawable.image because i create it dynamically by creating an Uri to that file. The problem is set the imageview from the uri. There are 4 ways to set the image to the imageview. I already tried all of them. When i debug right after setting the image i can see the bitmap of the imageview and it is the picture i took by the camera.

try to call this at the end:


Just in case, make sure imgHolder is visible.

Dynamic RelativeLayout Params Programmatically Example In , In this example we created dynamic view's (ImageView and Button) In this step we open xml file and then add RelativeLayout as main layout in which we add  It looks like you might be adding the view to the root of the layout xml instead of the RelativeLayout. You could try: RelativeLayout layout = (RelativeLayout)findViewById(; layout.addView(addIcon());

Try this:

RelativeLayout.LayoutParams imParams = 
    new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    ImageView imLove = new ImageView(context);

Dynamic ImageView in Kotlin, //accessing our relative layout from activity_main.xml val layout = findViewById(R.​id.layout) // Add ImageView to LinearLayout layout?.addView(imageView)  In android programming, we can dynamically create an ImageView and add it to a layout programmatically using the example code below. Please note that both the ImageView and LinearLayout are dynamically created. This might be different from your use case but in generally it will give you an insight on how to solve the problem.

First, you need to declare an Intent that retrieves a photo taken by the Camera. Don't forget the permissions for the application. For example, this code works well for such an action :

PackageManager pm = v.getContext().getPackageManager();

if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    if (ContextCompat.checkSelfPermission(v.getContext(), Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
        // Permission is not granted
        Log.d("No permission", "You need to enable the CAMERA permissions to access to this feature");
                            new String[]{Manifest.permission.CAMERA},REQUEST_IMAGE_CAPTURE);
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    Snackbar.make(v,"No CAMERA : Your phone doesn't have the correct feature for this action", 1000);

Then, you need to get the result from the intent by overriding the method onActivityResult() :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        photo = (Bitmap) extras.get("data");

How to dynamically set the width/height of an image view , When I created the predefined Image View I simply dragged the sides to Id.​mainLinearLayout); //Parameter //Create relative layout var param  Below is the example of Dynamic RelativeLayout aka programmatically. In this example we created dynamic view’s (ImageView and Button) programmatically in Java class. For creating views programmatically firstly we define the Relative Layout in our xml file and then add the code in java class to create ImageView and a Button one after another.

How to add an ImageView to an empty layout using Java in Android , Add the ImageView to your layout Example: [code]LinearLayout layout = (​LinearLayout) Create an Imageview Dynamically and addView to your layout. (1 reply) What I am trying to do is create *N* (in this case 9) copies of the ImageView object, place each of them at different coordinates on the layout, and give each its own individual identifier.

android.widget.ImageView.setId java code examples, RelativeLayout rl = (RelativeLayout) findViewById(; Finding child views from parent view created programmatically in Android. Try this code. adjustViewBounds attribute makes the ImageView the same size as image that you put in it. <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" /> If you need specific width or height change the wrap_content value.

android.widget.RelativeLayout.setLayoutParams java code , CENTER_IN_PARENT); ImageView mImage = new ImageView(this); mImage. Adding fields dynamically to a layout in Android. LinearLayout.LayoutParams  Hello Readers! In this post, we are going to learn how to create and use android imageView programmatically in any android application. We will also learn to add imageView in linearLayout programmatically in any application.

  • Possible duplicate of ImageView.setImageURI does NOT work when trying to assign a R.drawable.X Uri
  • Hi, thanks for your answer. The imgHolder is visible and the button is added. The imageview also has a background color, but the image itself isn't being shown.
  • Hi there, i already have the feature for taking photo from camera. The photo is saved in the picture folder. In my code after i set the imageview with the Uri i can watch in the image by evaluating the image view one step after setting the image. I also tried your code, but inside the onActivityResult the data is null.