Scrolling a Linearlayout (Android App programming) - android

I already read a lot about scrolling a Linearlayout, but nothing helped yet.
It says you can simply put it in a ScrollView, but whenever i do that, my app crashes when i try to start it. Thats my Sourcecode of the xml :
<?xml version="1.0" encoding="utf-8"?>
<Scrollview xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/lout"
android:orientation="vertical"
android:background="#drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
</LinearLayout>
</Scrollview>
I always get an error which says that the app failed to inflate the class ScrollView.
If i knew what that exactly mean, i may be able to solve the problem on my own.

You have
<Scrollview
small "v". Change that to
<ScrollView
You will also need to change the end tag
</ScrollView>
Error inflating class means just what it says. It can't find a class with that name here it is because the capitalization is wrong.

Related

Displaying a View beneath a WebView

I apologize if this has been answered, extensive Googling found me a lot of similar but not-quite-the-same situations that didn't provide an answer. I have a WebView and would like to put a Google AdView underneath it. However, I don't want the add always attached to the bottom of the window, I want it to come AFTER the WebView, i.e. I scroll through the content of the webpage and then, underneath that, the ad is displayed. I've tried many variations of Relative and Linear layout, here's one attempt as an example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="1"/>
<com.google.android.gms.ads.AdView
android:id="#+id/web_frag_ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="myUnitId"
ads:adSize="BANNER" />
</LinearLayout>
I've tried RelativeLayout with layout_below, but the WebView covers it up. It displays if I alignParentBottom, but that isn't the behavior I want. I want it to only be visible after scrolling through the content of the web page. I'm sure I'm making a simple layout mistake, and help pointing me in the right direction is greatly appreciated.
I have already done the similar thing :
1- Build your Layout like this :
<LinearLayout
... >
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="1"/>
<com.google.android.gms.ads.AdView
android:id="#+id/web_frag_ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="myUnitId"
ads:adSize="BANNER" />
</ScrollView>
</LinearLayout>
2- Calculate the content height of your web view after the content is rendering. If you don't know how to do this, tell me, I can give you the corresponding code.
3- Set the webView.height = content.height
So your scrollview.content will adjust to the global height webView.content+web.frag_ad.height

Android - background does not repeat, already used titleMode, fixed only when resuming app

I have two layouts that call "footer.xml", first one is "login.xml", and second is "main.xml"
When opening the application, onCreate method sets the content view to login.xml.
After I successfully logged in, the main.xml is loaded(successfully).
Now, here's the problem.
If the main.xml is set as the context view after the user logged in, the footer does not repeat, instead, it is stretched.
I'm wondering why, because the login.xml and main.xml call the footer.xml the same way.
Any solution about this problem?
By the way, here's the footer.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/footer"
android:tileMode="repeat" />
This is how I call footer.xml with login.xml and main.xml:
<LinearLayout android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="#layout/footer"
android:layout_alignParentBottom="true">
</LinearLayout>
Edit:
I have observed that when pressing the home button, and resuming the app again fixes the footer.
But closing the app using task manager and opening it again does not repeat the background image.
How to solve this?
Found the solution!
This is my login.xml and main.xml look like:
<LinearLayout android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="#layout/footer"
android:layout_alignParentBottom="true">
</LinearLayout>
They are really the same, so they also have the same ID.
My solution was to change the ID of every xml file.
For the login.xml,
<LinearLayout android:id="#+id/loginFooter"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="#layout/footer"
android:layout_alignParentBottom="true">
</LinearLayout>
and for the main.xml,
<LinearLayout android:id="#+id/mainFooter"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="#layout/footer"
android:layout_alignParentBottom="true">
</LinearLayout>
By doing this, it solved my problem!
but I don't know why eclipse does not return errors when compiling having the same IDs on layouts.

LinearLayout isn't stretching. Filename collision?

Would you mind pointing out the reason why LinearLayout element isn't stretching to match parent element. Is this related to project configuration or class names I use ? I've noticed that name of the xmlfile might be somehow related to this issue. I'm not joking - a couple of days ago I was testing the same code on different xml file names and it worked differently in terms of LinearLayout stretching. This one is named "clip.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"
android:clickable="true" >
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</FrameLayout>
Thanks
i see nothing wrong with what you did , but you won't notice a thing in the linearLayout since it doesn't contain anything .
in order to demonstrate it , try setting a background for the linearLayout , for example :
android:background="#88ff000000"

How to draw 2 PNG Images to the screen at the same time

I'd like to know how to draw two PNG pictures onto the screen.
My XML layout: (named paperxml.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/paperid"
android:src="#drawable/paperrepresentation"
/>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rockid"
android:src="#drawable/rockrepresentation"
android:layout_alignTop="#id/paperid"
/>
</RelativeLayout>
What would be the java code to instantiate the XML layout and display both ImageViews on the screen at the same time?
Simply calling setContentView(R.drawable.paperxml); crashes my application on startup.
Replace the xml with:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView android:id="#+id/paperid"
android:src="#drawable/paperrepresentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView android:id="#+id/rockid"
android:src="#drawable/rockrepresentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Explanation:
RelativeLayout doesn't use android:orientation="vertical".
Every view should have android:layout_width and android:layout_height.
Add the xmlns:android thing just in the first element.
Calling setContentView(R.drawable.paperxml); is not crashing your code - it's your XML file. Macarse has the correct answer to your problem, and keep your code the same!
You may also want to look at the View Tutorials for some examples of setting up your XML and using different View objects.
I put the XML in but it only displays one ImageView Here's a screenshot of the emulator I took. i852.photobucket.com/albums/ab87/thomasjakway1/Capture.png Its worth mentioning that the file shown is paperrepresentation
If you look hard enough you will see that at the bottom there is a second very tiny image. You just need to increase the scale.

How to get RelativeLayout working with merge and include?

I have been trying for a few days now to make my layouts more efficient by converting from using several levels of nested LinearLayouts to one RelativeLayout and have come across a few problems that I haven not been able to find a workaround for...
I have searched the Android beginners group and this site and have not been able to find anything that would help me solve the problem.
I read on one of the blogs that you can combine layouts with merge and include tags. So what I have is a main layout file with a RelativeLayout root element. Inside of that I have 5 include tags that reference 5 different xml layout files that each have a merge element for the root (all of my merge files are the same except for the ids in them).
I am running into two problems, which I will explain after posting a simplified version of my layout code:
Sample Main Layout File:
<?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"
android:background="#drawable/translucent_gray" >
<include
android:id="#+id/running_gallery_layout_id"
layout="#layout/running_gallery_layout" />
<include
android:id="#+id/recent_gallery_layout_id"
layout="#layout/recent_gallery_layout"
android:layout_below="#id/running_gallery_layout_id" />
<include
android:id="#+id/service_gallery_layout_id"
layout="#layout/service_gallery_layout"
android:layout_below="#id/recent_gallery_layout_id" />
<include
android:id="#+id/process_gallery_layout_id"
layout="#layout/process_gallery_layout"
android:layout_below="#id/service_gallery_layout_id" />
</RelativeLayout>
Sample included merge file:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
style="#style/TitleText"
android:id="#+id/service_gallery_title_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/service_title" />
<Gallery
android:id="#+id/service_gallery_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_below="#id/service_gallery_title_text_id" />
<TextView
style="#style/SubTitleText"
android:id="#+id/service_gallery_current_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/service_gallery_title_text_id"
android:layout_above="#id/service_gallery_id" />
</merge>
I am running into two problems:
1) The android:layout_* attributes seem to be ignored when used in the include tag and all of the merged layouts are displayed on top of each other. According to this post (http://developer.android.com/resources/articles/layout-tricks-reuse.html) "any android:layout_* attribute can be used with the <include /> tag"
2) Since I couldn't get this working I decided to try adding an android:layout_below attribute to the first TextView item in each merge layout file, meaning that each merge file would be referencing an id from another merge layout file... For the most part this actually worked and my layout looks fine. However, I get an error on one of the android:layout_below attributes saying that it can't find the id I specified... I have double and triple checked the ids to make sure they were correct. The weirdest part is that I used the AutoFill feature to put the id in the attribute in the first place.
If anyone has any suggestions or workarounds I will be more than happy to try them out. Also, if anyone can think of a way for me to just have one merge xml layout file instead of 5 that would be greatly appreciated. I couldn't find a way to do that because I need to have access to each item in the merge layout files at runtime...
There is an issue with the include tag. Check: https://issuetracker.google.com/issues/36908001
To fix it, make sure you overwrite BOTH layout_width and layout_height when including, otherwise everything will be ignored.
See the more highly voted answer below. Mine is woefully outdated
i can address one issue Justin raised: inability of RelativeLayout to manage positioning of an include (at least in this simple case, on a 1.6 emulator)
CommonsWare suggests wrapping the includes in a unique parent container, but does so in order to assist addressing & scoping identically named Views within Justin's includes
Each would have to have a
unique parent container, and you would
call findViewById() on that container
(ViewGroup) rather than on the
Activity.
In fact, you also must do it in order to get RelativeLayout to behave as expected:
This works (footer is well positioned):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<include android:id="#+id/header" layout="#layout/header"
android:layout_alignParentTop="true" />
<WebView android:id="#+id/webView" android:layout_below="#id/header"
android:background="#77CC0000" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:focusable="false" />
<LinearLayout android:layout_alignParentBottom="true"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<include android:id="#+id/footer" layout="#layout/footer" />
</LinearLayout>
</RelativeLayout>
This does not (footer is floating at top of screen):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<include android:id="#+id/header" layout="#layout/header"
android:layout_alignParentTop="true" />
<WebView android:id="#+id/webView" android:layout_below="#id/header"
android:background="#77CC0000" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:focusable="false" />
<include android:id="#+id/footer" layout="#layout/footer"
android:layout_alignParentBottom="true" />
</RelativeLayout>
The bare footer include will not align to bottom of parent, without the surrounding LinearLayout.. I wouldn't call this expected behavior.
Additionally, the WebView appears to attach itself nicely to the header by ID, but I believe this to be illusion, due to it simply flowing below the header vertically. I also tried to set a button right above the footer include, but it got all floaty and wrong, too
RelativeLayout had more problems in 1.5, but i still like it :)
Man, this is old, but it seems to come up at the top of searches, so I'm going to comment.
I think the trick here is that the <merge> tag combined with the <include> tag essentially remove any sort of "parent" view group at that level. So then, who exactly are you asking to "layout_below" someone else? No one. There is no view at that level.
The <merge> tag takes the child views and pops them right into the parent of the <include> tag. You must therefore ask the children in the layout you're including to anchor themselves accordingly.
For positioning to work on RelativeLayout you need to set the layout_* parameters in the include file, not in the main layout file. That way
main_layout.xml
<RelativeLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
....
</RelativeLayout>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
.....
</RelativeLayout>
<include layout="#layout/content_layout" />
content_layout.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/footer"
android:layout_below="#id/header" >
....
</RelativeLayout>
</merge>
This is obviously not what us developers want, but it's the only solution I've found to avoid duplicating xml
The android:layout_* attributes seem
to be ignored when used in the include
tag and all of the merged layouts are
displayed on top of each other.
My guess is that you cannot reference, from layout rules, android:id attributes that are defined on <include> elements, only ones that are on "real" widgets and containers.
Also, if anyone can think of a way for
me to just have one merge xml layout
file instead of 5 that would be
greatly appreciated.
Simple: put them all in one file.
I couldn't find a way to do that
because I need to have access to each
item in the merge layout files at
runtime
Whether you have one <include> element or 1,000, all of the contents should be accessible at runtime. One exception is if you have duplicated android:id attributes -- you would need to properly scope your findViewById() calls to get the right one, just like you have to when getting widgets from a ListView row.
If you can create a sample project that uses 2+ merge files where you can demonstrate that the contents are not accessible at runtime, let me know.
try :
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:showIn="#layout/activity_home">
In my case, the layout which I was trying to include starts with <merge tag. When I changed it to a layout, say <RelativeLayout it worked. Below is the illustration.
WORKING
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:showIn="#layout/activity_home">
NOT WORKING
<merge xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:showIn="#layout/activity_home">
I had the same problem and even defining layout_width and layout_height it didn't work.
The problem was that the layout I was including had tags and after removing them, everything worked like a charm.
I suppose that merge is not a layout tag and because of that, it cannot receive positioning and size parameters. Since everything you define in is transferred to the inner parent layout, the settings just were throw away.
TL:DR: Just remove the tags, move the xmlns definitions to a real layout viewholder and you should be good.
Before:
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<...ui.component.BorderCardView
android:layout_width="112dp"
android:layout_height="32dp"
app:cardCornerRadius="4dp"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/ic_logout"
android:tint="#color/divider" />
</...ui.component.BorderCardView>
</merge>
Working:
<...ui.component.BorderCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="112dp"
android:layout_height="32dp"
app:cardCornerRadius="4dp"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/ic_logout"
android:tint="#color/divider" />
</...ui.component.BorderCardView>

Categories

Resources