<?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();
Related
I am trying to build an Application where there is a list-view with many items but I am not being able to change or set the width and height of single items.I have searched everywhere and the answer I got is making the width fill_parent,but its not working for me...
Kindly help.... thanks in advance... here are codes:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
tools:context=".CustomListViewAndroidExample" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</RelativeLayout>
If you want to change the height of list view dynamically, you can use
list.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));
or
import android.view.ViewGroup.LayoutParams;
ListView mListView = (ListView) findViewById(R.id.listviewid);
LayoutParams list = (LayoutParams) mListView.getLayoutParams();
list.height = set the height acc to you;//like int 200
mListView.setLayoutParams(list);
This link shows you how to do it in java with your own custom adapter.
When overriding the getView() in your adapter, your can modify the height before supplying your view to the framework for render. Also, note that you do not have to use a SimpleCursorAdapter, an ArrayAdapter can also be used in the same fashion.
final SimpleCursorAdapter adapter = new SimpleCursorAdapter (context, cursor) {
#Override
public View getView (int position, View convertView, ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
final TextView text = (TextView) view.findViewById(R.id.tvRow);
final LayoutParams params = text.getLayoutParams();
if (params != null) {
params.height = mRowHeight;
}
return view;
}
}
I have a custom adapter for a listview and here is the getView method;
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) JourneyPlannerActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.journey_planner_route_detail, viewGroup, false);
LinearLayout layout = (LinearLayout) rowView.findViewById(R.id.journey_planner_detail_detail_main_id);
JourneyPlannerRoute r = m_Routes.get(i);
String directions = "";
for(int j=0 ; j < r.getRoutes().size() ; j++){
ImageView image = new ImageView(JourneyPlannerActivity.this);
String transportMethod = r.getRoutes().get(j).getMeansOfTransport();
if(transportMethod.equals("Train"))
image.setImageResource(R.drawable.network_rail_logo);
else if(transportMethod.equals("Subway"))
image.setBackgroundResource(R.drawable.roundel_tube);
else if(transportMethod.equals("Bus"))
image.setBackgroundResource(R.drawable.bus);
else if(transportMethod.equals("Walk"))
image.setBackgroundResource(R.drawable.walking);
image.setVisibility(View.VISIBLE);
layout.addView(image);
//directions += r.getRoutes().get(j).getMeansOfTransport()+",";
}
directions += " "+r.getDuration();
TextView tv = (TextView) rowView.findViewById(R.id.journey_planner_detail_main_text_view);
tv.setText(directions);
return rowView;
After going through the debug it seems the imageviews are being added to the layout but they're just not appearing on the screen;
I had a feeling it was due to not picking up the correct layout but it seems as though it is?!
Here is the xml file for the row;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/journey_planner_detail_detail_main_id">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bus_small"/>
<TextView
android:id="#+id/journey_planner_detail_main_text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Instead of creating new Image view just call rowView.findViewById(R.id.image_id); (you'll have to add id to the xml).
The actual bug is with the TextView. It is set to fill_parent causing the newly added ImageView to appear to the right of the visible screen. You can change this by changind the TextView width to wrap_content. However, using the ImageView defined in xml layout is better.
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.
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
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.