Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Gallery with first thumbnail as coverpage
    primarykey
    data
    text
    <p>Thanks for reading! </p> <p><strong>Some background:</strong></p> <p>I am building a Gallery app from the tutorial <a href="http://developer.android.com/resources/tutorials/views/hello-gallery.html" rel="nofollow">here</a> </p> <p>Only change I made to this code is to replace </p> <p><code> i.setLayoutParams(new Gallery.LayoutParams(150, 100)); </code> </p> <p>with </p> <p><code> i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); </code></p> <p>to display only one gallery image at a time (Kinda like a slideshow viewer). </p> <p><strong>Problem:</strong></p> <p>I want the first thumbnail of the gallery to act as an Album Cover with two <code>TextView</code>'s to display Album info.</p> <p><strong>Experiment:</strong> So, I created an <code>cover.xml</code> like this:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="6dip"&gt; &lt;ImageView android:id="@+cover/imgImage" android:adjustViewBounds="true" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;/ImageView&gt; &lt;TextView android:id="@+cover/tvCoverText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLines="2" android:text="Text1" /&gt; &lt;TextView android:id="@+cover/tvCoverText2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" android:maxLines="1" android:layout_below="@cover/tvCoverText1" android:text="Text2" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>And here's the Java code. I check in <code>getView()</code> if the position is 0 (the first thumbnail) and then play around with the views.</p> <pre><code> package com.sagar.sample; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class Main extends Activity { private LayoutInflater mInflater = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery g = (Gallery) findViewById(R.main.gallery); g.setAdapter(new ImageAdapter(this)); g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show(); } }); } public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; private Integer[] mImageIds = { 0, R.drawable.bp1, R.drawable.bp2, R.drawable.bp3 }; public ImageAdapter(Context c) { mContext = c; mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); // TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery); // mGalleryItemBackground = a.getResourceId( // R.styleable.HelloGallery_android_galleryItemBackground, 0); // a.recycle(); } public int getCount() { return mImageIds.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; View view = convertView; if(view == null) { view = mInflater.inflate(R.layout.cover, null); viewHolder = new ViewHolder(); viewHolder.tvCoverText1 = (TextView)view.findViewById(R.cover.tvCoverText1); viewHolder.tvCoverText2 = (TextView)view.findViewById(R.cover.tvCoverText2); viewHolder.imgView = (ImageView)view.findViewById(R.cover.imgImage); view.setTag(viewHolder); } else { viewHolder = (ViewHolder)view.getTag(); } if(position == 0) { viewHolder.tvCoverText1.setVisibility(View.VISIBLE); viewHolder.tvCoverText2.setVisibility(View.VISIBLE); viewHolder.imgView.setVisibility(View.GONE); } else { viewHolder.tvCoverText1.setVisibility(View.GONE); viewHolder.tvCoverText2.setVisibility(View.GONE); viewHolder.imgView.setVisibility(View.VISIBLE); //viewHolder.imgView = new ImageView(mContext); viewHolder.imgView.setImageResource(mImageIds[position]); //Album cover is at 0th position viewHolder.imgView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); viewHolder.imgView.setScaleType(ImageView.ScaleType.FIT_XY); viewHolder.imgView.setBackgroundResource(mGalleryItemBackground); } return view; } } static class ViewHolder { TextView tvCoverText1, tvCoverText2; ImageView imgView; } } </code></pre> <p><strong>End Result:</strong> When the app loads up, I first see a blank screen for a while and then the view changes to display the AlbumCover. And it's painfully slow to scroll across the images. </p> <p>Hmm..obviously, I am doing something wrong. I sincerely hope someone could help me here :(</p> <p>Thanks!</p> <p>UPDATE: Adding <code>main.xml</code></p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;Gallery android:id="@+main/gallery" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>UPDATE2: So, here's some psuedo code to explain what I am trying to achieve:</p> <pre><code> if(position == 0) //show toptext and bottomtext from cover.xml else //show tvTitle1 and tvTitle2 (may later include tvTitle3 and tvTitle4) from main.xml </code></pre> <p>Right now, only the position 0 case works and that too when I swipe to position 1 and swipe back to position 0 - the <code>TextView</code>s are grayed out and barely visible. :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload