Gridview return only one row - android

I am using a GridView in a Relative layout. I want the GridView to show
up as tall as needed to show all the rows. However, I am seeing only
one row when set (layout_width=MATCH_PARENT and)
layout_height=WRAP_CONTENT. If I set layout_height to a size that
equals mulitple rows, then I do see those rows.
<?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:gravity="center"
>
<RelativeLayout
android:id="#+id/layout_month"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/textv_month_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="center_horizontal|center_horizontal"
android:textColor="#ffffff"
android:textSize="7dp" />
<ImageView
android:id="#+id/top_bar"
android:layout_width="fill_parent"
android:layout_height="15dp"
android:layout_below="#+id/textv_month_name"
android:background="#drawable/calendar_top_small"
android:clickable="false" />
<GridView
android:id="#+id/gridv_inner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top_bar"
android:layout_gravity="center"
android:clickable="false"
android:gravity="center"
android:numColumns="7"
android:stretchMode="columnWidth"
>
</GridView>
<Button
android:id="#+id/monthBtn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" />
</RelativeLayout>
</RelativeLayout>
Any Help will be appreciable

This is a very useful answer.
The GridView didn’t like the wrap_content of its height when it was put in a vertically tight space; it only showed a single row of items (scrolling all content in that single row).
The short version of my fix includes sub classing the GridView and overriding the onMeasure method. The essence of the fix focuses on setting the measured height spec to a ridiculously large number.
MyGridview.Java
package com.arun.calendar;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec;
if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
// The two leftmost bits in the height measure spec have
// a special meaning, hence we can't use them to describe height.
heightSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >>2, MeasureSpec.AT_MOST);
}
else {
// Any other height should be respected as is.
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
And the new XML 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:gravity="center" >
<RelativeLayout
android:id="#+id/layout_month"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textv_month_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="center_horizontal|center_horizontal"
android:textColor="#ffffff"
android:textSize="7dp" />
<ImageView
android:id="#+id/top_bar"
android:layout_width="fill_parent"
android:layout_height="15dp"
android:layout_below="#+id/textv_month_name"
android:background="#drawable/calendar_top_small"
android:clickable="false" />
<com.arun.calendar.MyGridView
android:id="#+id/gridv_inner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top_bar"
android:layout_gravity="center"
android:clickable="false"
android:gravity="center"
android:numColumns="7"
android:stretchMode="columnWidth" />
</RelativeLayout>
<Button
android:id="#+id/monthBtn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" />
</RelativeLayout>
And in java file
MyGridView viewHolder.gridView = (MyGridView) v.findViewById(R.id.gridv_inner);

Related

GridView is not showing last row

I have the following GridView:
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="120dp"
app:layout_constraintTop_toTopOf="parent"
android:numColumns="2"
android:gravity="center" />
and the following Item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<TextView
android:id="#+id/planlayout_teams_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Tambora 0 - 0 Balagath"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center"/>
<TextView
android:id="#+id/planlayout_status_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="noch unentschieden"
android:gravity="center"/>
</LinearLayout>
<TextView
android:id="#+id/planlayout_menu_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text=".\n.\n."
android:textSize="12sp" />
</LinearLayout>
And everything is working fine except for the last item(s). Whether there is one or two items left, the whole row is not displayed. I googled a lot but nothing could help me.
Edit: I have the same "problem" as here: Custom GridView's Last row items not showing fully but those links don't help me.
I think the problem is about your layout. Your adapter load your all items but your layout not let you see last item because of your top margin.
You can edit your grid layout like this. Can you try this way.
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="120dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:numColumns="2"
android:gravity="center" />
just try this -->
<GridView
android:id="#+id/mGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:columnWidth="172dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"/>
Or try with custom gridview
your gridview code on layout something like this :--
<com.yourpackagename.MyGridView
android:id="#+id/mGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:adjustViewBounds="true"
android:columnWidth="90dp"
android:verticalSpacing="-2dp"
android:horizontalSpacing="-10dp"
android:stretchMode="columnWidth"
android:scrollbars="vertical"
android:fastScrollEnabled="true"
/>
MyGridview activity:-
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec;
if (getLayoutParams().height == LayoutParams.MATCH_PARENT) {
heightSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
}
else {
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
In your mainactivity (activity where you want to declare the gridview)
private MyGridView mGridView;
//also add your adapter of gridview
.
.
oncreate(){
mGridView = getView().findViewById(R.id.mGridView);
//then attach your adapter
adapter = new GridViewAdapter(getContext()...whatever parameters you have in your constructor);
mGridView.setAdapter(adapter);
try this custom gridview...still it doesnt worked let me know

RecyclerView inside a LinearLayout along with several other views [duplicate]

This question already has answers here:
RecyclerView inside ScrollView is not working
(26 answers)
Closed 6 years ago.
I have a recyclerView inside my LinearLayout.
<ScrollView 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:id="#+id/book_detail_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.BookDetailActivity">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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:orientation="vertical">
<ImageView
android:layout_width="250dp"
android:layout_height="270dp"
android:id="#+id/book_image"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_gravity="center"
android:src="#drawable/book_default"/>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#EEEEEE"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/book_authors"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:fontFamily="sans-serif-condensed" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:max="5"
android:id="#+id/book_rating"
android:stepSize="0.5"
android:paddingTop="10dp"
android:isIndicator="true"
android:progressTint="#FFA500"
android:secondaryProgressTint="#D1D6D8"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="5dp"
android:id="#+id/book_rating_text"
android:fontFamily="sans-serif-condensed" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/book_reviews_text"
android:textColor="#color/redColor"
android:fontFamily="sans-serif-condensed" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#EEEEEE"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:fadingEdge="vertical"
android:fadingEdgeLength="20dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/book_description"
android:onClick="toggleText"
android:ellipsize="end"
android:maxLines="3" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:id="#+id/book_description_below_line"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#EEEEEE"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/dark_translucent_black"
android:textAlignment="center"
android:textSize="15dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="People near you who are willing to lend this book"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/lender_recycler_view"
android:layout_width="match_parent"
android:layout_height="400dp"
android:scrollbars="vertical" />
</LinearLayout>
</ScrollView>
The problem is that when I inflate this recyclerView, the complete activity is not scrolling (Only the part of recycler view does). Moreover I have specify height of recyclerView otherwise I am not able to see it.
So my two problems are:
How to give this RecyclerView hight dynamically (layout_height: wrap_content isn't working)
How to scroll the entire activity (and not just the recyclerView part) when content is more.
P.S I am new to Android, though I have implemented recyclerView earlier but in those cases there was only one element (one recyclerView inside a parent LinearLayout)
ExpandableHeightListView.java
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
import android.content.Context;
public class ExpandableHeightListView extends ListView
{
boolean expanded = false;
public ExpandableHeightListView(Context context)
{
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightListView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
Add this customized java file and use it in xml.
and in your MainActivity do like this:
ExpandableHeightListView listview= (ExpandableHeightListView) findViewById(R.id.comment_listview);
and set adapter and set one property
listview.setAdapter("Your Adapter");
listview.setExpanded(true);

GridLayout with SquareRelativeLayout item

I succeed to create a SquareRelativeLayout. Then I use this CustomLayout as one grid item of a GridLayout. I have 5 items for now (but it can be variable). For the first row, everything is fine. My item has a size of 400*400 but for the second row I have something like 400*429. I think my issue come from above, indeed I use a LinearLayout and my GridLayout has a height of layout:weight 0.50. Here is my current layout :
<?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:id="#+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66000000">
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="0dp" ... />
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:grid="http://schemas.android.com/apk/res-auto"
android:id="#+id/droparea"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.50"
grid:columnCount="3"
grid:orientation="horizontal">
<SquareRelativeLayout
android:id="#+id/relativelayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
grid:layout_columnWeight="1"
grid:layout_gravity="fill"
android:background="#drawable/round_layout"
android:gravity="center_vertical|center_horizontal">
...
</SquareRelativeLayout>
<SquareRelativeLayout
android:id="#+id/relativelayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
grid:layout_columnWeight="1"
grid:layout_gravity="fill"
android:background="#drawable/round_layout"
android:gravity="center_vertical|center_horizontal">
...
</SquareRelativeLayout>
<SquareRelativeLayout
android:id="#+id/relativelayout3"
android:layout_width="0dp"
android:layout_height="wrap_content"
grid:layout_columnWeight="1"
grid:layout_gravity="fill"
android:background="#drawable/round_layout"
android:gravity="center_vertical|center_horizontal">
...
</SquareRelativeLayout>
<SquareRelativeLayout
android:id="#+id/relativelayout4"
android:layout_width="0dp"
android:layout_height="wrap_content"
grid:layout_columnWeight="1"
grid:layout_gravity="fill"
android:background="#drawable/round_layout"
android:gravity="center_vertical|center_horizontal">
....
</SquareRelativeLayout>
<SquareRelativeLayout
android:id="#+id/relativelayout5"
android:layout_width="0dp"
android:layout_height="wrap_content"
grid:layout_columnWeight="1"
grid:layout_gravity="fill"
android:background="#drawable/round_layout"
android:gravity="center_vertical|center_horizontal">
...
</SquareRelativeLayout>
</android.support.v7.widget.GridLayout>
<include layout="#layout/draggable_linearlayout"/>
</LinearLayout>
</RelativeLayout>
And here is the code of my SquareRelativeLayout :
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class SquareRelativeLayout extends RelativeLayout {
public SquareRelativeLayout(Context context) {
super(context);
}
public SquareRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
So I want to be able to set a variable height for my GridLayout but to keep squared items. Is it possible to let GridLayout fill the 29dp ? Did I miss an attribute ? If you can help me, don't hesitate :)
It was simple, but yesterday I didn't think about it. You just need to use a ParentLayout for the GridLayout and then set to the layout_height of the GridLayout the attribute wrap_content and that's it :)

how to display 2 grid view in android

sorry for my bad English ,I want to display 2 Grid views and one text view and image view in android ,Here below my XML code any one help me How to achieve that
I want to display 2 Grid views and one text view and image view in android
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="100sp"
android:src="#drawable/ic_launcher"
/>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/image"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<TextView
android:id="#+id/text"
android:layout_below="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Treding"
/>
<GridView
android:id="#+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/text"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</RelativeLayout>
</ScrollView>
What you should do is the following:
Expandable height gridview:
This means the more you add items to the GridView, the more it will expand. (This is what you need).
Now how to do it is simple:
First you need to create a java class:
public class ExpandableHeightGridView extends GridView {
boolean expanded = false;
public ExpandableHeightGridView(Context context)
{
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// This is a definite hack!!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = View.MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
View.MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
} }
Then in xml layout:
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="100sp"
android:src="#drawable/ic_launcher"
/>
<custom.ExpandableHeightGridView
android:id="#+id/grid1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="5dp"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<TextView
android:id="#+id/text"
android:layout_below="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Treding"
/>
<custom.ExpandableHeightGridView
android:id="#+id/grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="5dp"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</RelativeLayout>
</ScrollView>
Then in your activity:
grid = (ExpandableHeightGridView) findViewById(R.id.grid);
grid.setExpanded(true);
grid.setAdapter(your adapter);
**Let me know if you need further assistance **

How to resize an ImageView in Android?

I am trying to do something similar to the image below in Android. The problem is, whenever the screen size changes (when the user rotates the device), the checkboxes and the button disappear from the screen. This is my layout
<?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:id="#+id/preview"
android:layout_width="fill_parent"
android:contentDescription="#string/content_description"
android:layout_height= "1.5in" />
<CheckBox
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/preview"
android:text="#string/location_text" />
<CheckBox
android:id="#+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/location"
android:text="#string/timestamp_text" />
<Button
android:id="#+id/buttonUpload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/timestamp"
android:layout_marginRight="19dp"
android:gravity="center_vertical"
android:onClick="uploadClicked"
android:text="#string/upload_button_text" />
</RelativeLayout>
I need to display the image and all controls and support all screen sizes. How do I make the size of the ImageView dynamic?
I never got the scaleType thing to work for me.
I subclassed ImageView:
package your.pkg;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AspectRatioImageView extends ImageView {
public AspectRatioImageView(Context context) {
super(context);
}
public AspectRatioImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
if (getDrawable() != null && getDrawable().getIntrinsicWidth() != 0) {
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
} else {
setMeasuredDimension(0, 0);
}
}
}
And used that instead of the regular ImageView:
<your.pkg.AspectRatioImageView
android:id="#+id/photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
/>
This scales to the full width of the parent View and maintains the aspect ratio while doing so.
If you used a LinearLayout instead of a RelativeLayout, you could make the ImageView expand to take all the empty space in the screen. The scaleType attribute will let you specify how to stretch the image.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/preview"
android:layout_width="fill_parent"
android:contentDescription="#string/content_description"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
<CheckBox
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/location_text" />
<CheckBox
android:id="#+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/timestamp_text" />
<Button
android:id="#+id/buttonUpload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="19dp"
android:layout_gravity="right"
android:onClick="uploadClicked"
android:text="#string/upload_button_text" />
</LinearLayout>
Based on all the feedback. This is what I used:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/preview"
android:layout_width="fill_parent"
android:contentDescription="#string/content_description"
android:layout_height= "1.5in" />
<CheckBox
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/preview"
android:text="#string/location_text" />
<CheckBox
android:id="#+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/location"
android:text="#string/timestamp_text" />
<Button
android:id="#+id/buttonUpload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/timestamp"
android:layout_marginRight="19dp"
android:gravity="center_vertical"
android:onClick="uploadClicked"
android:text="#string/upload_button_text" />
</RelativeLayout>
</ScrollView>

Categories

Resources