How to wait for an ImageView in xml to finish loading - android

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/

Related

View is shown instantly even though I use handler.postDelayed

I have a two RelativeLayout where I have set their visibility to gone using,
android:visibility="gone"
And then on the root element of the activity which is also a RelativeLayout, I have added this attribute
android:animateLayoutChanges="true"
Here's my activity java code
RelativeLayout rellay1, rellay2;
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
rellay1 = findViewById(R.id.rellay1);
rellay2 = findViewById(R.id.rellay2);
handler.postDelayed(new Runnable() {
#Override
public void run() {
rellay1.setVisibility(View.VISIBLE);
rellay2.setVisibility(View.VISIBLE);
}
}, 2000);
}
When I run the app, the views get displayed immediately without any wait. If I increase the time to 5s, still same results.
I've looked at the logs when starting the activity and nothing suspicious.
NOTE
When answering questions on SO, I sometimes find it hard to understand the users problem when it's cluttered with XML, so I tried hard to keep my question clean. But that might not be enough to diagnose what's wrong with my code so I'm adding a minimal version of the xml for the parent and two child RelativeLayouts without their contents, just the attributes. If more detail is needed I'd be happy to provide.
<?xml version="1.0" encoding="utf-8"?>
<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:animateLayoutChanges="true"
android:background="#drawable/grad_bg"
tools:context=".LoginActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp">
<ImageView
android:id="#+id/img_view_logo"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_android_black_24dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
<RelativeLayout
android:id="#+id/rellay1"
android:layout_below="#+id/img_view_logo"
android:layout_width="wrap_content"
android:visibility="gone"
android:layout_height="wrap_content">
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rellay2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:visibility="gone"
android:layout_alignParentBottom="true">
</RelativeLayout>
</RelativeLayout>
From the XML above, you'll see that my intention is to have only the ImageView visible initially and the rest should become visible after 2s.
I have looked similar questions about Handler#postDelayed but the problems are not exactly like mine so the solutions where not applicable.
UPDATE
The only problem here seems to be my lack of proper understanding of the android lifecycle(I'm a beginner here). I carried out an experiment by killing all running apps and tried launching my app again and I saw the animation.
So I guess what was happening here was that, the view had already been created and since my activity was still alive even though I had exited the app, subsequent launches of the App loaded a previously saved instance of the activity and that is why it seemed as if the animation wasn't working.

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));

PNG not showing in ImageView

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"

Background Image in Android

I´m trying to put a background image in my layout, I put the path of the file (on drawables) in the "background" propierty, it shows in the xml preview, but when I run the app on my phone it doesn´t.
<LinearLayout 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=".MainActivity"
android:background="#drawable/background_image">
</LinearLayout>
My onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Also, I was trying this tutorial --> link but it doesn´t works, I can call the xml file and it shows an error.
There's one case where images wont be shown, if the dimensions of your drawable exceeds 2048 pixels in any axis (on non-HD devices only).

Image not resizing in Android Studio's emulator

I'm using a USB connected device to run my app. In this practice app, I used an Android logo image to implement the zooming feature. When I try to change the image dimensions in the activity, it does not show the changes when the app imported into the usb device is loaded. The logo is simply zoomed to fit. But I want it to display the image with the way I see it in the Design view: http://i.imgur.com/hcaOHjW.png
I forked the code from this source to implement the zooming: http://www.c-sharpcorner.com/UploadFile/88b6e5/multi-touch-panning-pinch-zoom-image-view-in-android-using/
It does not have the double tap zoom in and out feature so let me know what I can add to implement that
And here is the code for my activity:
<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.example.test.firstapp.Login"
android:id="#+id/activity_activity2"
android:focusable="false"
android:nestedScrollingEnabled="false">
<ImageView
android:layout_width="180dp"
android:layout_height="220dp"
android:id="#+id/botImg"
android:src="#drawable/bot"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"/>
in your java class try creating this view and then try changing the height and width parameters .inflate this view dynamically and assign parameters in the java itself dont use xml for this view .

Categories

Resources