Adding ImageView in XML File causes program to stop unexpectedly - android

I am attempting to add a tiled image as a background of the program.
The code that I am currently using in main.xml, which is crashing:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/cartoonclouds"
android:contentDescription="#string/desc" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
</ImageView>
However, when the ImageView is removed and the xmlns is moved to the LinearLayout, it functions fine (without, of course, the image).
I can't see any related errors in LogCat.
More information:
Using Eclipse and Android 2.2, API 8. The program runs but crashes instantly.

You can not put anything in an ImageView. It's not a layout. Use the background attribute of the LinearLayout to set the background to your image.
Another option would be to wrap everything in a RelativeLayout. Separate the ImageView and the LinearLayout. The image will fill the RelativeLayout and the LinearLayout will be on top of it.
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/cartoonclouds"
android:contentDescription="#string/desc" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
</RelativeLayout>

You should not add a linear layout inside an imageview. Have a linearlayout at the top as root and then add the image view in there

Related

Android Webview with child at bottom

I've been searching both Google and stackoverflow for a while now and haven't found anything answering my question.
I'm wondering about putting a LinearLayout (with no specific known height in pixels) at the bottom of a WebView. This LinearLayout will later be populated with a fragment containing comments for the article displayed in the WebView.
The issue of the problem is that the WebView is almost always larger than the screen so the user has to scroll to read the full article. At the bottom of the scrolling I want the CommentsFragment to be inserted (through the LinearLayout, it takes a few parameters so it can't be loaded directly from the XML).
I've tried a bunch of solutions but all of them make the LinearLayout stick to the bottom of the layout the all time, not on the bottom of the WebView.
My current code is the following, of course not working:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/article_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
<LinearLayout
android:id="#+id/comments_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Thank you for your help!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/article_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/comments_container"
android:layout_below="#+id/article_webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
got it working after a few rounds. The solution was using a ScrollView then inside that a relativelayout mirroring #Karan_rana's answer.
If you want to put the comments fragment below web view then you can try as following and tell me if it works for you
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/article_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/comments_container"/>
<LinearLayout
android:id="#+id/comments_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Try this, make sure to have layout_height="wrap_content" in your WebView
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<WebView
android:id="#+id/article_webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/comments_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>

Android background image not shown in device?

I add the following code to main.xml to set an image as a background image for my app:
android:background="#drawable/bg"
So main.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom|fill_vertical"
android:background="#drawable/bg0"
>
<EditText
android:id="#+id/edWord"
android:layout_width="fill_parent"
android:layout_height="53px"
android:textSize="20px"
android:singleLine="true"
android:hint=""
/>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:layout_weight="1"
>
<ListView android:id="#+id/lstWord"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/btnGetText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/get"
android:layout_weight="0.25"
/>
<ImageButton
android:id="#+id/btnFind"
android:text="Click me..."
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/find"
android:layout_weight="0.25"
/>
</LinearLayout>
</LinearLayout>
And of course the image bg.png is in drawable.
The problem is that the background image is shown only in the emulator but not in my actual device (HTC Desire).
What have I done wrong? What do I need to do to solve this problem?
Can you guys there help? Thank you very much.
I found the problem . Just remove fill_parent from the main Linear Layout Gravity. No need to set background in the Listview as you do right now. Just Change as i suggest it solve your problem.
android:gravity="bottom|fill_vertical"
try this in place of that
<LinearLayout
xmlns:android= "http://schemas.android.com/apk/res/android"
android:id="#+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg" />
</Linearlayout>
I got a similar bug my background drawable was shown in the graphical editor of eclipse but not on the devices I used for debugging.
-> For me it helped to clean the image (I just opened it in paint and saved it) after that i
overwrote the old image with the new one and copied it into the drawable folders.
Some lolipop devices wouldn't show with higher resolution images. You could also manually resize the bitmap programmatically if you generate the backgrounds randomly or use a set of images to load them into the background

Android ListView doesn't expand the whole screen

I've problem similar to the one here: Android ListView doesn't expand the whole screen?
The XML is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/ResultLayout">
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:layout_width="fill_parent" android:id="#+id/ListView"
android:layout_height="fill_parent"></ListView>
</ScrollView>
<LinearLayout android:id="#+id/pagingPanel"
android:gravity="center" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"></LinearLayout>
<Spinner android:layout_width="fill_parent" android:id="#+id/ManSpinner"
android:layout_height="fill_parent" />
</LinearLayout>
In this case, I should have a ScrollView or the PagingPanel LinearLayout will disappear.
Edit
The desired layout is to have all the elements stacked on top of each other. but if the elements exceed the page height, a scroll should be added to the ListView.
If you have a vertical LinearLayout, You can't have multiple fill_parent for layout_height of your LinearLayout elements. What layout you would like to achieve? Uniformly divide screen for all elements?
Another problem is that using ListView in ScrollView is not a good idea also because ListView itself is scrollable.
You should write what layout you would like to achieve with your XML. Also generally is a good practice to write your XML in multiple steps and iterate to woking solution layout by layout.
EDIT: Ok, try something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ResultLayout">
<ListView android:id="#+id/ListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout android:id="#+id/pagingPanel"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<Spinner android:id="#+id/ManSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

uable to set layout of a view after adding it in WebView

I have added a view into WebView , but the problem is after adding a view using webView.addView(myview) the layout of the myview resides on the top of the screen though i have set android:layout_gravity="bottom" in myviews's layout resource file.
so far i have tired following things
myview_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mc_track_icon"
>
</ImageView>
</LinearLayout>
i tried the relative layout too
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mc_track_icon"
>
</ImageView>
</RelativeLayout>
Even i have tried to set the layout explicitly in code also
webView.add(myView,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.BOTTOM));
But the ImageView is visible on the top left corner only.
Maybe using a Relative layout would be better.Then in the yourlayout.xml add
android:layout_alignParentBottom="true" >

glsurfaceview inside a scrollview, moving but not clipping

I have a scrollview with a linear layout inside. One of the elements inside this linearlayout is a glsurfaceview.
This all works correctly and when I scroll the glsurfaceview moves up and down however when the glsurfaceview reaches the top or bottom of where it should of the scrollview where it should be clipped it is not and is continued outside of the scrollview. This screenshot should make it clearer:
Don't think it's completly nessecary but here is my layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
>
<!-- LOTS OF SEEKBARS/TEXTVIEWS -->
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.4"
android:layout_marginRight="10dip"
android:layout_marginLeft="10dip"
android:orientation="horizontal" >
<android.opengl.GLSurfaceView android:id="#+id/glview"
android:layout_width="100px"
android:layout_height="250px"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_marginTop="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- OK/CANCEL BUTTONS -->
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
All help much appreciated :)
Hosting SurfaceViews inside ScrollView (or Listview, etc.) is currently not supported.
You may use below lines for removing over scrolling :
glView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
glView.setZOrderOnTop(true);
glView.setZOrderMediaOverlay(true);
And for removing black background :
linearLayout.setBackgroundColor(context.getResources().getColor(R.color.white));
linearLayout.addView(glView);
Also add below line in your manifests file :
android:hardwareAccelerated="true"
It is not about support as answered abowe. The cause of issue is that GLSurfaceView is not on the view level. In short: for you case you have GLSurfaceView at the first level, then other views with holes in views abowe GLSurfaceView (this handles by windows manager, and it dosent matter where you put GLSurfaceView on xml, it will be on first level in any case and others views on next level)
So, what you need is to force view update, and there is work around: you should put scroll with GLSurfaceView or (RecyclerView with GLSurfaceViews) "between" two FrameLayouts. something like 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" >
<ScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
---- here should be linear layout with your glView, or RecyclerView instead if required ----
</ScrollView>
--- this frame also required: ---
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
</FrameLayout>

Categories

Resources