Saturday 11 January 2014

Android Fundamentals


  1. Activity
  2. Service
  3. Broadcast Receiver
  4. Content Provider.
Activity
An activity represents a single screen with a user interface.
The user can interact with the activity i.e. click the button.

service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface.

Example:
       Listening Music is the service. Because no user interaction is neccessary to play music. Once you start it will playing all the songs.
content provider manages a shared set of application data. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your application can access. 

Example:
       Consider that you are going to develop message sending application. For choosing the contacts you need to add contacts one by one in your application. 
       Instead of that you just share the contact which is in your phone contacts.

broadcast receiver is a component that responds to system-wide broadcast announcements.

            Example:
                      It reacts some thing, when the phone receive any messages or phone etc.

Thursday 9 January 2014

MD5 Encryption in Android


MD5 Encryption in Android

1.Md5.java

package com.JavaPages4All.md5encryption;

import java.security.MessageDigest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Md5 extends Activity

{
 EditText textview;
 Button get;
 TextView textlbl;
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.md5);

  textview=(EditText)findViewById(R.id.textview);
  get=(Button)findViewById(R.id.get);
  textlbl=(TextView)findViewById(R.id.textlbl);
  get.setOnClickListener(new OnClickListener()
  {
   @Override
   public void onClick(View arg0)
   {
    String strpassword=textview.getText().toString();
    if(strpassword!=null && strpassword.trim().length()!=0)
    {
     String md5=getMD5Encryption(strpassword);
     textlbl.setText(md5);
    }
   }
  });
 }
 public String getMD5Encryption(String strpassword)
 {
  try
  {
   // Create MD5 Hash

   MessageDigest digest = java.security.MessageDigest.getInstance("MD5");

   digest.update(strpassword.getBytes());
   byte messageDigest[] = digest.digest();
   // Create Hex String
   StringBuffer hexString = new StringBuffer();
   for (int i = 0; i < messageDigest.length; i++)
   {
    String h = Integer.toHexString(0xFF & messageDigest[i]);
    while (h.length() < 2)
     h = "0" + h;
    hexString.append(h);
   }
   return hexString.toString();
  } catch (Exception e)
  {
   e.printStackTrace();
  }
  return "";
 }
}
2.md5.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

      <EditText
          android:id="@+id/textview"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:ems="10"
          android:hint="Enter String"
          android:inputType="textPersonName" >
      
        <requestFocus />
       </EditText>
          
    <Button
        android:id="@+id/get"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get MD5" />

    <TextView
        android:id="@+id/textlbl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Md5 string"
        android:textColor="#ff00ff"
        android:textSize="20sp" />

</LinearLayout>

Hide the Title Bar and Status Bar in Android



Hide the Title Bar and Status Bar in Android

1.Hide the Title Bar Using AndroidManifest.xml

If you want to hide the title bar for particular Activity 
use the below code in AndroidManifest.xml within the <activity> tag.

 android:theme="@android:style/Theme.Black.NoTitleBar"

Example:
     <activity
            android:name=" MainScreen"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar " >

 Hide the Title Bar and Status Bar Using XML code.

If you want to hide the Title Bar and Status Bar for particular activity, use the below code in AndroidManifest.xml within the <activity> tag.

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

Example:
     <activity
            android:name=MainScreen"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen " >

Hide the Title Bar and Status Bar for entire Application Using XML code.

 If you want to hide the Title Bar and Status Bar for entire application,just use the above code in <application> tag.

Example:

 for hiding title bar:
        <application
         android:icon="@drawable/icon"
         android:label="@string/app_name"
         android:theme="@android:style/Theme.Black.NoTitleBar" >

 for hiding title and status bar:
        <application
         android:icon="@drawable/icon"
         android:label="@string/app_name"
         android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
                Working in Java file using java code

Hide the Title Bar Using java code.

If you want to hide the title bar for particular activity, use this java code above the setContentView() method.

requestWindowFeature(Window.FEATURE_NO_TITLE);

Android Spinner View Text Not Visible


Android Spinner View Text Not Visible 

(White background on Text Also showing White Color)

---------------------------------------------------------------------------------------------------------------------------------


<Spinner
     android:id="@+id/courselist"     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="1" />
Spinner spnrcourse=(Spinner)findViewById(R.id.courselist);
String[] courselist = {"Java", "Cpp", "Oracle","Android" };
spnrcourse.setAdapter(new ArrayAdapter(getApplicationContext(), 
android.R.layout.simple_dropdown_item_1line, courselist));

Replace getApplicationContext() to getBaseContext()
                              (OR)
Replace getApplicationContext() to this (if above is not working)
                              (OR)
Replace getApplicationContext() to getActivity() (if you are Using Fragment) 


Wednesday 8 January 2014

Mutable and Immutable Object in Java



package raja.JavaPages4All;

import java.util.Date;

/**
 * @author Raja(JavaPages4All)
 */
public class TestPojo 
{
 private String blogName;
 private Date date;

 public TestPojo(String blogName,Date date)
 {
  this.blogName=blogName;
  this.date = date;
 }

 public String getBlogName()
 {
  return this.blogName;
 }

 public Date getDate()
 {
  return this.date;
 }
}



package raja.JavaPages4All;

import java.util.Date;

public class ImmutableTest 
{

 /**
  * @author Raja(JavaPages4All)
  */
 public static void main(String[] args) 
 {
  /*
   * String Class is Immutable.
   * Date Class is Mutable. 
   */
  String str="JavaPages4All";
  Date myDate = new Date();

  TestPojo pojo =new TestPojo(str,myDate);

  /**
   * Getting Values from TestPojo Reference Object(When First Time) 
   */
  System.out.println(pojo.getBlogName());
  System.out.println( pojo.getDate() );

  /**
   * String str Object value changing so it is Creating Newly(Immutable).
   * Date myDate Object value changing so it is Updated the 
   * Previous Object myDate Value(Mutable)
   */
  str="Raja";
  myDate.setMonth( myDate.getMonth() + 1 );

  /**
   * Getting Values from TestPojo Reference Object
   * (When Second Time object Values Changed)
   */
  System.out.println(pojo.getBlogName()+"  "+str);
  System.out.println( pojo.getDate() );
 }

}




Mutable vs Immutable in Java


Mutable Objects: When you have a reference to an instance of an object, the contents of that instance can be altered

Immutable Objects: When you have a reference to an instance of an object, the contents of that instance cannot be altered

(OR)
Immutable objects are simply objects whose state (the object's data) cannot change after construction

Immutability and Instances

To demonstrate this behaviour, we'll use java.lang.String as the immutable class and java.awt.Point as the mutable class.
package raja.JavaPages4all;

import java.awt.Point;

public class Immutability  
{
 /**
  * @author Raja(JavaPages4All)
  * @param args
  */
 public static void main(String[] args) 
 {
  Point pt= new Point( 0, 0 );
  System.out.println( pt);
  pt.setLocation( 1.0, 0.0 );
  System.out.println( pt);

  String str= new String( "old String" );
  System.out.println( str);
  str.replaceAll( "old", "new" );
  System.out.println( str);
 }
}
In case you can't see what the output is, here it is:
 java.awt.Point[0.0, 0.0]
 java.awt.Point[1.0, 0.0]
 old String
 old String

We are only looking at a single instance of each object, but we can see that the contents of pt has changed, but the contents of str did not. To show what happens when we try to change the value of str, we'll extend the previous example.
package raja.JavaPages4all;

import java.awt.Point;

public class Immutability  
{
 /**
  * @author Raja(JavaPages4All)
  * @param args
  */
 public static void main(String[] args) 
 {
  Point pt= new Point( 0, 0 );
  System.out.println( pt);
  pt.setLocation( 1.0, 0.0 );
  System.out.println( pt);

  String str= new String( "old String" );
  System.out.println( str );
  str= new String( "new String" );
  System.out.println( str );
 }
}
The output from this is:
 old String
 new String
Now we find that the value displayed by the str variable has changed. We have defined immutable objects as being unable to change in value, so what is happening? Let's extend the example again to watch the str variable closer.
package raja.JavaPages4all;


public class Immutability  
{
 /**
  * @author Raja(JavaPages4All)
  * @param args
  */
 public static void main(String[] args) 
 {
  //Comparison
  String str = new String( "old String" );
  String myCache = str ;
  System.out.println( "String Comparison: " + str.equals( myCache ) );
  System.out.println( "Object Comparison:  " + ( str == myCache ) );

  str = "not " + str ;
  System.out.println( "String Comparison: " + str.equals( myCache ) );
  System.out.println( "Object Comparison:  " + ( str == myCache ) );
 }
}
The result from executing this is:
 String Comparison: true
 Object Comparison: true
 String Comparison: false
 Object Comparison: false
What this shows is that variable str is referencing a new instance of the String class. The contents of the object didn't change; we discarded the instance and changed our reference to a new one with new contents.