android gridview cell size - android

In my gridview, all the cell initially render as rectangles, but the second time it is viewed they are square.
I have to logic to make them square, but I want them square the first time they are loaded
my problem is that I don't understand why the views are initially rectangles, or any particular proportion at all!
my XML files are all set to fill parent, and I even put in a square placeholder image in an attempt to force the views to be rendered square
is there something about gridview I should know about?

create a custom array adapter.(java)
see ( Showing graphical instead of strings in an imagelist with title )
in custom array adpater xml file (cell) write something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="50dp"
android:layout_height="50dp" >
<ImageView
android:id="#+id/iv_caarow"
android:src="#drawable/icon"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
android:layout_width="50dp", you can set cell size like this.

The answer was to extend my ImageView within the gridview cell, and #Override the onMeasure method and dynamically generate square proportions there

Related

Listview Header Wrap_content causing weird layout

I have a listview with header attach using method addHeaderView(header);
same listview layout loading different data for same fragment depdens upon navigation drawer category.
header is having its own layout
{
**R.layout.header_layout**
<?xml version="1.0" encoding="utf-8"?>
<com.loopj.android.image.SmartImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/desc"
android:scaleType="centerCrop"
android:baselineAlignBottom="true"
android:src="#drawable/blankk" />
}
Note: SmartImageView is library that loads image from url .
my problem is the, images that are loading into smartimageview (listview header) are not having fixed height so the layout for different fragment are different i.e not consistent .if i assign height as say 300dp images are getting crop.
plz help me to get get header of same height with images not getting crop.
Please remove android:baselineAlignBottom="true" from your smart imageview and use fix width. I hope it will works

Adding bottom margin to ListView last element

I need to add to add ListView with complicated items background: different for even/odd and rounded corners at the top and bottom. It looks like this:
I have implemented all this stuff via level-list, but there is one more thing I want to do.
Now the bottom item is near the bottom of the screen. It is better to add some space.
I don't want to add bottom margin to ListView, I need margin only for last item.
The ways I see to do this:
Footer
A kind of hack – add footer with empty TextView to ListView. But footers are quite unstable things, they usually disappear after notifyDataSetChanged and there is no way to get them back
Image with transparent pixels
I asked designer to add transparent pixels to bottom background resource. Unfortunately, in this case vertical centering is completely broken.
For example, there is 9patch like this:
And layout like this:
<?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"
>
<!-- View with background with transparent pixels on bottom -->
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="#+id/item"
android:background="#drawable/some_bgr"
android:padding="10dp"
>
<TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
android:text="Title"
android:layout_gravity="center"
android:textSize="18sp"
/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Detail"
android:layout_gravity="center"
android:textSize="18sp"
/>
</LinearLayout>
<!-- Just for marking place took by view -->
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_below="#id/item"
android:background="#88ff55"
/>
</RelativeLayout>
The result:
As you see, centering is not working. Unfortunately.
(BTW, if specify this 9patch as background for TextView, centering works good. If you know any article, explaining this, please let me know.)
Add bottom margin to last item in Adapter implementation
That should work, but for unknown reason I still can't get it work.
I don't like this way, because I don't like to modify dimensions in code.
So
There is already imaginary way – construct some XML drawable with particular bitmap and margin. According to drawables concept it should be possible, but I can't find implementation. May be somebody knows?
Any other ideas?
In your ListView, set a paddingBottom and clipToPadding="false".
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"/>
This also works for RecyclerView.
Only use android:scrollbarStyle="outsideOverlay" if you want the scroll bar to not overflow into the padded area.
add an empty footer in your list like this:
TextView empty = new TextView(this);
empty.setHeight(150);
listview.addFooterView(empty);
you can also do it from code if you want, for example here I react to
to EditText different situations:
if(s.toString().length()>0)
{
contacts_lv.setClipToPadding(false);
contacts_lv.setPadding(0,0,0,270*screenDensity);
}
else
{
contacts_lv.setClipToPadding(true);
contacts_lv.setPadding(0,0,0,0);
}
Clocksmith's answer is the best and pretty clever. You can also create an empty footer view.
Add these two lines in your listView XML code:
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"
Another solution might be that you make a mock view with certain height.
In your adapter in getViewCount return 2.
In getCount return yourData.size+1.
In getViewType check if the element is last element return 2;
Use this type in getView to populate the mockview.
I guess you want to add margin only to last item:
So you can do in this manner, in your getview method the index of the list item and check if its the last item, then progrmatically add margin to the view.

How to set these two different images using position in android?

I have two different images and would like to set them using different views but overlap slightly to match the below images. I also want to be able to set a different OnClickListener for each of them. I know with iOS I can set the view positions using x and y values but I'm not sure how to do this in Android.
How can I go about doing this?
Desired result:
Image One
Image Two
Try set those two images as ImageView's and put them inside a FrameLayout. That way you can set them as you want, So they can overlap one another. That way you can set an clickable and onClick properties for both of them separately:
<?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:orientation="vertical" >
<ImageView
android:id="#+id/iFirstImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#drawable/sort_image_1"
android:clickable="true"
android:onClick="setFirstImageClick"
android:src="#drawable/sort_image_1" />
<ImageView
android:id="#+id/iSecondImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#drawable/sort_image_2"
android:clickable="true"
android:onClick="setSecondImageClick"
android:src="#drawable/sort_image_2" />
And create methods setFirstImageClick and setSecondImageClick in your activity to decide what each image click should do.
You can place the first image however you want, and then in the xml for the second image use
android:layout_toRightOf="firstImageId"
You can set each onClick separately after that.

GridView having 1 ImageView per row in portratit and 2 ImageViews in landscape mode

I actually have a ListActivity fed by a custom ArrayAdapter. It builds rows made by single ImageViews. In portrait mode it looks nice. But in landscape mode it stretches the image to the borders (I set scaleType to fitXY).
I would like to have 2 ImageViews per row in landscape mode. Is GridView the right layout?
How would you do this?
Since I have a 480x800 screen, in portrait, the ImageView would be 480px wide, while in landscape each of the two would be 400px wide. This is not a problem, but it is important to me to respect the width/height ratio.
Why don't you use Gridview to do your task. If you want to use listview you have to check for the orientation, based on the orientations
1) In landscape mode the layout from the layout-land/ will be loaded which contains the 2 imageviews.
2) In portrait mode the layout from the layout-port/ will be loaded which contains the 1 imageview.
Yes, GridView is the best and easiest approach. You can use the same custom ArrayAdapter and OnClickListener you have now for the ListView, and only need to change
ListView myList = (ListView) findViewById(R.id.list);
to
GridView myGrid = (GridView) findViewById(R.id.gridview);
and the xml to something like:
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="390px"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
Now, you should avoid using "px" as units and use "dp" instead, and that only if you are really sure your app is designed only for a 480x800 resolution.
gridView is not much of a layout . it's an adapterView , so its purpose is to be able to "show" tons of views and also be able to scroll between them .
maybe you would like to use gridLayout instead ? it also has a comparability library ...
anyway , if you wish to stick with gridView , simply set the android:numColumns to be of a variable that you set via the values folder for portrait mode , and and values-land for the value you need for landscape
EDIT: here's the solution of using a gridView :
the layout xml file should contain something like:
<GridView android:layout_width="match_parent"
android:numColumns="#integer/cols" android:layout_height="match_parent"
/>
create an xml file (for example "consts.xml") in the res/values folder that has something like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="integer" name="cols">2</item>
</resources>
also , create the same file in res/values-land folder , and set the 2 to be 3 .
You can supply different resources for different orientations. So, when the device is in landscape, you populate the ListView with a layout that has two ImageViews in every item and in portrait, the layout has 1 ImageView per item.
Take a look at Providing Resources to know about how you can provide different layout XMLs for different orientations.
Sample code:
// Create an anonymous implementation of OnClickListener
private OnClickListener mImageListener = new OnClickListener() {
public void onClick(View v) {
// do something when the ImageView is clicked
}
};
//Check the orientation and handle accordingly in the getView
public View getView (int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = (LinearLayout)inflater.inflate(R.layout.list_item, parent);
}
ImageView leftImage = (ImageView)convertView.findViewById(R.id.leftImage);
ImageView rightImage = (ImageView)convertView.findViewById(R.id.rightImage);
boolean isTwoColumn = (rightImage != null);
//If it is two column layout, set both ImageViews
if(isTwoColumn){
leftImage.setImageResource(...);
leftImage.setOnClickListener(mImageListener);
rightImage.setImageResource(...);
rightImage.setOnClickListener(mImageListener);
}else{
leftImage.setImageResource(...);
leftImage.setOnClickListener(mImageListener);
}
}
/res/layout-land/list-item.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="+#id/leftImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="+#id/rightImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
/res/layout-port/list-item.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="+#id/leftImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
I tried Gridview and personally found it was incredibly fiddly to get the display I was after (which was ratio correct display for smaller cells inside)... after weeks of restling with it on different resolutions and screen formats I opted for creating my own gridview from scratch and haven't looked back.
I'm not saying Gridview isn't without it's merit but what I can say is to implement your own system takes a matter of hours if you have a very clear idea in your mind how it should operate you'll probably find it easier to code your own.
In essence it was just a class derived from ViewGroup, override the onMeasure and onLayout items to measure the child views it contains and next lay them out into the locations on screen that you want them.
If you're looking for long lists however I'd still look at the listview or perhaps gridview might be worth a shot but this is certainly good peace of mind to know you can take control of it all if you need to.

Multiple views on top of an ImageView

I'm trying to port an app from iOS to Android. In my iOS app I have an image view which displays a map, and on top of it I want to display multiple button views on certain specific positions.
None of the standard views in Android seem to fit my needs, but I am surely missing something. How could I achieve this?
Thanks
The easiest thing to do would be to use any layout that's best suited for your needs (LinearLayout, FrameLayout, RelativeLayout, etc.) and set its background to any image you need. I believe the image will automatically scale to fill the corresponding Layout. Put the image into the appropriate drawable folders - and format your XML similar to this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_image">
...
</RelativeLayout>

Categories

Resources