GTU Android Program 11 Android Image Gallery Example

      Comments Off on GTU Android Program 11 Android Image Gallery Example

GTU Android Program 11 Understanding of UI : a.  Create an UI such that , one screen have list of all the types of cars b. On selecting of any car name, next screen should show Car details like : name , launched date , company name, images(using gallery) if available, show different  colors in which it is available.

GTU Android Program 11

Want to display number of images in single activity like Image Gallery ? Here is the complete program which will display image gallery as per the choice of user. Here there are two activity, Activity 1 will ask user for option like selection of interested in cars, in the second activity car details will be shown along with image gallery of the car. Make sure to add images of  car before you run it and be careful here is CustomAdapter which will bit difficult to understand at first sight, try to read 2-3 times to understand the concept of the CustomAdapter.

Step  1 : Create New Project.

Step 2 : Add images into res/drawable folder (a1.png, a2.png, b1.png, b2.png, c1.png, c2.png)

Step 3 : Add another activity

Lets check out complete code.

Question related with Android Image Gallery

  • Explain with code snippet Gallery view? How it is differ from Image View

 

Location : res\layout

FileName: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="gtu.android.prog11.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:text="Select Your Favourit Car"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:entries="@array/car_names"
        android:layout_marginTop="21dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="34dp"
        android:text="Developed By:Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="83dp"
        android:text="Go !" />

</RelativeLayout>

 

Location : res\layout

FileName: activity_main2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="gtu.android.prog11.MainActivity2" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="29dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="23dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp" />

</RelativeLayout>

 

Location : src\package\

FileName : MainActivity.java

package gtu.android.prog11;

/*
 * GTU Android Program 11 Understanding of UI : a.  Create an UI such that , one screen have list of all the types of cars b. On selecting of any car name, next screen should show Car details like : name ,
 * launched date , company name, images(using gallery) if available, show different 
 * colors in which it is available.  
 * 
 */


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

public class MainActivity extends Activity {

	Spinner sp1;
	int cartype;
	Button b1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		sp1 = (Spinner) findViewById(R.id.spinner1);
		b1 = (Button) findViewById(R.id.button1);
		
		b1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			
				//=======================================================
				// Calling another activity to display car information.
				// passing car index along with it.
				//=======================================================
				Intent i = new Intent(getApplicationContext(),MainActivity2.class);
				i.putExtra("cartype", cartype + "");
				startActivity(i);
	
			}
		});
		
		//=====================================================
		// When use select car, put it into some variable so later on we can pass this 
		//====================================================
		sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				
				//Storing car index into variable.
				cartype = arg2;
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
				
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		
		return super.onOptionsItemSelected(item);
	}
}

 

Location : src\package\

FileName : MainActivity2.java

package gtu.android.prog11;

/*
 * GTU Android Program 11 Understanding of UI : a.  Create an UI such that , one screen have list of all the types of cars b. On selecting of any car name, next screen should show Car details like : name ,
 * launched date , company name, images(using gallery) if available, show different 
 * colors in which it is available.  
 * 
 */


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity2 extends Activity {

	TextView tvCarName,tvcarDetails;
	Gallery gallery;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_activity2);
	
		tvCarName = (TextView) findViewById(R.id.textView1);
		tvcarDetails = (TextView) findViewById(R.id.textView2);
		
		//=====================================================================
		//Read Car index value from the intent, sent using previus activity intent
		//=====================================================================		
		int carType = Integer.parseInt( getIntent().getStringExtra("cartype"));

		
		//=====================================================================
		// Display car information into TextView as per parameter
		//=====================================================================
		if(carType == 0)
		{
			tvCarName.setText("You Love Maruti 800");
			tvcarDetails.setText("Car was Launched on 2000 \nCompany : Maruti");
			
		}
		else if(carType == 1)
		{
			tvCarName.setText("Its Duster !");
			tvcarDetails.setText("Car was Launched on 1980 \nCompany : Abc");
		}
		else if(carType == 2)
		{
			tvCarName.setText("Its Scorpio");
			tvcarDetails.setText("Car was Launched on 2005 \nCompany : PQR");
		}
		
		
		//=====================================================================
		// Load image gallery as per selection
		//=====================================================================
		
		gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new CustomAdapterForGallery(this,carType+""));
        
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_activity2, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		
		
		
		return super.onOptionsItemSelected(item);
	}
	
	
	//==================================================
	// Customer Adapter to add Images into Gallery
	//==================================================
	class CustomAdapterForGallery extends BaseAdapter
	{
		int GalItemBg;
		private Context currentContext;
		String CarIndexInString;
		
		//========================================================
		// Resource Image Array
		//========================================================
		private Integer[] imagearray1 = {R.drawable.a1, R.drawable.a2};
		private Integer[] imagearray2 = {R.drawable.b1, R.drawable.b2};
		private Integer[] imagearray3 = {R.drawable.c1, R.drawable.c2};

		
		public CustomAdapterForGallery(Context context,String indx) 
		{
			currentContext = context;	
			CarIndexInString=indx;
		}

		public int getCount()
		{
			if(CarIndexInString.equals("0"))
			{
				return imagearray1.length;
			}
			else if(CarIndexInString.equals("1"))
			{
				return imagearray2.length;
			}
			else if(CarIndexInString.equals("2"))
			{
				return imagearray3.length;
			}
			else
			{
				return imagearray1.length;
			}
		}

		
		public Object getItem(int position) {
		return position;
		}

		
		public long getItemId(int position) {
		return position;
		}


		public View getView(int position, View convertView, ViewGroup parent)
		{
			ImageView imgView = new ImageView(currentContext);
			
			if(CarIndexInString.equals("0"))
			{
				imgView.setImageResource(imagearray1[position]);	
			}
			else if(CarIndexInString.equals("1"))
			{
				imgView.setImageResource(imagearray2[position]);
			}
			else if(CarIndexInString.equals("2"))
			{
				imgView.setImageResource(imagearray3[position]);
			}
			else
			{
				imgView.setImageResource(imagearray1[position]);
			}	
			
			
			imgView.setLayoutParams(new Gallery.LayoutParams(200, 200));
			imgView.setScaleType(ImageView.ScaleType.FIT_XY);
			
			return imgView;
		}
	}

}

 

Output

GTU Android Program 11

output

GTU Android Program 11

output

GTU Android Program 11

GTU Android Program 11

GTU Android Program 11

GTU Android Program 11

GTU Android Program 11

Download Complete Code with All Images and Code

[sociallocker]Download[/sociallocker]

Download Few Popular Android Apps

  1. Bank IFSC Code : Now easily get bank ifsc code which you need while online transfer.
  2. All India PIN Code : Get all india PIN Code Numbers on 1 Android App.
  3. RTO Code : Get all India RTO Code on 1 Android App.
  4. Google 2 Step Code Speak : Now app will speak your google 2 step code, so no need to check mobile, code will reach you.
  5. All Gujarat ST Bus Stand Helpline Numbers (GSRTC Helpline Numbers)
  6. Mumbai City Bus Numbers (MSRTC Helpline Numbers)

  7. Andhra Pradesh City Bus Helpline Numbers (APSRTC Helpline Numbers)

  8. Bhavnagar Diary (Bhavnagar Business Directory)

  9. Baroda Diary (Baroda Business Directory)
  10. Surat Diary (Surat Business Directory)
  11. Ahmedabad Diary (Ahmedabad Business Directory)
  12. Rajkot Diary (Rajkot Business Directory)