Sunday, 18 January 2015

How to clear notification in Android

Cancel the Notification that we started
 
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager Nmang = (NotificationManager) getApplicationContext()
.getSystemService(ns);
Nmang .cancel(getIntent().getExtras().getInt("notificationID"));

Put the above code in Notification Activity...
For examble.
public class NotificationView extends Activity
{

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.notification);
      String ns = Context.NOTIFICATION_SERVICE;
NotificationManager Nmang = (NotificationManager)                 
getApplicationContext() .getSystemService(ns);
Nmang.cancel(getIntent().getExtras().getInt("notificationID"));
    }
  protected void displayNotification()
{

  int notificationID = 100;
    NotificationManager mNotificationManager;
   int numMessages = 0;
  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this);
    mBuilder.setContentTitle("New Message");
mBuilder.setContentText("You've received new message.");
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.ic_launcher);  
    mBuilder.setNumber(++numMessages);
    Intent resultIntent = new Intent(this, NotificationView.class);
resultIntent.putExtra("notificationID", notificationID);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NotificationView.class);
    stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);   
    mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager)          
    getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notificationID, mBuilder.build());
}

Monday, 12 January 2015

Android Get ListView children that are not in view

Get ListView children that are not in view:



Step 1).Get The Count of the Listview.

    
               int d = listView.getCount();
              for (int i = 0; i < di++)
              {
View v = getViewByPosition(ilistView);
             }

Step 2)You need to create your method.

   public View getViewByPosition(int position, ListView listView)
           {
               final int firstListItemPosition = listView.getFirstVisiblePosition();
               final int lastListItemPosition = firstListItemPosition +                                                                                                  listView.getChildCount() - 1;
                if (positionfirstListItemPosition || positionlastListItemPosition )
                 {
                  return  listView.getAdapter().getView(position, nulllistView);
                 }
                 else
                 {
                  final int childIndex = position - firstListItemPosition;
                  return   listView.getChildAt(childIndex);
                 }
        }






      

Friday, 9 January 2015

Android Reading Text File from URL

Android Reading Text File from URL
 String value = readfile(url);
private static String readfile(String src)
{
       String result = "";
try
            {  
             String line;
            URL url  = new URL(src);
             URLConnection connection = url.openConnection();
           BufferedReader in;
             in =new BufferedReader(new InputStreamReader(
connection.getInputStream()));
              while ((line = in.readLine()) != null) 
                 {
                       result += line;
                 }        
in.close();

}
catch (MalformedURLException e)
{
e.printStackTrace();
 }
     catch (IOException e)             
{
            e.printStackTrace();
            }            
return result;    
 }

Thursday, 8 January 2015

Android Converting URL to Bitmap

Get Bitmap from URL in Android       
Bitmap b=getBitmap(url);   //pass a valid url as a parameter   public static Bitmap getBitmapFromURL(String src)
  {    
    try  
      {     
          URL url = new URL(src);       
          HttpURLConnection connection;        
          connection=(HttpURLConnection) url.openConnection();                                                             connection.setDoInput(true);        
          connection.connect();       
          InputStream input = connection.getInputSt ream();                                          
          Bitmap myBitmap = BitmapFactory.decodeStream(input);                                                         
          return myBitmap;   
         }
  catch (IOException e)
    {         
      e.printStackTrace();        
      return null;     }
  }