image isnt showing on layout preview in android studio - android

Am new to app development , however I have seen multiple sources online that show you how to add an image to your layout
However the image isnt being shown on the layout preview
My code is below
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitStart"
app:srcCompat="#drawable/mm"
tools:layout_editor_absoluteX="132dp"
tools:layout_editor_absoluteY="16dp" />
I just don't know what I'm doing wrong .. I've followed multiple guides but still nothing
Image screenshot of studio showing issue :
https://imgwiz.com/image/gzHD
My PC seems to be having issues freezing occasionally whe Android studio is running so am setting up a VPS with studio on it and will test all these suggestions on the new installation as that may be related to my issue

Use
<android.support.v7.widget.AppCompatImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitStart"
app:srcCompat="#drawable/mm"
tools:layout_editor_absoluteX="132dp"
tools:layout_editor_absoluteY="16dp" />
instead of
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitStart"
app:srcCompat="#drawable/mm"
tools:layout_editor_absoluteX="132dp"
tools:layout_editor_absoluteY="16dp" />
helped me.
You can replace it without any changes in your code.
In addition: Be aware of using
tools:layout_editor_absoluteX="132dp"
tools:layout_editor_absoluteY="16dp"
The namespace tools: means that it only affects your editor preview. As i saw in your screenshot, you are using a ConstraintLayout as parent layout. Consider adding app:layout_constraintBottom_toBottomOf etc to align your ImageView.

it seems that your ImageView's position is not in the viewing area of the Design/BluePrint
so I recommend to switch to Text mode
and add these to your ImageView
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp"
to make it show on the designer when move it to your desired position

Related

CircleIndicator does not fit the screen

I am using this library https://github.com/ongakuer/CircleIndicator, in some cases too much data is coming. The indicator goes out of the screen, can I add animation to it, there is an animation that exists as default in ios.
Ios Example: https://streamable.com/6nu6j3
Android Problem:
I found this as a problem like this before, i was wondering if there could be a nicer solution like in ios
How to set a limit of items on CirclePageIndicator?
Layout
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<me.relex.circleindicator.CircleIndicator3
android:id="#+id/circleIndicator"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="#id/viewPager2"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="25dp"
android:layout_marginTop="20dp"
android:layout_marginRight="25dp"
android:gravity="center"
app:ci_drawable="#drawable/circle"
app:ci_height="5dp"
app:ci_width="5dp" />
Edit:
This library my work has been read -> https://github.com/wching/Android-Indefinite-Pager-Indicator
you have to set margin into your root level layout according to that they set margin .....
and don't check into the emulator some time emulator may be goes wrong so check it into real device only

Image showing in preview but not on Android tablet. XML issue?

So I have been working on an Android application to make an application for use with DJI's consumer level drones. I have been making the user interface and using XML throughout the project with no issues. I want to add a .png to the UI just so that my logo is showing. When I preview it in Android Studio, I can see the logo being shown but when it is run on the tablet, it is not there. Everything else using XML is fine though. I even preview using the same resolution/ screen size as my tablet I am using (cheap Hudl 2 tablet).
If anybody has any ideas about how I can get this to work, I would appreciate it. I have shown my university lecturer it and he couldn't give me an answer too as nothing seems to be out of the ordinary.
Android studio preview
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/macrosfordji"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
Try this (android:src="..." instead app:srcCompat="..."):
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/macrosfordji"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />

UI of my app gets broken when testing on a phone [Android]

I am trying to do some simple app to practice, and the user interface gets broken for some reason I don't know why when I run test on my phone. I did an app on android only once before and I didn't have this problem, I was using a different phone though. I'm testing this on Samsung Galaxy A5.
That's how it looks in project: http://imgur.com/Pnbg5ns
And that's how it looks on my phone: http://imgur.com/a/uki84
Anyone knows how to resolve this?
All your views have locations set with the tools:... attribute. The locations set this way (using tools:) position the views within the Android Studio editor, and Android Studio editor only. It doesn't do anything at all to position the views for when the app is actually run, that's why all your views are on top of each other, they simply don't have any attributes to indicate their positioning on the screen when the app is run.
You should check tutorials for how to use ConstraintLayout (if you want to use that layout) which is a recent (2016) addition to Android and position your views with the constraints you need. Or you could use some of the older layouts like LinearLayout which should be perfectly fine for your layout needs.
And absolute positioning of the views like you have with the use of the tools:... attribute is also a big no most of the time. Views need to be positioned in some relative way within a layout, which doesn't mean it has to be within a RelativeLayout :), just saying that the views should be positioned in reference to the layout containing them, not just at some absolute point f.e. (150, 110).
This is a very simple layout. You can use LinearLayout instead of ConstraintLayout.
Here is an example using LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="16dp">
<EditText
android:id="#+id/etLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Login"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Hasło"
android:inputType="textPassword" />
<Button
android:id="#+id/bLogin"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Loguj"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/tvRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Nie masz jeszcze konta? Kliknij tutaj."
android:layout_gravity="center_horizontal"/>
</LinearLayout>
OUTPUT:
Hope this will help~

Android - Seemingly harmless layout causes app to crash after short use without any errors

After 2 days of fruitless testing, I've decided to post my issue here, in the hopes that I'm missing something obvious.
I've boiled down a seemingly innocuous xml-layout to a random collection of images, layouts, and a scrollview. Here it is:
<?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:background="#drawable/wood_texture"
tools:context="com.testlayout.example.testlayout.MainActivity">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/image_one"
android:id="#+id/imageOne"
android:adjustViewBounds="true"
android:layout_gravity="center_vertical"
android:visibility="visible"/>
<ImageView
android:layout_width="300dp"
android:layout_height="150dp"
android:background="#drawable/dark_wood_texture"
android:id="#+id/darkWood"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:visibility="visible"/>
<HorizontalScrollView
android:layout_width="300dp"
android:layout_height="150dp"
android:id="#+id/handScrollView"
android:scrollbars="none"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:visibility="visible">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/handLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/image_three"
android:id="#+id/imageView"
android:adjustViewBounds="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/image_three"
android:id="#+id/imageView2"
android:adjustViewBounds="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/image_three"
android:id="#+id/imageView6"
android:adjustViewBounds="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/imageTwo"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:src="#drawable/image_two_tiled"
android:scaleType="fitXY"/>
</RelativeLayout>
I can load this layout onto my phone just fine, but after swiping around in the scrollview for as little as 5 seconds, the app will suddenly crash to the home screen - without reporting that the app has stopped working. I've been testing on a Samsung Galaxy s6.
I've also tested this on a Samsung Galaxy Tab 2, where this problem does not occur (both my real app and this test app run just fine on the tablet).
I've created a new Android Studio project; the only changes I've made are to the res/layout/activity_main.xml, as outlined above. I've also included the drawables I'm using. I'm hoping someone with better debugging skills than I can either pull down that project, or simply create a new one of their own using the above layout (that's all there is to it, though you'll need my drawables regardless).
I'd like to clarify that I'm not looking for a way to fix this issue, I'm trying to understand the issue. The thing is, after 2 days of testing, I've found half a dozen ways to seemingly "fix" the issue. But none of them make any sense, and they all seem unrelated. If I don't know why my solution fixes the issue, I won't know how to avoid it during future development.
Observations
As I mentioned above, I've found several ways to "fix" the issue (i.e. make it no longer crash), but none of them seem to be related. Here are a few:
Set the visibility of almost any of the elements to gone.
Remove the background from the root element.
Remove the line android:tileMode="repeat" from drawable/image_two_tiled.xml
Note that changing the tileMode to something else - for instance, clamp - does not fix the issue. Only removing it (or setting it to disabled).
Set the src of the ImageView with the id "#+id/imageTwo" to image_two_smaller_tiled.
This is the exact same image as image_two_tiled, just a smaller resolution.
This is by no means an exhaustive list of, but it gives you an idea of how disjointed these fixes are (especially the tileMode one).
As near as I can tell, it looks like some kind of memory issue. I'm fairly new to android development, so I'm not quite familiar with the memory constraints I need to work with, but I would be horrifically concerned if I was causing the heap to explode with so few images, and of fairly small size (at the very least, certainly not big).
If anyone could tell me exactly what is causing my app to crash, I would be. . . Well, I would be extremely grateful. 'Cause after 2 days of trying to debug this myself, I'm about ready to quit android development : (
Edit
The app itself does not throw an exception, however logcat does consistently show this error at the time of the crash when I'm not filtering log messages from my app alone.
E/Resources: RunTimeException
android.content.res.Resources$NotFoundException: Resource ID #0x7f0202cf
at android.content.res.Resources.getValue(Resources.java:2558)
at android.content.res.Resources.startRC(Resources.java:1116)
at android.app.ActivityThread$mRunnable.run(ActivityThread.java:3056)
at java.lang.Thread.run(Thread.java:818)
e2: I'm starting to wonder if this is an issue with my phone, and not the app, since I can run it just fine on my tablet. I'd really like to know if anyone else is experiencing this issue if they try to run this on their Galaxy s6. Specifically the S6 - that would tell me if it's an issue with my phone or all S6's in general.
This previous SO post answer solved my issue.
In short, it was the BitmapDrawable that was causing the entire issue. While unfortunately I cannot say why this fixes the issue, setting the view that uses the bitmap with layerType="software" will prevent the crashes.
In a layout: android:layerType="software"
In code: view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Android-studio Project fails to run

I'm currently working on a project in android studio and as a part of my project I needed to clean it. I followed the proper steps and now my assignment wont load.
I keep getting these errors and I don't know what they mean. Please help!
Try changing your ImageView definition to:
<ImageView
android:src="#drawable/sundae"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:id="#+id/imageView" />
I don't believe the tag "layout_centerCrop" exists.
The error basically says that the layout attribute mentioned is not identified, or is nonexistent. In this case, it is layout_centerCrop. Please replace android:layout_centerCrop="true" with android:scaleType="centerCrop".
By the way, I would suggest centerInside rather than centerCrop so that none if the image will be cropped out, and will still be in the same aspect ratio.
It will look like this:
<ImageView
android:src="#drawable/sundae"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:id="#+id/imageView" />

Categories

Resources