PNG not showing in ImageView - android

I have a large .png and i want to show it in a Splash screen, but the image is not showing. The xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.infaplic.lpi.activities.SplashActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"/>
And the code of the Activity:
public class SplashActivity extends Activity {
ImageView imagen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
imagen=(ImageView)findViewById(R.id.imageView2);
imagen.setImageDrawable(getResources().getDrawable(R.drawable.pantalla_arranque));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this, NextActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, 1000);
}
}
This way, the image is not showing. I have tried with android:src: in the xml, but it doesn't work.
The image is very large. Do I need to resize it before putting it in the ImageView? If not, why is the image not showing?
Thank you.
EDIT: The PNG is 1080x1920

In my case I was using a image with file name like "go-back-filename.png". And xml file is like
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/go-back-icon"/>
After i changed the file name to "go_back_icon" it worked fine.
Hope this helps someone.

Since you write that your image is very large I would guess it is too large. I have a bug in one of my apps there I generate an image which is bigger then 2048 pixels. Some devices does not support such big images I would guess that you run into a similar problem.
If that is some background image reduce the image size, a background image is not so important that each pixel on the screen has to own a image pixel.

Add Below this. Please read Options for scaling the bounds of an image to the bounds of this view.ImageView.ScaleType in your ImageView
Use This
imagen.setImageResource(R.drawable.pantalla_arranque);
Then
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
android:adjustViewBounds : Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
Must be a boolean value, either "true" or "false".
Please Check demo ImageView scaleType Samples . I hope it helps you

Change your ImageView to this :
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"
android:scaleType="fitXY"
android:adjustViewBounds="false"/>
If it's not working try to add this on your manifest (inside your Activity) android:hardwareAccelerated="false"
EDIT
I was looking for the answer, but I thought that better try it by my self... and I've created a sample project where I use an ImageView 1080x1920 and I was testing since I've found that I've I put 3000 miliseconds it shows up... wiht 1000 miliseconds won't show up if you don't rescale it.
By the way getDrawable() is deprecated I recomend to you use :
imagen.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.imagenprueva));
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/imagen"
/>
</RelativeLayout>
Here's the full source code hope it helps Source code

In your .xml file
add
xmlns:app="http://schemas.android.com/apk/res-auto"
then in the ImageView add this line of code:
app:srcCompat="#drawable/books"
NOT
tools:srcCompat="#drawable/books"

Related

Image background in imageView not changing by using setImageResource()

I am trying to change the background image of an ImageView for different options on Radio buttons in the calling activity. The calling activity passes the parameters in a bundle to rendering activity and I am receiving the parameters properly (I get Curve1 and Curve2 as parameters in the rendering activity after making choice on radio buttons). But I am not able to change the image background either by setImageResource() or setBackgroundResource().
Here is the snippet doing that:
Rendering Activity
private void validateImage()
{ wave = (ImageView) findViewById(R.id.image_wave);
switch (extras.getString("Curve"))
{
case "Curve1":
wave.setB (R.drawable.sinewave1);
break;
case "Curve2":
wave.setImageResource(R.drawable.sinewave2);
break;
}
}
activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="shantanu.concussion.usu.concussiontest.TestActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/image_wave"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
Can anybody suggest what am I missing?
Thanks :-)
Try to call invalidate manually. Violently change least something. You need to understand where the problem is (wrongly chosen View, View does not change) Try to change another property for example.
May cause in the resource. (e.g., it is transparent)
Check the entrance to the branch switch. (perhaps wrongly runs comparison)
Change the properties through the editor. Maybe something prevents display background.
It's just a stupid mistake.
try
wave.setImageDrawable(getResources().getDrawable(R.drawable.sinewave2));

How to wait for an ImageView in xml to finish loading

I want to create a title screen for an Android app. The screen is a simple XML file with an image in it. Basically the screen will show until some other stuff finishes loading, upon which the screen will change to the app content. The problem is, many time the image in the XML doesn't appear at all - all I get is a blank xml page, and afterwards the app loads. I assumeit's because loading the image is done async or something. I tried to use Thread.sleep, which proved to be a bad idea.
Anyone has an idea how to get this to work?
Thanks!
The main xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="128dp"
android:src="#drawable/icon" />
The main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<do some stuff>
start_other_activity_with_different_xml();
Ok, got found the answer faster than I expected. For my purposes, what I'm looking for is a splash screen, and a good explanation of how to create one can be found here: http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/

How can I fit an Image into another imageview

I have an imageview with a default image already set from the xml, and I want to change it's image pragmatically so that the new image will scale to the previous image size, both of the images are rounded.
I have tried setting the scaleType attribute to different values before setting the new image but it didn't work.
This is the xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/up_right_circle_firstiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:src="#drawable/circle_medium_up" />
This is the activity code:
public class MainActivity extends Activity {
ImageView middleCircle;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
middleCircle = (ImageView) findViewById(R.id.up_right_circle_firstiv);
middleCircle.setImageResource(R.drawable.round_pic_one);
}
}
Inside the activity.
I have uploaded the screenshots:
http://imgur.com/OM79T
http://imgur.com/G7y3Q
The solution that I have found was setting the layout_width and layout_height to a default size.
First I took the size of the original image file, then I have converted it to dp.
http://labs.skinkers.com/content/android_dp_px_calculator/
I had different image sizes for each resolution.
And then my xml looked something like this :
<ImageView
android:id="#+id/up_right_circle_firstiv"
android:layout_width="33dp"
android:layout_height="33dp"
android:src="#drawable/circle_medium_up" />
I only changed the dp values for each layout.
I know it is not the best solution but it worked for me.

ImageView with background leaves margin when image is loaded

I have my imageview declared like this:
<ImageView
android:id="#+id/category_image_top"
android:layout_width="match_parent"
android:layout_height="170dp"
android:maxHeight="170dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/image_placeholder"
/>
And this is how I set my category_image_top in asynctask (on onPostExecute)
imageView.setImageBitmap(image);
When image is set, imageview suddenly gets a margin of 4px. But when I remove android:background="#drawable/image_placeholder" from XML everything is fine?!
BTW: image_placeholder is a 9-patch image, if this makes any difference.
Any ideas why this happens?
UPDATE: I've tried placing background as a solid color and then no margins appear when image is loaded. I've also tried placing another 9-patch image and when I do so margins appear again. So it must be something with background as an image
UPDATE2: Maybe it's an android bug like this guy points out? https://stackoverflow.com/a/8340745/581531
I can guess that image_placeholder has 4px transparent margin
OK. Solution was found, maybe it's not the prettiest one but hey, it work! :)
In my view I've just added an arbitrary View that is a holder for my image. On top of it i have my ImageView so when the image is loaded placeholder (View) get covered. This is my view (or at least part that matters):
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<View
android:layout_width="match_parent"
android:layout_height="210dp"
android:background="#drawable/image_placeholder"
/>
<ImageView
android:id="#+id/article_image"
android:layout_width="match_parent"
android:layout_height="210dp"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:scaleType="centerCrop"
/>
...
The element that contains the image view may have a margin, have you checked that?
Change android:background="#drawable/image_placeholder" to android:src="#drawable/image_placeholder"
Also add the following: android:scaleType="centerCrop"
If centerCrop doesn't work, try the other ones listed here.

Dispalying image in android

I am trying to display an image in android.The process is i am downloading 1 image from net and trying to display it in the screen,the problem i am facing is in the activity screen i cant able to set the content view and the image.Following is the code:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Activity Screen:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.);---->Here i cant set as main
Bitmap bitmap = DownloadImage(
"http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
ImageView img = (ImageView) findViewById(R.id.); //here i cant set the image id
img.setImageBitmap(bitmap);
}
Use Android Smart Image View. That is the easiest way to display an image using a URL. Here is the link.
Clean your project.sometimes it don't take R.java file.
Just clean the project using Project -> Clean. If it still exists, run the project, and they will usually disappear.

Categories

Resources