i have simple gridview. it's elements have different height. for example in row there are one small element and one bigger, next row will align to smaller element and a part of bigger element is under the second row. how can i set the height of each row of gridview to be the height of the biggest element in row??
my gridview
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/new_small_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:verticalSpacing="3dp"
android:horizontalSpacing="3dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:cacheColorHint="#android:color/transparent"
/>
For setting cell height, in GridViewAdapter when you write, for example:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater cellLayout = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = cellLayout.inflate(R.layout.gridcell_layout, null);
}
TextView cell_title = (TextView) convertView.findViewById(R.id.gridview_cell_text);
ImageView cell_icon = (ImageView) convertView.findViewById(R.id.griview_cell_image);
....
Just substitute this line:
convertView = cellLayout.inflate(R.layout.gridcell_layout, null);
With this:
convertView = cellLayout.inflate(R.layout.gridcell_layout, gridView, false);
By this way, setting parent reference in "inflate", Width and Height about cell will set!
Here you can find why:
Layout problem with button margin
Related
The items in my list view and reorganizing themselves unexpectedly. Meaning when I scroll down the same list item is shown. This is very odd behavior. Has anyone encountered this?
#Override
public View getView(int position, View view, ViewGroup parent) {
View row = view;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
......
//this view is on xml file (container)
LinearLayout ll = (LinearLayout) row.findViewById(R.id.listViewControllers);
//adding my own views to a linear layout container dynamically...
.......
}
}
return row;
I decided to inflate another list item view inside my getView using the inflater. all inside a for loop. However I'm having problems setting the text for my textview. Rows after first does not want to set Text properly.
Here is a code snippet inside getView
for(Controller aController : controllers) {
inflater.inflate(R.layout.list_view_item, ll, true);
TextView tv2 = (TextView) row2.findViewWithTag("controller_name");
tv2.setText(aController.getName());
}
Here is list_view_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_controller_cell"
android:orientation="vertical" >
<TableRow
android:id="#+id/tableRowBatchController"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:tag="controller_name"
android:text="name"
android:textColor="#ffffff" />
</TableRow>
</LinearLayout>
Thanks in advance. Still looking as to why the list item is not setting Text properly on "controller_name" text view
This is expected, ListView reuses list items which goes offscreen to show visible items. Add 'else' branch which doesn't inflate new View, but fills existing one with item data.
I created app to display images from internet in carousel view. I loaded images into carousel view using imageview. just like following way.
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.showsmain, null);
ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
// RelativeLayout rlayout=(RelativeLayout)vi.findViewById(R.id.rlayouts);
image.setImageResource(R.drawable.black);
orgWidth = image.getDrawable().getIntrinsicWidth();
orgHeight = image.getDrawable().getIntrinsicHeight();
imageLoader.DisplayImage(data[position], image);
imageLoader.getDimension(widthScreen, heightScreen);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthScreen, heightScreen/3);
params.width=widthScreen;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_XY);
//rlayout.addView(image);
return vi;
}
and my carousel layout xml file is,
<com.touchmenotapps.carousel.simple.HorizontalCarouselLayout
android:id="#+id/carousel_layout_event"
android:layout_width="600dp"
android:layout_height="500dp"
android:layout_above="#+id/relativeLayout1"
android:layout_alignTop="#+id/carousel_layout" >
</com.touchmenotapps.carousel.simple.HorizontalCarouselLayout>
then image show as following way in carousel,
![enter image description here][1]
Here i want to increase the image width & height. It means one images must load fill with in the screen
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.showimage, null);
ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
//LinearLayout rlayout=(LinearLayout)vi.findViewById(R.id.layout);
image.setImageResource(R.drawable.black);
orgWidth = image.getDrawable().getIntrinsicWidth();
orgHeight = image.getDrawable().getIntrinsicHeight();
imageLoader.DisplayImage(data[position], image);
imageLoader.getDimension(widthScreen, heightScreen);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthScreen,heightScreen/3);
params.width=widthScreen/2;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_XY);
//rlayout.addView(image);
return vi;
}
this way I add the imageview into carousel view.
how to do that. I asked question similer this one. But i didn't get answer. pls help me someone. pls.....
my imageview xml 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:id="#+id/rlayouts">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:layout_alignParentTop="true" />
</RelativeLayout>
I think you should use a Horizontal oriented Linear layout inside a Horizontal ScrollView then you should add ImageViews with FillParent Height and Width in this Linear layout.
It would be great if you elaborate your question a bit more.
1.Check condition only display
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.main_alllatestnewslist, parent,
false);
ImageView imageview = (ImageView) vi
.findViewById(R.id.image_alllatestnewstitle);
imageview.setVisibility(View.INVISIBLE);
TextView titletext = (TextView) vi
.findViewById(R.id.text_particularlatestnewstitle);
TextView categorytext = (TextView) vi
.findViewById(R.id.text_newscategorytitle);
TextView datetext = (TextView) vi.findViewById(R.id.text_newsdate);
if (!imagepath[position].toString().equals("no picture")) {
imageLoader.DisplayImage(imagepath[position], imageview);
imageview.setVisibility(View.VISIBLE);
titletext.setWidth(460 - imageview.getWidth() - 5); <-- this line
} else {
imageview.setVisibility(View.INVISIBLE);
imageview.setImageDrawable(null);
}
titletext.setText(title[position].toString());
categorytext.setText(category[position].toString());
datetext.setText(date[position].toString());
return vi;
}
The statement cannot be check when first view of the list and causing the textview stack with imageview. After scroll one or more times it only move to left.
How to go in this statement before display it?
2. set all items' height in listview programmatically base on condition
RelativeLayout childlayout = (RelativeLayout)vi.findViewById(R.id.layout_childlist);
This is the layout for an item.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_childlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/list_bg"
android:minHeight="?android:attr/listPreferredItemHeight" >
<LinearLayout
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="5px"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:layout_marginTop="5px" >
<ImageView
android:id="#+id/image_alllatestnewstitle"
android:layout_width="140px"
android:layout_height="80px"
android:scaleType="centerCrop" />
</LinearLayout>
....
</RelativeLayout>
For example, one of the item took 80px to display but the other items only took 40px. However, the other items will not wrap to 40px and wrap to 80px and make all the items has same height.
How to set the height for all items with difference height?
Try imageview.setVisibility(View.GONE);
INVISIBLE only hides the view while GONE 'removes' it
<?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/venueImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I have an ordinary listview and a custom adapter using the xml above for the row.
I will only have 3 rows, but I want those 3 rows to have equal heights to fit the height of the screen's resolution. At the moment I don't even know how to set the height of the rows because setting the LinearLayout's height doesnt do anything.
You can set the height of your ImageView to get what you want. To set it programmatically, you can do this on the getView() of your custom adapter:
public View getView(int position, View convertView, ViewGroup parent) {
View cv = convertView;
if (convertView == null) {
cv = inflater.inflate(R.layout.listview_item, null);
}
ImageView venueImage = (ImageView) cv.findViewById(R.id.venueImage);
LinearLayout.LayoutParams vi_params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, (int)(screenHeight*0.33);
venueImage.setLayoutParams(vi_params);
return cv;
}
You can get the screen height by adding this code on your main activity:
int screenHeight = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
I'm pretty new to Android, and I'm looking for a little help.
I have a GridView with the following XML in a linear layout.
<GridView android:id="#+id/gridview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:numColumns="2"
android:columnWidth="90dp"
android:stretchMode="columnWidth"/>
I'm adding a series of CheckBoxes to the view with...
public View getView(int position, View convertView, ViewGroup parent)
{
CheckBox checkView;
if (convertView == null) {
checkView = new CheckBox(context);
checkView.setPadding(5, 5, 5, 5);
} else {
checkView = (CheckBox) convertView;
}
checkView.setText(checkNames[position]);
checkView.setWidth(400);
return checkView;
}
No matter how many different widths I try, including fill_parent and
wrap_content, the text winds up behind the box instead of next to it.
Any hints or tricks?
Thanks,
Andrew
Try using only wrap content for both layout width and layout height. Because the fill_parent will fill the full layout, where as wrap_content will fill only to the size of your text entered.