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" />
Related
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
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);
The social icons below (for fb, twitter, mail and share) are circular PNGs with transparent backgrounds. When I use the code and exact same images in Eclipse, I get transparent background but when build and run with Android Studio, I get black backgrounds. See details below.
Images are the same. Code used in layout xml files:
<LinearLayout
android:id="#+id/ll_shareBtns"
android:layout_width="#dimen/sharesection_width"
android:layout_height="#dimen/sharesection_height"
android:layout_gravity="center"
android:background="#drawable/bg_sharebox"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_facebook"
android:layout_width="#dimen/dimens_sharebtns"
android:layout_height="#dimen/dimens_sharebtns"
android:layout_marginLeft="#dimen/sharebtns_leftmargin"
android:layout_marginRight="#dimen/sharebtns_rightmargin"
android:background="#drawable/bg_facebookbtn"
android:gravity="center" />
<Button
android:id="#+id/btn_twitter"
android:layout_width="#dimen/dimens_sharebtns"
android:layout_height="#dimen/dimens_sharebtns"
android:layout_marginLeft="#dimen/sharebtns_leftmargin"
android:layout_marginRight="#dimen/sharebtns_rightmargin"
android:background="#drawable/bg_twitterbtn"
android:gravity="center" />
<Button
android:id="#+id/btn_email"
android:layout_width="#dimen/dimens_sharebtns"
android:layout_height="#dimen/dimens_sharebtns"
android:layout_marginLeft="#dimen/sharebtns_leftmargin"
android:layout_marginRight="#dimen/sharebtns_rightmargin"
android:background="#drawable/bg_emailbtn"
android:gravity="center" />
<Button
android:id="#+id/btn_share"
android:layout_width="#dimen/dimens_sharebtns"
android:layout_height="#dimen/dimens_sharebtns"
android:layout_marginLeft="#dimen/sharebtns_leftmargin"
android:layout_marginRight="#dimen/sharebtns_rightmargin"
android:background="#drawable/bg_sharebtn"
android:gravity="center" />
</LinearLayout>
When code compiled and run with Android Studio, I see this:
When code is compiled and run with Eclipse, I see this:
Quite Strange. Could difference in build tools version cause this problem too?
Its seems to be problem in Theme used in xml file .
-check it once if the theme is different in eclipse and Android Studio then you can correct it.
by changing the Theme on file style.xml
Hope this will helpful .thanks
Did you try testing your app in a device? This is a known bug with Android Studio where icon PNGs show a black box despite being transparent. I faced the same issue where I saw a black box background in Android Studio layout design preview but the image rendered correctly in the device.
This link can give you more confidence - ic_stop_white_*dp.png background is black, not transparent
i have any troubles with switching between orientation from portrait to landscape.
i made this page cause i don't have permission to add images
http://www.guitart.comyr.com/
There are my codes and two images.
I've tried to make some small application to test switching, i made same folders as you can see at the page and this small test app works fine. But my original app doesnt work.
Sorry for English.
Do you have
<Button
android:id="#+id/btn_menu_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/info64"
android:src="#drawable/info64" />
in both menu.xml layouts(layout|layout-land) folders? If you change the orientation to landscape, your app automatically uses the xmls from your layout-land folder. Eclipse don't recognize if the ID exists in both orientations. Eclipse can't stop you from building because it's no error but it could at least give you a warning imo.
No problem :)
Have you tried to just copy the layout file from layout folder and paste it in "layout-land". check the id's of the views because logcat is showing nullPointerException in onCreate.
<Button
android:id="#+id/btn_menu_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/info64"
android:src="#drawable/info64" />
remove
android:src="#drawable/info64"
line from this button's property.thats the problem.and you are done.
I have used this code to get a feed reader form twitter. I have list them in the listView as the article says, but with some images I have a problem with the size and they are anormally big...I have tried unsuccesfully to fix the size of the image with:
<ImageView android:id="#+id/photoUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="10dp" android:maxHeight="10dp"
android:paddingRight="12dp"/>
Strange that the code didn't do that for me when I wrote it. What android version are you testing on?
For one reason or another, I've found that often only specifying maxWidth/maxHeight doesn't behave as you would expect, and I also have to specify minWidth/minHeight to effectively control image size. I had to do exactly that in a follow-up development tutorial to the post you reference.
Give something like this a try:
<ImageView
android:id="#+id/photoUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="30dip"
android:minWidth="30dip"
android:maxHeight="30dip"
android:minHeight="30dip"
android:paddingRight="12dip" />