Dummy ImageView not showing image - android

It is showing the image in the Android Studio preview but doesn't show anything when I'm running this on my device. Any ideas why this happens? What's wrong with my code?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myimage" />
</RelativeLayout>

Does your class extend Activity? Try extending AppCompatActivity. I had the same issue and this solution fixed it.

In addition to my comment i've added a few possible soultions. It is very strange the image isn't showing already. Does it show in the imageview in the xml gui design view?
Anyway here are two possible solutions:
xml:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/myimage" />
Also your imageview hasn't been assigned an Id which seems weird something like this would be needed for my second idea:
<ImageView
android:id="#+id/Image_ID"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Java side:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //what ever the name of the activity you are using
Image= (ImageView)findViewById(Image_ID);
Image.setImageResource(Image_ID);
}
Lets hope this helps!
EDIT
<ImageView
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/myimage" />
Note fitXY may lower the quality of the image. try messing with the other scale types

Related

ImageView on mapActivity

I made a little app with a map for learning.
Over the map I added a textView with actual position upgrading..
After some problems I did it with a such layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
<TextView
android:id="#+id/txt_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="TextView"
android:textSize="30sp"
android:textAlignment="center"
android:textColor="#drawable/common_google_signin_btn_icon_light"/>
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/txt_location"
android:layout_centerHorizontal="true"
app:srcCompat="#drawable/ic_action_name" />
</RelativeLayout>
Now I would add an imageView for showing gps status (focused, enabled, disabled).
I added the imageView under the textView, but I don't see anything.
After some try I'm asking why.
ps:
If I put imageVIew in a LinearLayout (before the map) I see linearLaout (in black for es.) over the map, and I still can't see image but, eventually, only the background if setted.
It's not my goal to add a linear layout, but also in this way I can't see image.
I created image by -> new image asset -> launcher icon (legacy only) clip-art (android logo) -> mo effects.
than I added it by reference in the imageView.
Solved!
I don't know if it's usual,
but adding ImageView from design window, it code Imageview as I posted.
Searching around I found similar situations and I found that correct way to show image over MapFragment is make a change to default code of ImageVIew:
from:
app:srcCompat="#drawable/img_name" />
to
android:src="#drawable/img_name" />
I don't know if I do something wrong with inserting new ImageView,
but hope this could help people that do as me.

Zooming a layout (Relative Layout) in android

I want to zoom in and out all the images inside the layout. I have seen many questions like here and here and tried to implement them but I couldn't.
Please help me in order to understand this. My confusion is, why would I extend my class from relative layout, as described in the question above, i mean i have a relative layout in my XML and I am setting this XML in content view but how could I grab the specific relative layout by using this methodology. And even if I have extended class from relative layout, how would I use this class in the activity which contains my images inside layout.
My XML setup is like this, can anyone please tell me what approach I have to follow.
<?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: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="com.cpec.farrukhtanveer.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="130dp"
android:layout_marginRight="130dp"
android:background="#drawable/imageBackGround">
<ImageView
android:id="#+id/img_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:src="#drawable/imageone"/>
<ImageView
android:id="#+id/img_two"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="imagetwo"/>
</RelativeLayout>
</RelativeLayout>
Please help folks !
Thanks.
Umair
Since no one came up with solution, I kind of sort it out myself and thought it might be helpful for some one else who is stuck in this problem. The basic guidance came from here, where author implemented the zoom in and out for a single image, we just have to add images and handle other initialization as described in that tutorial.
ImageViewWithZoomClass:
private Drawable imageOne, imageTwo;
imageOne = context.getResources().getDrawable(R.drawable.icon1);
imageTwo = context.getResources().getDrawable(R.drawable.icon2);
.
.
imageOne.setBounds(0, 0, imageOne.getIntrinsicWidth(), imageOne.getIntrinsicHeight());
imageTwo.setBounds(0, 0, imageTwo.getIntrinsicWidth(), imageTwo.getIntrinsicHeight());
.
.
imageOne.draw(canvas);
imageTwo.draw(canvas);
and since I am using relative layout, I just used onClickListener in my MainActivity like this
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(new ImageViewWithZoom(getApplicationContext()));
}
});
and XML for the relative layout looks like this:
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="130dp"
android:layout_marginRight="130dp"
android:background="#drawable/abc"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
>
<ImageView
android:id="#+id/img_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/roadandrailways"
android:visibility="invisible"/>
<ImageView
android:id="#+id/img_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/capital"
android:visibility="invisible"/>
</RelativeLayout>
You can add multiple images in layout.
Thats it, hope it helps.

Sony SmartWatch - show a view over the Gallery on the

While playing around with the Gallery I've got another question (this probably should be simpler).
I'm trying to show a view, lying in front of the Gallery, probably hiding it partially. Like a message dialog (Toast).
In order to do so, I'm using a FrameLayout with a Gallery in the back, and ImageView with a transparent src in the front. When a dialog should be shown, I'm setting the src to the needed drawable through "sendImage".
The problem is, as I assume, that the gallery/list items (that are added dynamically upon "onRequestListItem -> sendListItem") have more recent z-Order (as they have been added later), so the dialog is shown between the background of the Gallery and the transparent icons, representing the list items.
Maybe someone will have an idea, how to avoid this situation?
I am not aware of any way of adding views manually to the layout on the SmartWatch, or changing their zOrder through "bringToFront".
Here is the source code:
gallery.xml
<?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" >
<Gallery
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/toast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/toast_dialog_message" />
</FrameLayout>
gallery_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20px"
android:src="#drawable/item_icon" />
</RelativeLayout>
GalleryControlExtension.java
sendImage(R.id.spritz_image, drawableResourceId);
P.S. In the emulator it works as expected: toast lies over the item_icon ...

Issue with sherlock actionbar

Image as seen in ICS
Where as when run in Gingerbread
XML code for the fragment
<?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:background="#drawable/background"
android:id="#+id/homelayout" >
<ImageView
android:id="#+id/homepageLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background"
android:alpha="255"
android:scaleType="fitXY" />
</LinearLayout>
Any reason for such strange behavior?
try changing
android:layout_width="match_parent"
android:layout_height="match_parent"
to
android:layout_width="wrap_content"
android:layout_height="wrap_content"
in your ImageView. I don't know how you use your XML, but by the looks of it you might also want to change te same parameters in the LinearLayout to "wrap_content". Now you are telling the ImageView to (probably) fill the whole area.
// Alex
The problem is observed only when you dynamically request for the actionbar from the code.
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
If you do it using styles, this problem is not seen.

Unable to hide TitleBar, becomes transparent

I want to delete the title bar from my app, but when use the following code, the title bar isn't hidden, in fact it becomes transparent and the content is pushed downwards:
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
So, I have a gap where the title bar used to be (between the notification bar and the app).
I have searched everywhere but haven't found something similar.
Edit:
I have tried both of the solutions given by seretum and harism (shown below) but I'm still unable to find the problem, this is a part of my xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff">
<ImageView android:id="#+id/title"
android:src="#drawable/main"
android:layout_width="480dp"
android:layout_height="155dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"/>
Somehow, the ImageView isn't aligned to the Top of the relative layout...
Last Edit. SOLUTION:
So, I tinkered with the xml and found out that when you set a custom dimension to a widget and so use the Realtuve Layout; no matter if the Title bar is present or not, the Widget will always be under it. So, here is the solution (you need to put the object in a layout):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff">
<LinearLayout android:id="#+id/title"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:maxHeight="155dp"
android:maxWidth="480dp"
android:layout_height="wrap_content">
<ImageView android:src="#drawable/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"/>
Have you tried requesting no title in different order;
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Works for me.
In your app's AndroidManifest.xml, inside the "application" tag, put
android:theme="#android:style/Theme.NoTitleBar"
That will remove your bar from every activity.

Categories

Resources