1

I try insert image on API 21 and that not working, but on API 26 everything working. Where my mistake or what is missing here?

public class MainActivity extends AppCompatActivity {
ImageView img1,img2,img3,img4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    img1 = (ImageView) findViewById(R.id.img1);
    img2 = (ImageView) findViewById(R.id.img2);
    img3 = (ImageView) findViewById(R.id.img3);
    img4 = (ImageView) findViewById(R.id.img4);

    //setResource
//        if(img1 != null) img1.setImageResource(R.drawable.china); //Вылетает

    //setDrawable
    if(img2 != null) `img2.setImageDrawable(Drawable.createFromPath("/res/drawable/tokyo1.png"));`

    //setBitmap
    if(img3 != null) img3.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.tokyo2));

    //Picasso(local)
    if(img4 != null) {
        Picasso.get()
                .load(R.drawable.china)
                .into(img4);
    }

    //Picasso(URI)
    if(img1 != null) {
        Picasso.get()
                .load("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTmG5K1tMdQ4W3G1WlZSfSWfL6jo8E9rS4NTHflXoT1CsF2Na_i")
                .into(img1);
    }
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    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="com.example.kiril.imgtest4.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/img1"
                android:layout_width="1dp"
                android:layout_height="150dp"
                android:layout_weight="1"
                android:background="@android:color/holo_orange_dark"/>
            <ImageView
                android:id="@+id/img2"
                android:layout_width="1dp"
                android:layout_height="150dp"
                android:layout_weight="1"       
                android:background="@android:color/holo_blue_light"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/img3"
                android:layout_width="1dp"
                android:layout_height="150dp"
                android:layout_weight="1"

                android:background="@android:color/holo_green_light"/>
            <ImageView
                android:id="@+id/img4"
                android:layout_width="1dp"
                android:layout_height="150dp"
                android:layout_weight="1"
                android:background="@android:color/holo_red_dark"/>
        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>
K.Os
  • 5,123
  • 8
  • 40
  • 95

2 Answers2

1

The problem is with the layout_width tag in your xml file.

You are specifying the layout_width size as 1 dp. Make it large enough to display the image in the ImageView

OR

use layout_width="wrap_content" to display the image properly without any adjustments in the width of the image.

So, in conclusion just replace the line android:layout_width="1dp" with android:layout_width="wrap_content" or android:layout_width="any size in dp"

Hope this helps !!

HarshitMadhav
  • 4,769
  • 6
  • 36
  • 45
0

Ok Although i have no idea what you are trying to achieve here . I am pointing out some steps . Use Unit 0dp with layout_weight according to orientation. So your layout should look like this.

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
        <ImageView
            android:id="@+id/img2"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/img3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
        <ImageView
            android:id="@+id/img4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
    </LinearLayout>
</LinearLayout>

And you can set image to ImageView in several way.

imageView.setImageResource(R.drawable.ic_launcher);
    imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_launcher));
    Picasso.with(context).load(R.drawable.ic_launcher).into(imageView);

Use android:scaleType attribute in xml for if you set image with src. Also no need to check if(img2 != null). Also read what-is-the-difference-between-src-and-background-of-imageview.

ADM
  • 20,406
  • 11
  • 52
  • 83