Image is showing in studio but not in Phone.
Image is placed in res.
Running on one plus one.
using linear layout
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/wifi"
android:id="#+id/imageView3"
android:layout_centerHorizontal="true"
/>
You have to add below line in to your root layout.
xmlns:app="http://schemas.android.com/apk/res-auto"
Please see below example.
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/white"
android:clickable="true"
android:orientation="vertical">
<ImageView
app:srcCompat="#drawable/ic_user_red"
android:layout_width="50dp"
android:layout_height="50dp"/>
</LinearLayout>
without xmlns:app="http://schemas.android.com/apk/res-auto" you can not use app:srcCompat.
Inside res folder there like drawable,drawable-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi etc.
You should make sure that you copied your image in drawable folder not in Idpi or mdpi
try it by Changing the code to
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/wifi"
android:id="#+id/imageView3"
android:layout_centerHorizontal="true"
/>
Related
I'm struggling with an issue with the ImageView tag. It seems to load a PNG from the drawable folder in preview, but when I run the app on my phone via the simulator the image does not show up.
Here's the XML layout for my image:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context="org.tensorflow.demo.CameraActivity" >
<Button
android:id="#+id/calButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start/Stop" />
<Button
android:id="#+id/clearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="110px"
android:text="Clear" />
<ImageView
android:id="#+id/cView"
android:layout_width="50dp"
android:layout_height="46dp"
android:layout_marginTop="240px"
android:src="#drawable/record_circle" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="120dp"
android:layout_height="62dp"
android:layout_gravity="bottom|end"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</FrameLayout>
Here's the code in my activity to load the image as well:
ImageView circleView = (ImageView) findViewById(R.id.cView);
circleView.setImageResource(0);
Drawable circleDraw = getResources().getDrawable(R.drawable.record_circle);
circleDraw = (circleDraw);
circleView.setImageDrawable(circleDraw);
Thanks!
**I've tried using both src and background in the android:src="#drawable..." property
Try this:
<ImageView
android:id="#+id/cView"
android:layout_width="50dp"
android:layout_height="46dp"
android:layout_marginTop="240px"
app:srcCompat="#drawable/record_circle" />
Here is an example of the app namespace import:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
Note that this is the top-most layout, so it may be "LinearLayout," Or whatever you are using
I created an navigation drawer activity and found that its content page looks different in the preview.
This is the preview.
And this is the AVD appearance (it looks like a real device too).
XML code is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.xy.xy.HomePage">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="160sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp">
<ImageView
android:id="#+id/TestMenuImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
app:srcCompat="#drawable/bg1"
tools:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
tools:background="#color/hbm_text_background">
<TextView
android:id="#+id/TestMenuName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:contentDescription="#string/app_name"
android:text="#string/test"
android:textColor="#color/white" />
</RelativeLayout>
<Button
android:id="#+id/TestMenuButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</LinearLayout>
What do you think where the problem is?
Try adding android:scaleType="fitXY" in ImageView like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.xy.xy.HomePage">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="160sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp">
<ImageView
android:id="#+id/TestMenuImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
app:srcCompat="#drawable/bg1"
android:scaleType="fitXY"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
tools:background="#color/hbm_text_background">
<TextView
android:id="#+id/TestMenuName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:contentDescription="#string/app_name"
android:text="#string/test"
android:textColor="#color/white" />
</RelativeLayout>
<Button
android:id="#+id/TestMenuButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</LinearLayout>
That's probably a little bug with ImageViews´ previews in Android Studio,
Hope it helps
EDIT: I see you've included the style file and it does appear that your app is running the same style (AppTheme) as your preview so I think we can possibly rule out that as a culprit.
I doubt this makes a difference but I noticed your preview is running api 27 and your device is running api 27.
Try removing the
tools:context="com.xy.xy.HomePage" and see if it makes a difference.
The preview can only show you the current view. It doesn't know the parent view. The child view may have a parent view that constricts the size of the child view. Let's say that your image is set to width="match_parent". In this case, the preview may expand the image to fill the full width but at runtime your image my be placed inside a parent with a width="100dp" which would cause the image to have a width of only 100dp. Padding and margin of the parent view can also cause an issue.
If this isn't the problem then it may be a styling issue. I'd have to see more of your Theme and Style setup in order to know for sure. Your preview might be showing you a different style/theme than what you are actually using at runtime.
Hello I need help as i Am adding an ImageView in my android xml file and i get an extra space above and below it i have tried to see the options that i can use to remove it one of them was adding android:adjustViewBounds="true"
but when i do that that it removes my action Bar completely below is my code
<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"
tools:context="com.example.fred.yebo.MainFragment">
<ImageView
android:id="#+id/yeboAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/dice"
/>
</RelativeLayout>
Try this attribute :
android:adjustViewBounds="true"
automatically scales the height of the View.
<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"
tools:context="com.example.fred.yebo.MainFragment">
<ImageView
android:id="#+id/yeboAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/cal" />
</RelativeLayout>
Please check this answer.
You can try with scaleType property of your imageView
android:scaleType="fitXY"
this line scale your image with size of your container.There are a few properties of scaleType you can try which will be better for your case.Hope to help you!
<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"
tools:context="com.example.fred.piku.MainFragment">
<ImageView
android:id="#+id/pikuAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dice" />
</RelativeLayout>
I searched a ton in the internet on how to make my background image full on my android app and apparently it's still smaller. I was hoping if you guys know any solution for this?
Most of the image I've uploaded is on the #mipmap folder and apparently I can't find a way to insert an image in #drawable folder. I tried opening the drawable folder and copy paste a picture to put it inside but when it ran it came across a couple of errors.
Error:(12, 25) No resource found that matches the given name (at 'background' with value '#drawable/images').
This a picture of is how it looks like
I've also attached my content xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="fill_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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_sad"
tools:context="com.example.chadymaebarinan.emojiexpress.Sad">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#mipmap/ic_launchersad"
android:gravity="center"
android:id="#+id/imageView"
android:scaleType="fitXY"
android:layout_marginTop="43dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="''I am SAD''"
android:textColor="#FFFF00"
android:textSize="50dp"
android:textStyle="bold"
android:gravity="center"
android:id="#+id/textView4"
android:layout_below="#+id/imageView"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="111dp"
android:layout_height="111dp"
android:src="#mipmap/ic_launcherspeak"
android:background="#null"
android:scaleType="fitXY"
android:id="#+id/imageButton"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true" />
<ImageButton android:id="#+id/back"
android:layout_width="111dp"
android:layout_height="111dp"
android:src="#mipmap/ic_launcherback"
android:background="#null"
android:scaleType="fitXY"
android:layout_below="#+id/imageButton"
android:layout_alignStart="#+id/imageButton" />
</RelativeLayout>
and here is my activity xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context="com.example.chadymaebarinan.emojiexpress.Sad"
android:background="#drawable/ic_launcherimage">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_sad" />
</android.support.design.widget.CoordinatorLayout>
Put this into drawable folder instead of mipmap . The mipmap folders are for placing your app icons in only.
Drawable folders in res folder?
The image you want to add into your project just copy that and then right click on the drawable folder and press paste. I know you said you couldn't do it but that's how it works for me.
Once it is in the drawable folder you can set it as the background of your parent layout ie LinearLayout etc.
The mipmap is just for icons :)
put the my content.xml file into drawable folder and use it .
I'm trying to use some portions of my previous project in my new project but I have encountered with a strange problem. My new project renders the same layout differently from the previous project. Here is the layout.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center" >
<ImageView
android:id="#+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
/>
</FrameLayout>
And here are the two different results:
Layouts are 100% same but in the first one, ImageView holds a larger space than the image. In the second one, ImageView wraps the image, no extra space around the image. I want to use second one but what am I missing?
Target sdk, min sdk etc. are all the same.
Thanks in advance.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:paddingRight="50dp"
android:paddingLeft="50dp">
<ImageView
android:id="#+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
/>
</FrameLayout>
You may want to try this code above. Adjust the paddings as you want!
Try this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/id1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/image"/>
</FrameLayout>