Thursday, 27 October 2011

Grid view of images with names

package com.grid;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainSwitch extends Activity implements OnItemClickListener{
 Context context;
 MainSwitch mainSwitch;

 String[] items = { "music", "cricket", "astrology", "jobs", "beautytips", "movies"};

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  GridView gridview = (GridView) findViewById(R.id.gridview);
 
 
  gridview.setAdapter(new ImageAdapter(this));
 
  gridview.setOnItemClickListener(new OnItemClickListener()
  {
   @Override
   public void onItemClick(AdapterView<?> parent, View arg1, int Position,
     long id) {
    if(Position==0)
    {
     AlertDialog.Builder builder = new AlertDialog.Builder(MainSwitch.this);
     builder.setMessage("Are you sure you want to subscribe music alerts?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                 Intent mi = new Intent(MainSwitch.this ,MusicActivity.class);
                    startActivity(mi);
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
     AlertDialog alert = builder.create();
     alert.show();
    
    }
    if(Position==1)
    {
     AlertDialog.Builder builder = new AlertDialog.Builder(MainSwitch.this);
     builder.setMessage("Are you sure you want to subscribe Cricket alerts?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                 Intent mi = new Intent(MainSwitch.this ,MusicActivity.class);
                    startActivity(mi);
                
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
     AlertDialog alert = builder.create();
     alert.show();
     //Toast.makeText(getApplicationContext(), "Inside Cricket", 25).show();   
    }
   
    if(Position==2)
    {
     AlertDialog.Builder builder = new AlertDialog.Builder(MainSwitch.this);
     builder.setMessage("Are you sure you want to subscribe Astrology alerts?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                 Intent mi = new Intent(MainSwitch.this ,MusicActivity.class);
                    startActivity(mi);
                
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
     AlertDialog alert = builder.create();
     alert.show();
     //Toast.makeText(getApplicationContext(), "Inside asrology", 25).show();   
    }
    if(Position==3)
    {
     AlertDialog.Builder builder = new AlertDialog.Builder(MainSwitch.this);
     builder.setMessage("Are you sure you want to subscribe Jobs alerts?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                 Intent mi = new Intent(MainSwitch.this ,MusicActivity.class);
                    startActivity(mi);
                
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
     AlertDialog alert = builder.create();
     alert.show();
     //Toast.makeText(getApplicationContext(), "Inside asrology", 25).show();   
    }
    if(Position==4)
    {
     AlertDialog.Builder builder = new AlertDialog.Builder(MainSwitch.this);
     builder.setMessage("Are you sure you want to subscribe BeautyTips alerts?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                 Intent mi = new Intent(MainSwitch.this ,MusicActivity.class);
                    startActivity(mi);
                
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
     AlertDialog alert = builder.create();
     alert.show();
     //Toast.makeText(getApplicationContext(), "Inside asrology", 25).show();   
    }
    if(Position==5)
    {
     AlertDialog.Builder builder = new AlertDialog.Builder(MainSwitch.this);
     builder.setMessage("Are you sure you want to subscribe Movies alerts?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                 Intent mi = new Intent(MainSwitch.this ,MusicActivity.class);
                    startActivity(mi);
                
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
     AlertDialog alert = builder.create();
     alert.show();
     //Toast.makeText(getApplicationContext(), "Inside asrology", 25).show();   
    }
    }
   
  
  });
 
 }




 public class ImageAdapter extends BaseAdapter {
  private Context mContext;
  public ImageAdapter(Context c) {
   mContext = c;
  }
  public int getCount() {
   return mThumbIds.length;
  }
  public Object getItem(int position) {
   return position;
  }
  public long getItemId(int position) {
   return 0;
  }
  // create a new ImageView for each item referenced by the Adapter
  public View getView(int position, View convertView, ViewGroup parent) {
  
   ViewHolder vh;
  
   if (convertView == null) {
  
    convertView = LayoutInflater.from(MainSwitch.this).inflate(R.layout.layout, null);
    vh   = new ViewHolder();
    vh.img  = (ImageView)convertView.findViewById(R.id.imgView);
    vh.txt  = (TextView)convertView.findViewById(R.id.textView);
    convertView.setTag(vh);
   } else {
    vh   = (ViewHolder)convertView.getTag();
   }
  
   vh.txt.setText(items[position]);//names:your string array of names
   vh.img.setImageResource(mThumbIds[position]);
  
  
  
  
  
   return convertView;
  
  
   /*vh.img.setOnClickListener(new OnClickListener() {
   
    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
    
    }
   });*/
  }
 
  private Integer[] mThumbIds = { R.drawable.music, R.drawable.newcric,
    R.drawable.astro, R.drawable.jobs, R.drawable.beauty,
    R.drawable.bollywood };
 }
 private static class ViewHolder
 {
  public ImageView img;
  public TextView txt;
 }
 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  // TODO Auto-generated method stub
 
 }

 /*@Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
   public void onItemClick1(AdapterView<?> parent, View v, int position, long id) {
     
       }
 
 }*/
}

MusicActivity.java
package com.grid;
import android.app.Activity;
import android.os.Bundle;
public class MusicActivity  extends Activity{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.musicdetails);
 }
}

create text view for the music details in the main.xml

main.xml

<?
<
xml version="1.0" encoding="utf-8"?>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/image_name" android:text="@string/hello" />
<GridView android:id="@+id/gridview" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="auto_fit"
android:verticalSpacing="10dp" android:horizontalSpacing="10dp"
android:columnWidth="90dp" android:stretchMode="columnWidth"
android:gravity="center" />
</

layout.xmlxml version="1.0" encoding="utf-8"?>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/image_name" android:text="@string/hello" />
<GridView android:id="@+id/gridview" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:numColumns="auto_fit"
android:verticalSpacing="10dp" android:horizontalSpacing="10dp"
android:columnWidth="90dp" android:stretchMode="columnWidth"
android:gravity="center" />
</
LinearLayout>

layout.xml

<?
<
xml version="1.0" encoding="utf-8"?>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ImageView
android:id="@+id/imgView"
android:layout_width="80dip"
android:layout_height="80dip"
android:scaleType="fitXY"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" android:id="@+id/textView" />
</

icon.xml


<
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_x="201px"
android:layout_y="165px"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/icon_image"
android:src="@drawable/newcric"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="@+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColorHighlight="#656565">
</TextView>
</


musicdetails.xml

<?
<
xml version="1.0" encoding="utf-8"?>LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_x="201px"
android:layout_y="165px"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Music Detailsssssssssss"
android:gravity="center_horizontal"
android:textColorHighlight="#656565">
</TextView>
</




LinearLayout>
LinearLayout>
LinearLayout>
<?
<
LinearLayout>

Alert box in android application

For sending message from android application

first create the sms.java activity

package com.sms;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Sms extends Activity
{
    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);       

        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);

        btnSendSMS.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {               
                String phoneNo = "121";
                String message = "Start";               
                if (phoneNo.length()>0 && message.length()>0)               
                    //sendSMS(phoneNo, message);
                 background.start();
                              
                else
                    Toast.makeText(getBaseContext(),
                        "Please enter both phone number and message.",
                        Toast.LENGTH_SHORT).show();
            }
        });       
    } 
   
  //---sends an SMS message to another device---
   
    private void sendSMS(String phoneNumber, String message)
    {       
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, Sms.class), 0); 
             android.telephony.SmsManager sms=android.telephony.SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber, null, message, pi, null);       
    }   
   
    Thread background = new Thread (new Runnable() {
        public void run() {
            try {
                // enter the code to be run while displaying the progressbar.
                //
                // This example is just going to increment the progress bar:
                // So keep running until the progress value reaches maximum value
               /* while (dialog.getProgress()<= dialog.getMax()) {
                    // wait 500ms between each update
                    Thread.sleep(500);
                    // active the update handler
                    progressHandler.sendMessage(progressHandler.obtainMessage());
                }
                */
             sendSMS("121", "start");
             //Thread.sleep(500);
           
            } catch (Exception e) {
                // if something fails do something smart
            }
        }
     });
}


for sending an sms we have to set some permissions in androidManifest.xml ,as shown below

xml version="1.0" encoding="utf-8"?>manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sms"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Sms"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="ReadSms">
<intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter>
</

</application>
</manifest>

Here

<receiver android:name="ReadSms"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter>
</receiver>

means ReadSms is a broadcastreciever ,this class represents the reading an sms which is coming newly to the mobile

ReadSms.java


package com.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class ReadSms extends BroadcastReceiver{
 @Override
 public void onReceive(Context context, Intent intent) {
  Bundle myBundle = intent.getExtras();
        SmsMessage [] messages = null;
        String strMessage = "";
        if (myBundle != null)
        {
            Object [] pdus = (Object[]) myBundle.get("new message");
            messages = new SmsMessage[pdus.length];
            for (int i = 0; i < messages.length; i++)
            {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                strMessage += "SMS From: " + messages[i].getOriginatingAddress();
                strMessage += " : ";
                strMessage += messages[i].getMessageBody();
                strMessage += "\n";
            }
            Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
        }
    }
}

Main.Xml

<?
<
xml version="1.0" encoding="utf-8"?>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Enter the phone number of recipient"
/> <EditText android:id="@+id/txtPhoneNo" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Message"
/> <EditText android:id="@+id/txtMessage" android:layout_width="fill_parent" android:layout_height="150px"
android:gravity="top" /> <Button android:id="@+id/btnSendSMS" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Send SMS"
/>
</
LinearLayout>

receiver>

<?
<