Sunday, March 30, 2014

ViewPager Demo


ViewPager Demo

  • mainactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/buttonOne"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/Green"
            android:text="@string/One"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/buttonTwo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/Yellow"
            android:text="@string/Two"
            android:textColor="@android:color/black" />

        <Button
            android:id="@+id/buttonThree"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/Red"
            android:text="@string/Three"
            android:textColor="@android:color/white" />
    </LinearLayout>

    <android.support.v4.view.ViewPager

        android:id="@+id/myTab"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />


</LinearLayout>
___________________________________________________________________________


  • One.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

     <TextView

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/Green"
        android:padding="5dp"
        android:text="@string/FragmentOne"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white" />



</LinearLayout>
___________________________________________________________________________

  • One.java
import com.example.viewpagerdemo.R;

import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class One extends
Fragment {



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.one, container, false);


return rootView;
}
}
___________________________________________________________________________


  • two.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/Yellow"
        android:padding="5dp"
        android:text="@string/FragmentTwo"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/black" />


</RelativeLayout>
___________________________________________________________________________


  • Two.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.viewpagerdemo.R;

public class Two extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.two, container, false);

return rootView;
}

}
___________________________________________________________________________


  • three.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

       <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="@color/Red"
            android:padding="5dp"
            android:text="@string/FragmentThree"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/myThreePageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="150dp"
            android:text="@string/FragmentThree" />
    </RelativeLayout>


</ScrollView>
___________________________________________________________________________


  • Three.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.viewpagerdemo.R;

public class Three extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.three, container, false);

Button btn = (Button) rootView.findViewById(R.id.myThreePageButton);

return rootView;
}

}
___________________________________________________________________________


  • MyTabAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyTabAdapter extends FragmentPagerAdapter {

public MyTabAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}

@Override
public Fragment getItem(int pageCount) {

switch (pageCount) {
case 0:
return new One();
case 1:
return new Two();
case 2:
return new Three();
}

return null;
}

@Override
public int getCount() {
return 3;
}



}
___________________________________________________________________________


  • PageTabClickListener.java
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;

public class PageTabClickListener implements OnClickListener {

private int pageIndex;
private ViewPager viewPager;

public PageTabClickListener(int pageIndex, ViewPager viewPager) {
this.viewPager = viewPager;
this.pageIndex = pageIndex;
}
public PageTabClickListener() {
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}

public void setViewPager(ViewPager viewPager) {
this.viewPager = viewPager;
}

@Override
public void onClick(View v) {
viewPager.setCurrentItem(pageIndex);
}

}
___________________________________________________________________________


  • MainActivity.java
import com.example.viewpagerdemo.R;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.widget.Button;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);

Button buttonOne, buttonTwo, buttonThree;

buttonOne = (Button) findViewById(R.id.buttonOne);
buttonTwo = (Button) findViewById(R.id.buttonTwo);
buttonThree = (Button) findViewById(R.id.buttonThree);

ViewPager viewPager = (ViewPager) findViewById(R.id.myTab);

MyTabAdapter tabAdapter = new MyTabAdapter(getSupportFragmentManager());

viewPager.setAdapter(tabAdapter);

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageScrollStateChanged(int arg0) {
}

});

buttonOne.setOnClickListener(new PageTabClickListener(0,viewPager));
buttonTwo.setOnClickListener(new PageTabClickListener(1,viewPager));
buttonThree.setOnClickListener(new PageTabClickListener(2,viewPager));

}

}
___________________________________________________________________________


    • colors.xml
    <?xml version="1.0" encoding="utf-8"?>
    <resources>

        <color name="Green">#008000</color>
        <color name="Red">#97072D</color>
        <color name="Yellow">#FFD700</color>

    </resources>

    ___________________________________________________________________________

      • strings.xml
      <resources>

          <string name="app_name">ViewPagerDemo</string>

          <string name="One">One</string>
          <string name="Two">Two</string>
          <string name="Three">Three</string>


          <string name="FragmentOne">Fragment One</string>
          <string name="FragmentTwo">Fragment Two</string>
          <string name="FragmentThree">Fragment Three</string>
          
      </resources>




      OutPut:




      Tuesday, March 18, 2014

      Save Contact In Phone


      • activity_main.xml

      <LinearLayout 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:orientation="vertical"

          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=".MainActivity" >

          <EditText
              android:id="@+id/edtDisplayName"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="3dp"
              android:ems="10"
              android:hint="@string/DisplayName" >
          </EditText>

          <EditText
              android:id="@+id/edtMobileNumber"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="3dp"
              android:ems="10"
              android:hint="@string/MobileNumber"
              android:inputType="phone" />

          <EditText
              android:id="@+id/edtHomeNumber"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="3dp"
              android:ems="10"
              android:hint="@string/HomeNumber"
              android:inputType="phone" />

          <EditText
              android:id="@+id/edtWorkNumber"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="3dp"
              android:ems="10"
              android:hint="@string/WorkNumber"
              android:inputType="phone" />

          <EditText
              android:id="@+id/edtemailID"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="3dp"
              android:ems="10"
              android:hint="@string/emailID"
              android:inputType="textEmailAddress" />

          <EditText
              android:id="@+id/edtcompany"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="3dp"
              android:ems="10"
              android:hint="@string/company" />

          <EditText
              android:id="@+id/edtjobTitle"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="3dp"
              android:ems="10"
              android:hint="@string/jobTitle" />

          <Button
              android:id="@+id/btnAddContact"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="3dp"
              android:text="@string/AddInPhoneBook" />

      </LinearLayout>


      • AndroidManifest.xml
          
          <uses-permission android:name="android.permission.READ_CONTACTS" />
          <uses-permission android:name="android.permission.WRITE_CONTACTS" />



      • MainActivity.java
      public class MainActivity extends Activity {

      private EditText edtDisplayName, edtMobileNumber, edtHomeNumber,
      edtWorkNumber, edtemailID, edtcompany, edtjobTitle;
      private Button btnAddContact;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      edtDisplayName = (EditText) findViewById(R.id.edtDisplayName);
      edtMobileNumber = (EditText) findViewById(R.id.edtMobileNumber);
      edtHomeNumber = (EditText) findViewById(R.id.edtHomeNumber);
      edtWorkNumber = (EditText) findViewById(R.id.edtWorkNumber);
      edtemailID = (EditText) findViewById(R.id.edtemailID);
      edtcompany = (EditText) findViewById(R.id.edtcompany);
      edtjobTitle = (EditText) findViewById(R.id.edtjobTitle);

      btnAddContact = (Button) findViewById(R.id.btnAddContact);

      btnAddContact.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
      // TODO Auto-generated method stub

      ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

      ops.add(ContentProviderOperation
      .newInsert(ContactsContract.RawContacts.CONTENT_URI)
      .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,
      null)
      .withValue(ContactsContract.RawContacts.ACCOUNT_NAME,
      null).build());

      // -------------- Names -------------- 
      if (edtDisplayName.getText().toString() != null) {
      ops.add(ContentProviderOperation
      .newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(
      ContactsContract.Data.RAW_CONTACT_ID, 0)
      .withValue(
      ContactsContract.Data.MIMETYPE,
      ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
      .withValue(
      ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
      edtDisplayName.getText().toString())
      .build());
      }

      // -------------- Mobile Number -------------- 
      if (edtMobileNumber.getText().toString() != null) {
      ops.add(ContentProviderOperation
      .newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(
      ContactsContract.Data.RAW_CONTACT_ID, 0)
      .withValue(
      ContactsContract.Data.MIMETYPE,
      ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
      .withValue(
      ContactsContract.CommonDataKinds.Phone.NUMBER,
      edtMobileNumber.getText().toString())
      .withValue(
      ContactsContract.CommonDataKinds.Phone.TYPE,
      ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
      .build());
      }

      // -------------- Home Numbers --------------

      if (edtHomeNumber.getText().toString() != null) {
      ops.add(ContentProviderOperation
      .newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(
      ContactsContract.Data.RAW_CONTACT_ID, 0)
      .withValue(
      ContactsContract.Data.MIMETYPE,
      ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
      .withValue(
      ContactsContract.CommonDataKinds.Phone.NUMBER,
      edtHomeNumber.getText().toString())
      .withValue(
      ContactsContract.CommonDataKinds.Phone.TYPE,
      ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
      .build());
      }

      // --------------Work  Numbers --------------

      if (edtWorkNumber.getText().toString() != null) {
      ops.add(ContentProviderOperation
      .newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(
      ContactsContract.Data.RAW_CONTACT_ID, 0)
      .withValue(
      ContactsContract.Data.MIMETYPE,
      ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
      .withValue(
      ContactsContract.CommonDataKinds.Phone.NUMBER,
      edtWorkNumber.getText().toString())
      .withValue(
      ContactsContract.CommonDataKinds.Phone.TYPE,
      ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
      .build());
      }

      //  -------------- Email --------------

      if (edtemailID.getText().toString() != null) {
      ops.add(ContentProviderOperation
      .newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(
      ContactsContract.Data.RAW_CONTACT_ID, 0)
      .withValue(
      ContactsContract.Data.MIMETYPE,
      ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
      .withValue(
      ContactsContract.CommonDataKinds.Email.DATA,
      edtemailID.getText().toString())
      .withValue(
      ContactsContract.CommonDataKinds.Email.TYPE,
      ContactsContract.CommonDataKinds.Email.TYPE_WORK)
      .build());
      }

      //   -------------- Organization --------------

      if (!edtcompany.getText().toString().equals("")
      && !edtjobTitle.getText().toString().equals("")) {
      ops.add(ContentProviderOperation
      .newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(
      ContactsContract.Data.RAW_CONTACT_ID, 0)
      .withValue(
      ContactsContract.Data.MIMETYPE,
      ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
      .withValue(
      ContactsContract.CommonDataKinds.Organization.COMPANY,
      edtcompany.getText().toString())
      .withValue(
      ContactsContract.CommonDataKinds.Organization.TYPE,
      ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
      .withValue(
      ContactsContract.CommonDataKinds.Organization.TITLE,
      edtjobTitle.getText().toString())
      .withValue(
      ContactsContract.CommonDataKinds.Organization.TYPE,
      ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
      .build());
      }

      // Asking the Contact provider to create a new contact

      try {
      getContentResolver().applyBatch(ContactsContract.AUTHORITY,
      ops);

      edtDisplayName.setText("");
      edtMobileNumber.setText("");
      edtHomeNumber.setText("");
      edtWorkNumber.setText("");
      edtemailID.setText("");
      edtcompany.setText("");
      edtjobTitle.setText("");

      Toast.makeText(getApplicationContext(), "Contact Save",
      Toast.LENGTH_SHORT).show();

      } catch (Exception e) {

      Toast.makeText(getApplicationContext(),
      "Exception: " + e.getMessage(), Toast.LENGTH_SHORT)
      .show();
      }

      }
      });

      }

      @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;
      }

      }

      Get Memory Details

      • activity_main.xml
      <LinearLayout 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:orientation="vertical"
          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=".MainActivity" >

          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/GET_MEMORY_DETAIL"
              android:textColor="@color/Black"
              android:textSize="20sp" />

          <View
              android:layout_width="fill_parent"
              android:layout_height="1dp"
              android:background="@color/Gray_divider" />

          <TextView
              android:id="@+id/txtAvailableInternal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="20dp"
              android:textColor="@color/Gray_Text" />

          <TextView
              android:id="@+id/txtTotalInternal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="@color/Red" />

          <View
              android:layout_width="fill_parent"
              android:layout_height="1dp"
              android:layout_marginBottom="20dp"
              android:layout_marginTop="20dp"
              android:background="@color/Gray_divider" />

          <TextView
              android:id="@+id/txtExternalAvailable"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="@color/Green" />

          <TextView
              android:id="@+id/txtAvailableExternal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="@color/Blue" />

          <TextView
              android:id="@+id/txtTotalExternal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="@color/Orange_divider" />

      </LinearLayout>


      • values/colors.xml

      <?xml version="1.0" encoding="utf-8"?>
      <resources>

          <color name="Green">#006400</color>
          <color name="Blue">#0e72c8</color>
          <color name="Red">#CC0000</color>
          <color name="Black">#000000</color>
          <color name="Gray_divider">#ccc</color>
          <color name="list_divider">#F2F2F2</color>
          <color name="Label_bg">#ececec</color>
          <color name="Orange_divider">#FE9A2E</color>
          <color name="Gray_Text">#808080</color>
          <color name="Gray_Header">#EBEBEB</color>

      </resources>


      • values/string.xml
        <string name="GET_MEMORY_DETAIL">GET MEMORY DETAIL</string>


      • MainActivity.java
      public class MainActivity extends Activity {

      private TextView txtAvailableInternal, txtTotalInternal,
      txtExternalAvailable, txtAvailableExternal, txtTotalExternal;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      txtAvailableInternal = (TextView) findViewById(R.id.txtAvailableInternal);
      txtTotalInternal = (TextView) findViewById(R.id.txtTotalInternal);
      txtExternalAvailable = (TextView) findViewById(R.id.txtExternalAvailable);
      txtAvailableExternal = (TextView) findViewById(R.id.txtAvailableExternal);
      txtTotalExternal = (TextView) findViewById(R.id.txtTotalExternal);

      txtAvailableInternal.setText("Available Internal MemorySize : "
      + getAvailableInternalMemorySize());

      txtTotalInternal.setText("Total Internal MemorySize : "
      + getTotalInternalMemorySize());

      txtExternalAvailable.setText("External Memory Available ? "
      + String.valueOf(externalMemoryAvailable()));

      txtAvailableExternal.setText("Available External MemorySize : "
      + getAvailableExternalMemorySize());

      txtTotalExternal.setText("Total External MemorySize : "
      + getTotalExternalMemorySize());

      }

      public static boolean externalMemoryAvailable() {
      return android.os.Environment.getExternalStorageState().equals(
      android.os.Environment.MEDIA_MOUNTED);
      }

      public static String getAvailableInternalMemorySize() {
      File path = Environment.getDataDirectory();
      StatFs stat = new StatFs(path.getPath());
      long blockSize = stat.getBlockSize();
      long availableBlocks = stat.getAvailableBlocks();
      return formatSize(availableBlocks * blockSize);
      }

      public static String getTotalInternalMemorySize() {
      File path = Environment.getDataDirectory();
      StatFs stat = new StatFs(path.getPath());
      long blockSize = stat.getBlockSize();
      long totalBlocks = stat.getBlockCount();
      return formatSize(totalBlocks * blockSize);
      }

      public static String getAvailableExternalMemorySize() {
      if (externalMemoryAvailable()) {
      File path = Environment.getExternalStorageDirectory();
      StatFs stat = new StatFs(path.getPath());
      long blockSize = stat.getBlockSize();
      long availableBlocks = stat.getAvailableBlocks();
      return formatSize(availableBlocks * blockSize);
      } else {
      return "ERROR";
      }
      }

      public static String getTotalExternalMemorySize() {
      if (externalMemoryAvailable()) {
      File path = Environment.getExternalStorageDirectory();
      StatFs stat = new StatFs(path.getPath());
      long blockSize = stat.getBlockSize();
      long totalBlocks = stat.getBlockCount();
      return formatSize(totalBlocks * blockSize);
      } else {
      return "ERROR";
      }
      }

      public static String formatSize(long size) {
      String suffix = null;

      if (size >= 1024) {
      suffix = "KB";
      size /= 1024;
      if (size >= 1024) {
      suffix = "MB";
      size /= 1024;
      }
      }

      StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

      int commaOffset = resultBuffer.length() - 3;
      while (commaOffset > 0) {
      resultBuffer.insert(commaOffset, ',');
      commaOffset -= 3;
      }

      if (suffix != null)
      resultBuffer.append(suffix);
      return resultBuffer.toString();
      }

      @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;
      }

      }