Wednesday 31 December 2014

Creating Custom Dialog from Layout File


In this post, I will show how to create a custom dialog using xml layout file.
First you have to create a xml layout file. After creating layout file than you have to open activity in which you want to display dialog. Here, layout file contains a button "OK" as an example.

In the activity, declare the Dialog and than create a method as below:

private Dialog myDialog;

private void showDialog() { 


myDialog = new Dialog(this,R.style.wideDialogStyle);
myDialog.setCancelable(false);
myDialog.setCanceledOnTouchOutside(false);
myDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
myDialog.setContentView(R.layout.log_in);
myDialog.show(); 
 
// Set up the Button
okBtn = (Button) myDialog.findViewById(R.id.ok_btn); okBtn.setOnClickListener(okBtnListener); 
} 

//This is the onClickListener() for OK button of the Dialog
private Button.OnClickListener okBtnListener = new Button.OnClickListener() { 
@Override 
public void onClick(View v) { 
myDialog.dismiss();


Thank you for visiting my blog.

Monday 22 December 2014

Check SIM card availability in Android.


 
Sometimes, It is necessary to check the availability of Network service in Android. Here is the code.

TelephonyManager telephonyManager= (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

int simState = telephonyManager.getSimState();
switch (simState) {

    case TelephonyManager.SIM_STATE_ABSENT:
       Toast.makeText(context, "SIM Card Not Available., Toast.LENGTH_SHORT).show();
       break;
     case TelephonyManager.SIM_STATE_READY:
       Toast.makeText(context, "SIM Card Available.", Toast.LENGTH_SHORT).show();

}

Sunday 21 December 2014

Viewpager/Fragment Tabs Adapter

While working with Viewpager which contains multiple Tabs and each tab is a fragment, it is necessary to have an adapter which decides which fragment should be called when user taps on a specific viewpager tab. For this you have to specify a list of fragments which is passed to adapter then this adapter is set to the viewpager. Here is the method to add and returns an ArrayList of type Fragment.
    
    public ArrayList<Fragment> defaultFragmentList(){
         
        ArrayList<Fragment> fragmentsList = new ArrayList<Fragment>();
         
        fragmentsList.add(new FirstFragment());
        fragmentsList.add(new SecondFragment());
        fragmentsList.add(new ThirdFragment());
         
        return fragmentsList;
    }
After having an arraylist of fragments type we have to pass this list to tabs adapter. Here is the method which takes the fragments arraylist and pass it to adapter.This adapter is set to the viewpager as below:


    public void populateFragments(ArrayList<Fragment> fragmentsList){
         
        mAdapter = new TabsPagerAdapter(this, getSupportFragmentManager(), fragmentsList);   
        viewPager.setAdapter(mAdapter);       
    }

In the onCreate method of the main activity, we call the above methods as:

populateFragments(defaultFragmentList());


Now, here is the complete code of TabsPagerAdapter class. 
import java.util.ArrayList;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
    final int TABs=3;
    Context context;
    FragmentManager mFragmentManager;
    ArrayList<Fragment> mFragmentsList;
    public TabsPagerAdapter(Context mContext, FragmentManager fm, ArrayList<Fragment> fragmentsList) {
        super(fm);
        
        context=mContext; 
        mFragmentsList = fragmentsList;     
        mFragmentManager=fm;
    }
    @Override
    public Fragment getItem(int index) {
    
         Fragment fragment =null;                   
         switch (index) {         
         case 0:
             // FirstFragment
             fragment = mFragmentsList.get(0);
             break;
         case 1:   
             // SecondFragment
             fragment = mFragmentsList.get(1);
              break;
         case 2:
             // ThirdFragment
             fragment = mFragmentsList.get(2);
              break;
         }       
         return fragment;                        
    }       
    @Override
    public int getCount() {
         
        // get item count - equal to number of tabs
        return TABs;
    

This is one of the simple code.
In case you feel that I need to improve this post/blog Please don't hesitate to comment.
Thank you.

Calling an Activity from Service

To call an activity from service class through intent, write the code as below:


Intent intent=new Intent(UpdateWidgetService.this, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

where

"UpdateWidgetService" is the service class from where you are calling activity.
"Main" is the name of activity class.