I am trying to create a widget that displays items parsed form an XML file file. In the end, an image, a title, and URL of the original item are going to be displayed. But, now that I am working on the layout, I am getting a runtime error after adding the ImageView to its container. The <RelativeLayout android:id="#+id/newsdviewlayout" is inside the root View (which is a RelativeLayout) for the main layout. Help please.
<RelativeLayout
android:id="#+id/newsdviewlayout"
style="#style/newsviewstyle"
android:layout_alignParentBottom="true">
<ImageView
android:id="#+id/newsviewimg" />
</RelativeLayout>
Yeah, I think you miss the required attribute android:layout_width="" and android:layout_height="", so for your case, the xml will be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/newsdviewlayout"
style="#style/newsviewstyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true">
<ImageView
android:id="#+id/newsviewimg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Or as for the RelativeLayout you can also put that 2 attribute inside your newsviewstyle style.
You have to give the android:layout_width and android:layout_height for both RelativeLayout as well as the ImageView.
Related
I have a RecyclerView that has to inflate the following layout in the ViewHolder:
<?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"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.percent.PercentRelativeLayout
android:id="#+id/imageRelative"
....
....
<LinearLayout
android:id="#+id/premiumBlockerView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:alpha="0.5"
android:background="#color/Black"
android:layout_alignBottom="#id/linearLayout1" />
</RelativeLayout>
The RelativeLayout's content changes (image, video, text, etc.) so I'm not able to know the height (the width is the width of the device).
I have to add a view (my premiumBlockerView LinearLayout) that I will show as semi transparent glass or not depends on content type.
The problem is that the height=match_parent in the premiumBlockerView doesn't work and it is allways zero. So, like others post suggested, I added the layout_alignParent and alignBottom parameters and this work arround solution works propery for APIs greater than 20.
Now the application must to work for API 19 and this work arround doesn't work.
Do you have any suggestion?
Thanks!!!
I face the same problem too. I am sorry that i still can not find a perfect solution in RelativeLayout, But you can solve this problem by using ConstraintLayout or another layout instead of RelativeLayout.
Note:
if you set parent RelativeLayout
android:layout_height="wrap_content">
and child view
android:layout_height="match_parent"
in RecylerView.
then the child view will get
"wrap_content"
instead of match_parent, you can find why by reading source code of RelativeLayout onMeasure method.
I have a ListView and an ImageView contained in a LinearLayout. Unfortunately, ListView doesn't scroll the ImageView together with it.
I've tried to use ScrollView to wrap both components, but there is a problem with ListView if I did so.
Anyone know how to make all of the components inside LinearLayout scroll?
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="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgCanvas"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imgCanvas"
android:smoothScrollbar="true" />
</LinearLayout>
Here is the screenshot of how it looks right now:
You can make your ImageView in another layout file, inflate it and add as HeaderView.
For example:
View header = LayoutInflater.from(this).inflate(R.layout.your_imageview_layout, null);
yourListView.addHeader(header, null, false);
You should try to set your image as header of your list.
Use the following function to do this:
yourListView.addHeaderView(yourImageView, null, false);
I'm trying to place a TextView at the bottom of every activity by using the include... My code is below... I don't know why, but it remains at top... Although it works fine for TextView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#E0E0E0"
android:orientation="vertical" />
<include
android:layout_alignParentBottom="true"
android:id="#+id/footer"
layout="#layout/footer" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:text="Hello"
android:textColor="#000000"
android:textSize="15dp" />
¿Maybe you are using a LinearLayout instead of a RelativeLayout? The latter doesn't have android:orientation property. We don't see the parent XML node, so just a guess
Also, you'll need to place the include at the bottom and the TextView above it
<TextView
...
android:layout_above="#id/footer" />
Right now you are overlapping both views
Another suggestion is to use FragmentActivity and a Fragment to handle the Footer, but that's out of the scope of this question.
Use RelativeLayout in your same xml code as the parent layout.
Well i got the solution.... I had to add the include inside another child relative layout...This is what i did..
enter code here
RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<include
android:id="#+id/footer"
android:layout_alignParentBottom="true"
layout="#layout/footer" />
</RelativeLayout>
but...for a textview it works directly without using another child layout...So why do you need to do this for an include.....????:|
I checked your code. It works fine as posted in 1st post.
Please note that when you use layout tag then Eclipse' Graphical Layout won't draw the child xml-layout you've included (in your case footer.xml).
At the same time, you'll get a footer like you want if you run same code on actual device or emulator.
NOTE: you might want to remove android:layout_alignParentBottom="true" from textView1 as currently textView1 and footer layouts overlaps.
I have a problem with my ScrollView. It has an ImageView inside it. The imageView holds an image which is 480 * 3000 px. So It needs to be in a ScrollView so the user will have the ability to scroll down.
The problem is: when I test the application, the ScrollView does not wrap to the image hight. There is a black space under the image which is strange for me.
This is my XML code. I look forward for your opinion
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:src="#drawable/image01"
android:scaleType="fitStart" />
</ScrollView>
</RelativeLayout>
Update:
This is how my image looks like with #ruhalde code. I can't take a screenshoot since it is big and you'll not see the whole picture so I draw it.
Rewrite your layout. Make the ScrollView the outer most element. Take a look at this good tutorial for more information: http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts
Use a LinearLayout instead of RelativeLayout which is deprecated by now. Set fillViewPort=true in your ScrollView XML or programatically if you want with setFillViewPort(true).
Put imageview inside a LinearLayout and that inside ScrollView, your XML should look like this:
LinearLayout
---ScrollView
----LinearLayout
------ImageView
Check out my XML here working good with a 800 x 4000 pixels image, perfectly scrolling vertical:
<?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">
<ScrollView android:id="#+id/scrollView1"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="#+id/mapa" android:layout_height="wrap_content"
android:layout_width="wrap_content"></ImageView>
</LinearLayout>
</ScrollView>
</LinearLayout>
To those who might be interested:
My problem was with the image I'm using, not how I organize my layouts. Thanks.
I believe you should use scrollView tag before your layout tag
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.