ImageButtons In GrdView Different Screen Sizes - android

I am reviewing and fixing some issues at an Android application that was developed from other developer.
He created a screen layout for normal screen size and inserted a GridView in it.
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="45dp"
android:horizontalSpacing="10dp"
android:columnWidth="120dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_marginTop="30dp"
android:scrollbars="none"
/>
At code side images defined as integer array.
Integer[] a1 = { R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4, R.drawable.image5,R.drawable.image6,R.drawable.image6,R.drawable.image7,R.drawable.image8, R.drawable.image9,R.drawable.image10,R.drawable.image11
};
Integer array binded gridview with this code.
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageButtonAdapter(this,infox));
So here ise ImageButtonAdapter class
public class ImageButtonAdapter extends BaseAdapter {
private Context mContext;
public ImageButtonAdapter(Context c, Integer[] imageIds) {
mContext = c;
mThumbIds = imageIds;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageButton imageButtonx;
if (convertView == null) {
imageButtonx = new ImageButton(mContext);
imageButtonx.setBackgroundColor(Color.TRANSPARENT);
**imageButtonx.setLayoutParams(new GridView.LayoutParams(160, 160));**
imageButtonx.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageButtonx.setPadding(5, 5, 5, 5);
/* if ( position == 10 )
{
imageButtonx.setVisibility(ImageButton.GONE);
}
*/
} else {
imageButtonx = (ImageButton) convertView;
}
setMyTag(imageButtonx,mThumbIds[position]);
imageButtonx.setImageResource(mThumbIds[position]);
return imageButtonx;
}
private Integer[] mThumbIds;
}
Also, res\drawable-hdpi folder contains high resolution images that is 167x161 px and res\drawable-mdpi folder contains normal resolution images that is 82x72 px.
But at small screened devices, images streched. I think the bold code line cause this; but i couldn't fix it.
Do you have any suggestion how can i fix this problem and support large-normal and small screens?

LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
This is what i normally do, hope this will help.

yes due to this you are getting that problem
imageButtonx.setLayoutParams(new GridView.LayoutParams(160, 160))
change this code like this
imageButtonx.setLayoutParams(new GridView.LayoutParams(
(int) getResources().getDimension(R.dimen.width),
(int) getResources().getDimension(R.dimen.height)));
store width, height value in the values-->dimentions.xml folder like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="width">80dp</dimen> // your desired values
<dimen name="height">80dp</dimen>
</resources>
HTH..

Related

How to resize the webview According to the size of device Screen (Or Width) in android

I have a Custom ListView with a webView inflated in it. here is XML code
reader_layout.xml
<ListView
android:id="#+id/listReader"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/readerHeader"
android:divider="#android:color/background_dark"
android:dividerHeight="5dp" >
</ListView>
reader_list_webview.xml
<?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" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:scrollbars="none" />
</RelativeLayout>
I am using the AsyncTask and BaseAdapter to fill the listView with WebView, and I have loaded different URL in each webView. here is the java Code.
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
context = c;
}
// ---returns the number of images---
public int getCount() {
return URLs.size();
}
public ImageAdapter(Context ctx, Activity act) {
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// ---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ---returns an ImageView view---
public View getView(final int position, View convertView,
ViewGroup parent) {
// ImageView bmImage;
final ViewHolder holder;
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.book_reader_list_style, parent,
false);
holder = new ViewHolder();
holder.webView = (WebView) vi.findViewById(R.id.webView1);
WebSettings webSettings = holder.webView.getSettings();
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
holder.webView.setVerticalScrollBarEnabled(false);
holder.webView.setHorizontalScrollBarEnabled(false);
holder.webView.setVisibility(View.VISIBLE);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(false);
holder.webView.setInitialScale(scaleWebView);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
holder.webView.loadUrl((URLs.get(position)));
return vi;
}
}
class ViewHolder
{
WebView webView;
}
Everything is working fine. But problem is that some URLs becomes very wider then the webview size hence they doesn't fit the device screen width and some URLs load small pages hence we need to set zoom of webview.
I want to set the Zoom/scale of the webView according to the size of URLs page dynamically. so that user should not use zoom in or Zoom out.
I have tried this
android:layout_width="match_parent"
android:layout_height="match_parent"
and this
myWebview.setLoadWithOverviewMode(true);
myWebview.setUseWideViewPort(true);
and this also
holder.webView.setInitialScale(percent);
but it doesn't resolve my requirements.
Edit
if I m setting setLoadWithOverviewMode(true/false); to true my app crashes and here is the Logcat
01-08 16:16:56.600: E/AndroidRuntime(8666): java.lang.IllegalArgumentException: bitmap size exceeds 32bits
actually my webview loads images of 491X600 upto 300X500 in size. how can i resize the images or something like that?
but when i set setLoadWithOverviewMode(true/false); to false it working fine but problem is that the webview doesn't re size to fit the width of screen.
you can try this..
/**This is for to set wide */
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
Finally I sorted out my solution myself. Here it is
holder.webView.loadDataWithBaseURL(null,"<!DOCTYPE html><html><body style = \"text-align:center\"><img style=\"border-style:dotted;border-width:5px;border-color:black;width:99%;\" src= " + URLs.get(position)+ " alt=\"page Not Found\"></body></html>","text/html", "UTF-8", null)
it will exactly fit the webView's tag fill the screen. no matter your phone is 230X480 or u have any android tablet of 800X1200 .

"ScaleType:FIT_XY" doesnt stretch the images in the Gallery

I have a gallery in landscape mode with a lot of images with an imageadapter. I want that the images stretch to the whole screen Horizontally, and keep the aspect ratio and stretch as much as needed vertically..
I tried to use FIT_XY, it doesnt fit Horizontally, but it doesnt keep the ratio vertically ( the images become crushed)
How can I do this? Here is the custom image adapter:
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
int counter = 0;
private final Context mContext;
public String[] mImageIds;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public void insert(String string) {
mImageIds[counter] = string;
counter++;
}
#Override
public int getCount() {
return mImageIds.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageBitmap(BitmapFactory.decodeFile(mImageIds[position]));
i.setLayoutParams(new Gallery.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT));
i.setScaleType(ImageView.ScaleType.MATRIX);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
when I do this in android I'm using a fit scaling (fitCenter, fitStart, fitEnd) together with adjust view bounds the xml attributes would be e.g.
android:scaleType="fitCenter" android:adjustViewBounds="true"
You can compute the pixels for width and height and send them to setLayoutParams:
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = width * bitmapHeight / bitmapWidth;
imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
imageView.setPadding(6, 0, 6, 0); // set this to what you like
// remove this line:
//imageView.setScaleType(ImageView.ScaleType.MATRIX);
Setting the scale type won’t do anything (except FitXY will stretch horizontally to the bounds, but leave the height as something else, as you mentioned). Also FILL_PARENT does not seem to work as expected. The other layouts seem to affect the bitmap size (or in my case, Drawable).
In your layout xml file, you might have to set fill_parent:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
This worked for me downloading and streaming into a Drawable, so I think it will work for your bitmap.

Removing the extra padding in a GridView in android

I want to remove the extra padding that appears in a grid view. I have images of the size 128*128 which will be occupying cells in the grid. But somehow there is an extra space that gets added to the contents of the grid.
After some research, I was able to determine that I have to override the listSelector property of the grid view. Now here's my question - I know I have to specify something like an xml drawable here, but what to specify in that?? I tried using a shape drawable with padding and stroke set to 0dp to no avail.
The question is asked and answered here, but they haven't given what the drawable must contain.
Can some one help me with this. Thanks!
EDIT: Ok - here's a copy of the UI that I have. And the XML layout for the same is as follows:
<GridView android:id="#+id/GV_A2ZMenu" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="4"
android:layout_gravity="top" android:stretchMode="columnWidth"
android:gravity="center" android:listSelector="#null" />
And I am using a BaseAdapter class to populate the gridView. Here's its code:
public class AtoZMenu extends BaseAdapter {
private static Context AppC;
private Integer[] MenuImg = { R.drawable.alphabet_a, R.drawable.alphabet_b,
R.drawable.alphabet_c, R.drawable.alphabet_d,
R.drawable.alphabet_e, R.drawable.alphabet_f,
R.drawable.alphabet_g, R.drawable.alphabet_h,
R.drawable.alphabet_i, R.drawable.alphabet_j,
R.drawable.alphabet_k, R.drawable.alphabet_l,
R.drawable.alphabet_m, R.drawable.alphabet_n,
R.drawable.alphabet_o, R.drawable.alphabet_p,
R.drawable.alphabet_q, R.drawable.alphabet_r,
R.drawable.alphabet_s, R.drawable.alphabet_t,
R.drawable.alphabet_u, R.drawable.alphabet_v,
R.drawable.alphabet_w, R.drawable.alphabet_x,
R.drawable.alphabet_y, R.drawable.alphabet_z };
public AtoZMenu(Context C) {
AppC = C;
}
public int getCount() {
return MenuImg.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView IV;
float density = AppC.getResources().getDisplayMetrics().density;
if (convertView == null) {
IV = new ImageView(AppC);
IV.setMaxHeight((int) (1));
} else {
IV = (ImageView) convertView;
}
IV.setImageResource(MenuImg[position]);
return IV;
}
}
Can you spot the mistake?
Note: In the end I ended up implementing a similar screen in a table layout which renders much better grids.
Yep, I've had the same problem. You want to set the listSelector to #null:
<!-- Setting the listSelector to null removes the 5px border -->
<GridView
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#null" />
Also, try the following:
myGridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
I see you can do this in the XML, but I didn't when I had this same problem; not sure why.
I also hard-coded the key height:
float density = getContext().getResources().getDisplayMetrics().density;
mKeyHeight = (int) (56 * density);
....
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton b = (ImageButton) convertView;
if (b == null) {
b = new ImageButton(getContext());
b.setMinimumHeight(mKeyHeight);
b.setBackgroundResource(R.drawable.btn_keyboard_key);
b.setOnClickListener(this);
}
}
Sorry about not giving a precise answer, so let me know if you still need help after that.
The correct answer is to set android:listSelector to #android:color/transparent, as user mvds said here.
I used a variation of Shawn's solution.. it looks nice on the Emulator.
1) Decide on the # of columns, I chose 4
2) Set the Column Width
float xdpi = this.getResources().getDisplayMetrics().xdpi;
int mKeyHeight = (int) ( xdpi/4 );
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setColumnWidth( mKeyHeight );// same Height & Width
3) Setup the image in your adapter's getView method
imageView = new ImageView( mContext );
// (xdpi-4) is for internal padding
imageView.setLayoutParams(new GridView.LayoutParams( (int) (xdpi-4)/2, (int) (xdpi-4)/2));
imageView.setScaleType( ImageView.ScaleType.CENTER_CROP );
imageView.setPadding(1, 1, 1, 1);
4) Layout
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="4"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:gravity="center"
android:listSelector="#null"
/>
<!--
android:columnWidth="90dp" Specified in code
android:stretchMode="columnWidth" no noticable change
-->
That's it.
Even I had the same problem.
Try this:
image.setLayoutParams(new GridView.LayoutParams(imageWidth , imageHeight));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);.
Add padding accordingly for the images.It worked for me.
I had a similar problem, though in my case there was a quite large padding area at the bottom of my GridView (it was filled with the background color).
I haven't seen the solution to my issue here, so I'll post it here in case it's helpful.
Besides setting:
android:listSelector="#null"
in my GridView, I also had to set:
android:fadingEdgeLength="0px"
Or, in Java:
GridView.setFadingEdgeLength(0);
Try to give padding in pixels like this
android:paddingLeft="5px"

How to refresh a GridView?

I have a GridView which is pretty similar to the Google tutorial, except that I want to add the ImageViews on runtime (via a subactivity). The results are okay, but the layout of the View is messed up: The GridView doesn't fill the content of its parent, what do I have to do to design it properly?
Here the code of adding the children:
public void initializeWorkbench(GridView gv, Vector<String> items) {
Prototype.workbench.setDimension(screenWidth, divider.height()+workbenchArea.height());
Prototype.workbench.activateWorkbench();
// this measures the workbench correctly
Log.d(Prototype.TAG, "workbench width: "+Prototype.workbench.getMeasuredWidth());
// 320
Log.d(Prototype.TAG, "workbench height: "+Prototype.workbench.getMeasuredHeight());
// 30
ImageAdapter imgAdapter = new ImageAdapter(this.getContext(), items);
gv.setAdapter(imgAdapter);
gv.measure(screenWidth, screenHeight);
gv.requestLayout();
gv.forceLayout();
Log.d(Prototype.TAG, "gv width: "+gv.getMeasuredWidth());
// 22
Log.d(Prototype.TAG, "gv height: "+gv.getMeasuredHeight());
// 119
Prototype.workbench.setDimension(screenWidth, divider.height()+workbenchArea.height());
}
}
activateWorkbench, setDimension and measure in the workbench (LinearLayout above the GridView):
public void activateWorkbench() {
if(this.equals(Prototype.workbench)) {
this.setOrientation(VERTICAL);
show = true;
measure();
}
}
public void setDimension(int w, int h) {
width = w;
height = h;
this.setLayoutParams(new LinearLayout.LayoutParams(width, height));
this.invalidate();
}
private void measure() {
if (this.getOrientation() == LinearLayout.VERTICAL) {
int h = 0;
int w = 0;
this.measureChildren(0, 0);
for (int i = 0; i < this.getChildCount(); i++) {
View v = this.getChildAt(i);
h += v.getMeasuredHeight();
w = (w < v.getMeasuredWidth()) ? v.getMeasuredWidth() : w;
}
if (this.equals(Prototype.tagarea))
height = (h < height) ? height : h;
if (this.equals(Prototype.tagarea))
width = (w < width) ? width : w;
}
this.setMeasuredDimension(width, height);
}
The ImageAdapter constructor:
public ImageAdapter(Context c, Vector<String> items) {
mContext = c;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
if (mExternalStorageAvailable && mExternalStorageWriteable) {
for (String item : items) {
File f = new File(item);
if (f.exists()) {
try {
FileInputStream fis = new FileInputStream(f);
Bitmap b = BitmapFactory.decodeStream(fis);
bitmaps.add(b);
files.add(f);
} catch (FileNotFoundException e) {
Log.e(Prototype.TAG, "", e);
}
}
}
}
}
And the xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom"
android:paddingLeft="0px"
android:paddingTop="0px"
android:paddingRight="0px">
<com.unimelb.pt3.ui.TransparentPanel
android:id="#+id/workbench"
android:layout_width="fill_parent"
android:layout_height="10px"
android:paddingTop="0px"
android:paddingLeft="0px"
android:paddingBottom="0px"
android:paddingRight="0px">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</com.unimelb.pt3.ui.TransparentPanel>
</LinearLayout>
the GridView has an invalidateViews() method.
when you call this method: "all the views to be rebuilt and redrawn."
http://developer.android.com/reference/android/widget/GridView.html
i think this is what you need:)
You must, first tell the adapter to notify that the data has changed and set the adapter again to the grid
adapter.notifyDataChanged();
grid.setAdapter(adapter);
This may be helpful.
I refresh a gridview of book image thumbnails after a delete is executed on an item.
Using adapter.notifyDataChanged(); as mentioned above didn't work for me as it's called in my adapter.
//this is a call that retrieves cached data.
//your constructor can be designed and used without it.
final Object data = getLastNonConfigurationInstance();
I essentially just reload the adapter and bind it to the same view.
//reload the adapter
adapter = new BooksAdapter(MyBooks.this, MyBooks.this, data, show_collection );
grid.invalidateViews();
grid.setAdapter(adapter);
#flyerz #snagnever
Together you guys have got it. It should be:
adapter.notifyDataChanged();
grid.invalidateViews();
This will flag the adapter that its data has changed, which will then be propagated to the grid whenever after the invalidateViews() method is called.
Glad I found this question because I could not figure out how to add items to the grid after its been rendered.
None of these answers actually worked for me and I had to mash all of them together. To actually get the GridView to update, you need to do this:
adapter.notifyDataChanged();
grid.invalidateViews();
grid.setAdapter(adapter);
Hope this helps anyone who couldn't get the other solutions to work.
You should not call invalidateViews and setAdapter to refresh your grid view. This is not a good idea to keep your grid view updated, if you update in that way it would cost a lot of time and memory.
If you have a chance to look at getView method you will see that convertView is created just once. When you call notifyDataChanged, it will update this view. Whereas if you call invalidateViews, previously created views will be recreated. This is not a good solution.
Your getView method is called when you call notifyDataChanged. So your getView method should look something like the code below.
public List list;
public class SimpleItemViewHolder extends Object
{
public TextView textView;
public ImageView imageView;
}
public View getView(int position, View convertView, ViewGroup parent){
View itemView = convertView;
SimpleItemViewHolder viewHolder;
if(convertView==null)
{
viewHolder = (SimpleItemViewHolder)itemView.getTag();
}else{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.layout_name, null);
TextView labelField = (TextView) itemView.findViewById(R.id.label_field);
labelField.setText(list.get(position).Name);
//Typeface boldFont = Typeface.createFromAsset(context.getAssets(), "fonts/Font-Bold.ttf");
//labelField.setTypeface(boldFont);
ImageView imageView = (ImageView) itemView.findViewById(R.id.image_view);
//Bitmap bitmap = init your bitmap here;
//imageView.setImageBitmap(bitmap);
viewHolder = new SimpleItemViewHolder();
viewHolder.imageView = imageView;
viewHolder.textView = labelField;
itemView.setTag(viewHolder);
}
//don't create new views, instead use previous ones and update them.
viewHolder.textView.setText(list.get(position).Name);
//viewHolder.imageView.setImageBitmap(your_bitmap);
return itemView;
}
adapter.notifyDataChanged();
may not works just because data for the adapter is stale (if Activity or fragment was not destroyed and have been sitting in the back stack for instance).
So, if firstly refresh data, then after it will be working.
You are placing the GridView inside com.unimelb.pt3.ui.TransparentPanel which is 10px tall.
You shouldn't use px, you should use dp.
Change com.unimelb.pt3.ui.TransparentPanel's android:layout_height="10px" to android:layout_height="fill_parent"
In your adapter :
class MAdapter extends BaseAdapter{
List<Objects> mObjects;
...
public void clearAdapter(){
mObjects.clear();
}
public void addNewValues(List<Objects> mObjects){
this.mObjects = mObjects;
}
...
}
adapter.clearAdapter(); // clear old values
adapter.addNewValues(MObjects);
adapter.notifyDataChanged();

Gallery layout problems

I'm trying to create a gallery in my Android project, but can't seem to make it work as it should.
Here is what I am trying to do:
Basically, I want a gallery that displays only one picture, but still has the same controls (sliding left and right). I'm getting the pictures asynchronously from the internet.
Where am I at that?
Well, for now I'm just trying to display the gallery... I'll see the "one picture" step after.
What is my problem?
Well... My gallery doesn't show on my layout. At all.
What seems to be the problem?
Log information give me that the height and the widht of my gallery object are... 0.
Speaking about code...
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:background="#drawable/fond">
<ImageView android:background="#drawable/banniere" android:layout_height="wrap_content" android:layout_width="fill_parent"/>
<Gallery
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galleryfullscreen);
Gallery gal = (Gallery) findViewById(R.id.gallery);
ArrayList<Photo> photos = (ArrayList<Photo>) this.getIntent().getExtras().getSerializable("Photos");
int pos = this.getIntent().getExtras().getInt("Index");
Log.i("Season",photos.toString());
gal.setAdapter(new PhotoAdapter(this, photos, false));
gal.setSelection(pos);
Log.d("Bottom", String.valueOf(gal.getBottom()));
Log.d("Right", String.valueOf(gal.getRight()));
Log.d("Width", String.valueOf(gal.getWidth()));
Log.d("Height", String.valueOf(gal.getHeight()));
}
Extras are correctly set.
Adapter
public class PhotoAdapter extends BaseAdapter {
private ArrayList<PhotoView> views;
private ArrayList<Photo> season;
public PhotoAdapter(Context c, ArrayList<Photo> images, boolean mini) {
season = images;
views = new ArrayList<PhotoView>();
for (int i = 0; i < images.size() ; i++)
if (mini)
views.add(new PhotoView(c,images.get(i).getUrlMini(),false));
else{
views.add(new PhotoView(c,images.get(i).getUrlBig(),true));
}
}
#Override
public int getCount() {
return views.size();
}
#Override
public Object getItem(int position) {
return views.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return views.get(position);
}
public ArrayList<Photo> getSeason() {
return season;
}
}
This adaptor is working fine for other objects (such as gridview)
And the PhotoView...
public class PhotoView extends ImageView {
Bitmap image;
public PhotoView(Context context, String url, boolean Gallery) {
super(context);
if (!Gallery){
this.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT,GridView.LayoutParams.WRAP_CONTENT));
this.setScaleType(ImageView.ScaleType.FIT_CENTER);
this.setImageDrawable((getResources().getDrawable(R.drawable.blk)));
new DownloadImageTask().execute(url);
}
else{
this.setLayoutParams(new Gallery.LayoutParams(100,100));
this.setScaleType(ImageView.ScaleType.FIT_CENTER);
this.setBackgroundDrawable(((getResources().getDrawable(R.drawable.blk))));
this.setImageDrawable((getResources().getDrawable(R.drawable.blk)));
}
}
public Bitmap getImage(){
return image;
}
public void setImage(Bitmap img){
image = img;
this.setImageBitmap(image);
}
The same, it's working fine for a grid view. With the download task, that is not set up for the Gallery yet. I'm at least trying to display a ressource before setting this up, so I'm sure the problem doesn't come from here.
Sooo... why are my logs showing "0" for gallery width and height? LayoutParams are set for the object (I tried both XML and code), and for the views inside. The items are created, and the "getView" method is called. I tried seeting up LayoutParams here, doesn't change anything.
I also tried adding the xmlns:android="http://schemas.android.com/apk/res/android" part in the layout for the gallery, doesn't change a thing.
I really don't know what I'm missing. I tried to adapt many tutorials on gallery objects, but still... don't know!
If you have any idea of what I should do/try, please...
Thanks all!
Cheers
Looking at your XML file I can see what could be problem. Your LinearLayout is set to add views in an horizontal matter (that is default) and your first view has its layout_width set to fill_parent.
Set orientation to vertical in your LinearLayout.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/fond">

Categories

Resources