Friday, September 30, 2011

Grid View Demo With Images

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.

We are going to create a Grid View to show Images, where we can select each item via click.

so lets create simple app with default activity "GridViewActivity" and put this code inside it.
  
package com.rdc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GridViewActivity extends Activity {
    
 GridView gridView;
 
 static final String[] MOBILE_OS = new String[]{
  "Android", "iOS", "Windows", "Balckberry",
 };
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        gridView = (GridView) findViewById(R.id.gridView1);
        
        gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));
        
        gridView.setOnItemClickListener(new OnItemClickListener(){
         public void onItemClick(AdapterView parent, View v, 
           int position, long id){
          Toast.makeText(getApplicationContext(),
          ((TextView) v.findViewById(R.id.grid_item_label))
          .getText(), Toast.LENGTH_SHORT).show();
         }
        });
    }
}

ImageAdapter.java
  
package com.rdc;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter{
 
 
 private Context context;
 private final String[] mobileValues;
 
 public ImageAdapter(Context context, String[] 
   mobileValues){
  this.context=context;
  this.mobileValues=mobileValues;
 }
 
 public View getView(int position, View convertView, 
   ViewGroup parent){
  LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.
      LAYOUT_INFLATER_SERVICE);
  
  View gridView;
  if(convertView == null){
   gridView = new View(context);
   gridView = inflater.inflate(
     R.layout.mobile, null);
   
   TextView textView = (TextView) gridView.
     findViewById(R.id.grid_item_label);
   textView.setText(mobileValues[position]);
   
   ImageView imageView = (ImageView) gridView.
     findViewById(
       R.id.grid_item_image);
   String mobile = mobileValues[position];
  
   if(mobile.equals("Android")){
    imageView.setImageResource(
      R.drawable.android_logo);
   }
   else if(mobile.equals("iOS")){
    imageView.setImageResource(
      R.drawable.ios_logo);
   }
   else if(mobile.equals("Windows")){
    imageView.setImageResource(
      R.drawable.windows_logo);    
   }   
   else if(mobile.equals("BlackBerry")){
    imageView.setImageResource(
      R.drawable.ios_logo);
   }   
  }
  else{
   gridView = (View) convertView;
  }
  return gridView;
 }

 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return mobileValues.length;
 }

 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return null;
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return 0;
 }
}

main.xml
  


 

mobile.xml
  


  
  
  
  
  
     
    


you need to put these icons in res/drawable-mdpi folder or any:

Then our output is:

Sunday, September 18, 2011

How to add images in Android Emulator

how to add images to gallery in android emulator??

want to put some pics in emulator to start playing with animation stuff or media??

very easy !!

If you are developing on Eclipse IDE..Look at top-right corner "DDMS" tab. yes press it and go to this directory path.

/mnt/sdcard/DCIM/100ANDRO folder
now push image file using  "push a file on to the device" icon
like this..

okay now come to Android Emulator and click on "Dev Tools" --> "Media Scanner" to  synchronize

yes!! done.. now you should go to library you will get like this.

Life is so simple if we can share our knowledge to other isn't it.... :D

 I'd love to here your thoughts!!

Thursday, September 15, 2011

GridView Demo with Text

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.

We are going to create a Grid View to show Text, where we can select each item via click.

so lets create simple app with default activity "GridViewActivity" and put this code inside it.
  
package com.rdc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GridViewActivity extends Activity {
 
 GridView gridView;

 static final String[] numbers = new String[]{
  "A", "B", "C", "D",
  "E", "F", "G", "H",
 };
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gridView = (GridView) findViewById(R.id.gridView1);
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
        (this,android.R.layout.simple_list_item_1, numbers);
        
        gridView.setAdapter(adapter);
        
        gridView.setOnItemClickListener(new OnItemClickListener()
        {
         public void onItemClick(AdapterView parent, 
           View v,int position, long id){
          
          Toast.makeText(getApplicationContext(),
           "Selected: " +((TextView) v).getText(), 
           Toast.LENGTH_SHORT).show();
         }
        });        
   }
}

and the main.xml file is
  


 


out put is like this

Tuesday, September 13, 2011

Create Transparent Activity

Today we are goning to learn "How to create Transparent Activity ?"
we need to create two activities and a transparent style theme to acheive this.

let's start with creating simple app with default activity "first" and change
main.xml to first.xml in res/layout folder.

  
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FirstActivity extends Activity 
                implements OnClickListener {
   private Button btnLoad;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        
        
        btnLoad=(Button) findViewById(R.id.btnload);
        btnLoad.setOnClickListener(this);
    }
 @Override
 public void onClick(View v) {
  Intent i = new Intent(this,SecondActivity.class);
        startActivity(i);
  
 }
}

first.xml
  


    

    




then same way create second activity with second.xml file like this
  
package com.rdc;

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);        
    }
}

and the second.xml is
  


  
    
    


now create "styles.xml" in res/values folder
  


  


and also create and color.xml file in res/values
  

  #75AADB
  #00000000


at last make entries for activities and style theme in manifest file
  


    

    
        
            
                
                
            
        
        
        
     
        

    


now if you done all the stuff in right way you will get output like this: (after tapping the button to load transparent activity)

cheers!!

I'd love to hear your thoughts!