A android.widget.Gallery - Example

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby soniya » Wed Jan 16, 2008 9:51 am

Thank you it's working.....
but that code show Nullpointer error
it's print mes "after image adpater" then show error "Nullpointer"

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class demo2 extends View{
  2.  
  3.          private Paint   mPaint = new Paint();
  4.  
  5.             private Context ctx;
  6.  
  7.            
  8.  
  9.             private Canvas mCanvas = new Canvas();
  10.  
  11.             private int[] myImageIds = {
  12.  
  13.                        R.drawable.image_1,
  14.  
  15.                        R.drawable.image_2,
  16.  
  17.                        R.drawable.image_3,
  18.  
  19.                        R.drawable.image,
  20.  
  21.                        R.drawable.ena,
  22.  
  23.                        R.drawable.image_4
  24.  
  25.                      
  26.  
  27.                 };
  28.  
  29.            
  30.  
  31.             public demo2(Context context) {
  32.  
  33.                         // TODO Auto-generated constructor stub
  34.  
  35.                
  36.  
  37.                         super(context);
  38.  
  39.                          ctx = context;
  40.  
  41.                          
  42.  
  43.               this.setBackground(getResources().getDrawable(R.drawable.b));
  44.  
  45.                 setFocusable(true);
  46.  
  47.                
  48.  
  49.               //  ((Gallery) findViewById(R.id.gallery)).setAdapter(new ImageAdapter(this.ctx));
  50.  
  51.                
  52.  
  53.                  
  54.  
  55.                 }
  56.  
  57.             @Override
  58.  
  59.              protected void onDraw(Canvas canvas) {
  60.  
  61.                           super.onDraw(canvas);
  62.  
  63.                           mCanvas = canvas;
  64.  
  65.                           int screen_hei=mCanvas.getBitmapHeight();
  66.  
  67.                           int screen_wid=mCanvas.getBitmapWidth();
  68.  
  69.                           mPaint.setARGB(255,255,255,255);
  70.  
  71.                       mCanvas.drawRoundRect(new RectF(10,10,(screen_wid-10),screen_hei/5), 5, 5, mPaint);
  72.  
  73.                           mCanvas.drawRoundRect(new RectF(10,screen_hei/5+10,(screen_wid-10),screen_hei-30), 5, 5, mPaint);
  74.  
  75.                           Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.xs2);
  76.  
  77.                           int image_hei=image.height();
  78.  
  79.                           int image_wei=image.width();
  80.  
  81.                           canvas.drawBitmap(image,(screen_wid-image_wei)/2,(screen_hei/5-image_hei)/2, mPaint);
  82.  
  83.                           Gallery gallery = (Gallery) findViewById(R.id.gallery);
  84.  
  85.                         Log.i("canvas","Gallery Gallery Gallery Gallery ");
  86.  
  87.                          ImageAdapter IA=new ImageAdapter(this.ctx);
  88.  
  89.                          Log.i("Canvas","after image adpater ");
  90.  
  91.                           gallery.setAdapter(IA);
  92.  
  93.                           Log.i("canvas","ImageAdapterImageAdapterImageAdapter ");
  94.  
  95.                          
  96.  
  97.                          }
  98.  
  99.            
  100.  
  101.             public class ImageAdapter extends BaseAdapter{
  102.  
  103.                  private Context mContext;
  104.  
  105.                  
  106.  
  107.                  
  108.  
  109.                         public ImageAdapter(Context context1) {
  110.  
  111.                                 // TODO Auto-generated constructor stub
  112.  
  113.                                 mContext=context1;
  114.  
  115.                                                
  116.  
  117.                         }
  118.  
  119.  
  120.  
  121.                         public int getCount() {
  122.  
  123.                                 // TODO Auto-generated method stub
  124.  
  125.                                 return myImageIds.length;
  126.  
  127.                                
  128.  
  129.                         }
  130.  
  131.  
  132.  
  133.                         public Object getItem(int position) {
  134.  
  135.                                 // TODO Auto-generated method stub
  136.  
  137.                                 return position;
  138.  
  139.                         }
  140.  
  141.  
  142.  
  143.                         public long getItemId(int position) {
  144.  
  145.                                 // TODO Auto-generated method stub
  146.  
  147.                                 return position;
  148.  
  149.                         }
  150.  
  151.                          public float getScale(boolean focused, int offset) {
  152.  
  153.                                  
  154.  
  155.                      return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
  156.  
  157.                }
  158.  
  159.                
  160.  
  161.                public float getAlpha(boolean focused, int offset) {
  162.  
  163.                    return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
  164.  
  165.                  }
  166.  
  167.                         public View getView(int position, View convertView, ViewGroup parent) {
  168.  
  169.                                 // TODO Auto-generated method stub
  170.  
  171.                                 ImageView i = new ImageView(this.mContext);
  172.  
  173.                                    i.setTag(i.getResources().getText(myImageIds[position]));
  174.  
  175.                                    i.setImageResource(myImageIds[position]);
  176.  
  177.                                     i.setScaleType(ImageView.ScaleType.FIT_XY);
  178.  
  179.                                     i.setLayoutParams(new Gallery.LayoutParams(150, 150));
  180.  
  181.                                    return i;
  182.  
  183.                         }
  184.  
  185.                
  186.  
  187.             }
  188.  
  189. }
  190.  
  191.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Top

Postby plusminus » Wed Jan 16, 2008 6:27 pm

Hello soniya,

Note that either Activity AND View provide an findViewByID-method:

From Activity(looks in XML used):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. View android.app.Activity.findViewById(int id)
  2.  
  3.  
  4.  
  5. public View findViewById(int id)
  6.  
  7. Finds a view that was identified by the id attribute from the XML THAT WAS PROCESSED in onCreate(Bundle).
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


View (looks only in its children):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. View android.view.View.findViewById(int id)
  2.  
  3.  
  4.  
  5. public final View findViewById(int id)
  6.  
  7. Look FOR A CHILD VIEW with the given id. If this view has the given id, return this view.
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


So for you this should work:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. //added  ctx  before findViewById(...)
  3.  
  4. Gallery gallery = (Gallery) ctx.findViewById(R.id.gallery);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Let us know if it helped you. :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby soniya » Wed Jan 16, 2008 7:41 pm

thank you for reply but line

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Gallery gallery = (Gallery) ctx.findViewById(R.id.gallery);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


show error........when click on cross(error) then show "findViewById is undefined for the type context".. :cry:
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Postby soniya » Wed Jan 16, 2008 7:48 pm

how to solved that problem......... :cry:
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Postby plusminus » Wed Jan 16, 2008 8:04 pm

Hello soniya,

sry forgot that.
findViewByID(...) comes from Activity, which extends Context.

So add a cast here:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Gallery gallery = (Gallery) ctx.findViewById(R.id.gallery);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

change to:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Gallery gallery = (Gallery) ((Activity)ctx).findViewById(R.id.gallery);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby soniya » Thu Jan 17, 2008 6:50 am

plusminus wrote:Hello soniya,

sry forgot that.
findViewByID(...) comes from Activity, which extends Context.

So add a cast here:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Gallery gallery = (Gallery) ctx.findViewById(R.id.gallery);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

change to:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Gallery gallery = (Gallery) ((Activity)ctx).findViewById(R.id.gallery);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus



i try that but it's again show same error "NullPointer" on line "gallery.setAdapter(IA);"

im also try in onCreate() but it's show same error.....
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Top

Postby soniya » Thu Jan 17, 2008 6:10 pm

in code i have 2 class
IN first class

demo.java


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class canvas extends Activity {
  2.  
  3.                
  4.  
  5.             public void onCreate(Bundle icicle)
  6.  
  7.             {
  8.  
  9.                     super.onCreate(icicle);
  10.  
  11.                     demo2 mView = new demo2(this);
  12.  
  13.                     requestWindowFeature(Window.FEATURE_NO_TITLE);
  14.  
  15.                     setContentView(mView);
  16.  
  17.             }
  18.  
  19.  
  20.  
  21.         }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



and second class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class demo2 extends View{
  2.  
  3.       private Paint   mPaint = new Paint();
  4.  
  5.          private Context ctx;
  6.  
  7.        
  8.  
  9.          private Canvas mCanvas = new Canvas();
  10.  
  11.          private int[] myImageIds = {
  12.  
  13.                     R.drawable.image_1,
  14.  
  15.                     R.drawable.image_2,
  16.  
  17.                     R.drawable.image_3,
  18.  
  19.                     R.drawable.image,
  20.  
  21.                     R.drawable.ena,
  22.  
  23.                     R.drawable.image_4
  24.  
  25.                    
  26.  
  27.              };
  28.  
  29.          
  30.  
  31.          public demo2(Context context) {
  32.  
  33.                // TODO Auto-generated constructor stub
  34.  
  35.            
  36.  
  37.                super(context);
  38.  
  39.                 ctx = context;
  40.  
  41.                
  42.  
  43.            this.setBackground(getResources().getDrawable(R.drawable.b));
  44.  
  45.              setFocusable(true);
  46.  
  47.              
  48.  
  49.            //  ((Gallery) findViewById(R.id.gallery)).setAdapter(new ImageAdapter(this.ctx));
  50.  
  51.              
  52.  
  53.                
  54.  
  55.           }
  56.  
  57.          @Override
  58.  
  59.           protected void onDraw(Canvas canvas) {
  60.  
  61.                  super.onDraw(canvas);
  62.  
  63.                  mCanvas = canvas;
  64.  
  65.                  int screen_hei=mCanvas.getBitmapHeight();
  66.  
  67.                  int screen_wid=mCanvas.getBitmapWidth();
  68.  
  69.                  mPaint.setARGB(255,255,255,255);
  70.  
  71.                 mCanvas.drawRoundRect(new RectF(10,10,(screen_wid-10),screen_hei/5), 5, 5, mPaint);
  72.  
  73.                  mCanvas.drawRoundRect(new RectF(10,screen_hei/5+10,(screen_wid-10),screen_hei-30), 5, 5, mPaint);
  74.  
  75.                  Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.xs2);
  76.  
  77.                  int image_hei=image.height();
  78.  
  79.                  int image_wei=image.width();
  80.  
  81.                  canvas.drawBitmap(image,(screen_wid-image_wei)/2,(screen_hei/5-image_hei)/2, mPaint);
  82.  
  83.                  Gallery gallery = (Gallery) ((Activity)ctx).findViewById(R.id.gallery);
  84.  
  85.                   Log.i("canvas","Gallery Gallery Gallery Gallery ");
  86.  
  87.                    ImageAdapter IA=new ImageAdapter(this.ctx);
  88.  
  89.                    Log.i("Canvas","after image adpater ");
  90.  
  91.                     gallery.setAdapter(IA);
  92.  
  93.                     Log.i("canvas","ImageAdapterImageAdapterImageAdapter ");
  94.  
  95.                  
  96.  
  97.                 }
  98.  
  99.          
  100.  
  101.          public class ImageAdapter extends BaseAdapter{
  102.  
  103.            private Context mContext;
  104.  
  105.            
  106.  
  107.            
  108.  
  109.                public ImageAdapter(Context context1) {
  110.  
  111.                     // TODO Auto-generated constructor stub
  112.  
  113.                     mContext=context1;
  114.  
  115.                                
  116.  
  117.                }
  118.  
  119.  
  120.  
  121.                public int getCount() {
  122.  
  123.                     // TODO Auto-generated method stub
  124.  
  125.                     return myImageIds.length;
  126.  
  127.                      
  128.  
  129.                }
  130.  
  131.  
  132.  
  133.                public Object getItem(int position) {
  134.  
  135.                     // TODO Auto-generated method stub
  136.  
  137.                     return position;
  138.  
  139.                }
  140.  
  141.  
  142.  
  143.                public long getItemId(int position) {
  144.  
  145.                     // TODO Auto-generated method stub
  146.  
  147.                     return position;
  148.  
  149.                }
  150.  
  151.                 public float getScale(boolean focused, int offset) {
  152.  
  153.                      
  154.  
  155.                   return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
  156.  
  157.             }
  158.  
  159.            
  160.  
  161.             public float getAlpha(boolean focused, int offset) {
  162.  
  163.                 return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
  164.  
  165.               }
  166.  
  167.                public View getView(int position, View convertView, ViewGroup parent) {
  168.  
  169.                     // TODO Auto-generated method stub
  170.  
  171.                     ImageView i = new ImageView(this.mContext);
  172.  
  173.                           i.setTag(i.getResources().getText(myImageIds[position]));
  174.  
  175.                           i.setImageResource(myImageIds[position]);
  176.  
  177.                            i.setScaleType(ImageView.ScaleType.FIT_XY);
  178.  
  179.                            i.setLayoutParams(new Gallery.LayoutParams(150, 150));
  180.  
  181.                           return i;
  182.  
  183.                }
  184.  
  185.            
  186.  
  187.          }
  188.  
  189. }
Parsed in 0.052 seconds, using GeSHi 1.0.8.4



XML file

main.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  <?xml version="1.0" encoding="utf-8" ?>
  2.  
  3. - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  4.  
  5.   <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, demo" />
  6.  
  7.   <Gallery id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="bottom" />
  8.  
  9.   </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


i could not use xml file may be that's why it's show nullpointer error...........if i will use in line "setContentView(R.layout.main);" then how to use view class??we can use view or imageadapter class only once??
that is do different way then plz tel me :(
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Postby plusminus » Thu Jan 17, 2008 8:37 pm

Hello soniya,

strange.. :(
Getting the Gallery within the OnCreate(...) of your Activity has to work.
:idea: So save it there and pass it to the View, which then doesn't have to get it on its own.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby soniya » Fri Jan 18, 2008 3:13 am

how to do that???im not getting what u asked......... :cry:
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Postby plusminus » Fri Jan 18, 2008 9:25 am

Hello soniya,
soniya wrote:how to do that???im not getting what u asked......... :cry:


As you experienced not to be able to do the findViewByID in the View, you could save a reference to the Gallery-Object in the onCreate()-method. Then pass that Gallery-References to the View in its constructor.

Again, you can only get Views by findViewByID, from the xml-layout you passed to setContentView() in your onCreate(), what you do not yet do!

Javadoc of findViewById:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. View android.app.Activity.findViewById(int id)
  2.  
  3. public View findViewById(int id)
  4. Finds a view that was identified by the id attribute from the XML THAT WAS PROCESSED in onCreate(Bundle).
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


so you setup your main.xml and place a View inside there, similar to this:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <XYZLayout xmls:"...." >
  2.                <org.anddev.android.weatherforecast.views.SingleWeatherInfoView
  3.                    id="@+id/weather_1"
  4.                    android:layout_width="wrap_content"
  5.                    android:layout_height="wrap_content"
  6.                    />
  7. </XYZLayout>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Hope i am clearer now.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby soniya » Fri Jan 18, 2008 11:54 am

thank you for reply...........
im change in xml file

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13.  
  14.  
  15.     <org.android.demo.views.demo2 id="@+id/gallery"
  16.  
  17.        android:layout_width="fill_parent"
  18.  
  19.        android:layout_height="wrap_content"
  20.  
  21.        android:gravity="bottom"
  22.  
  23. />
  24.  
  25. </LinearLayout>
  26.  
  27.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


but that still show nullpointer error in same place....... how to solved :?: :cry:
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Postby plusminus » Fri Jan 18, 2008 12:02 pm

Hello soniya,

looks good but do you load THAT xml-file in your OnCreate(...) vie setContentView :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby soniya » Fri Jan 18, 2008 2:04 pm

No, i do not use xml file in onCreate()
i used that
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate(Bundle icicle)
  2.  
  3.          {
  4.  
  5.                  super.onCreate(icicle);
  6.  
  7.                  demo2 mView = new demo2(this);
  8.  
  9.                  requestWindowFeature(Window.FEATURE_NO_TITLE);
  10.  
  11.                  setContentView(mView);
  12.  
  13.          }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


if i used xml file in setContentView() then how to use view class???
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Postby plusminus » Fri Jan 18, 2008 7:14 pm

Hello soniya,

just use like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.YOURmain);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby soniya » Mon Jan 21, 2008 10:46 am

how we can,name of image is display on screen and change name when we scroll image???
soniya
Senior Developer
Senior Developer
 
Posts: 111
Joined: Mon Jan 14, 2008 1:52 pm
Location: india

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests