Saturday, July 26, 2014

Android Paypal Gateway Example | Paypal demo in android | Pay inside App using Paypal and Credit card


Hi Friends, in past we were using third party library for Paypal payment but now Paypal provide us their own library for payment so it is now much secure and easy to implement in our application. Below are the important step to do -




1)First go through Paypal Developer web site and create an application.

2)Now open your manifest file and give the below permissions-

<uses-permission android:name="android.permission.INTERNET" />

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


3)And some required Activity and Services-

<service
            android:name="com.paypal.android.sdk.payments.PayPalService"
            android:exported="false" />

        <activity android:name="com.paypal.android.sdk.payments.PaymentActivity" />
        <activity android:name="com.paypal.android.sdk.payments.LoginActivity" />
        <activity android:name="com.paypal.android.sdk.payments.PaymentMethodActivity" />
        <activity android:name="com.paypal.android.sdk.payments.PaymentConfirmActivity" />
        <activity android:name="com.paypal.android.sdk.payments.PayPalFuturePaymentActivity" />
        <activity android:name="com.paypal.android.sdk.payments.FuturePaymentConsentActivity" />
        <activity android:name="com.paypal.android.sdk.payments.FuturePaymentInfoActivity" />
        <activity
            android:name="io.card.payment.CardIOActivity"
            android:configChanges="keyboardHidden|orientation" />
        <activity android:name="io.card.payment.DataEntryActivity" />


4)Open your Activity class and set Configuration for your app-
//set the environment for production/sandbox/no netowrk
       private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_PRODUCTION;


5)Now set client id from the Paypal developer account-
     private static final String CONFIG_CLIENT_ID = "PUT YOUR CLIENT ID";


6)Inside onCreate method call the Paypal service-
Intent intent = new Intent(this, PayPalService.class);
        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
        startService(intent);


7)Now you are ready to make a payment just on button press call the Payment Activity-
PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(1),"USD", "androidhub4you.com",
                                 PayPalPayment.PAYMENT_INTENT_SALE);

                             Intent intent = new Intent(MainActivity.this, PaymentActivity.class);

                             intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);


                             startActivityForResult(intent, REQUEST_PAYPAL_PAYMENT);    


8)And finally from the onActivityResult get the payment response-
  @Override
           protected void onActivityResult(int requestCode, int resultCode, Intent data) {
               if (requestCode == REQUEST_PAYPAL_PAYMENT) {
                   if (resultCode == Activity.RESULT_OK) {
                       PaymentConfirmation confirm = data
                               .getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
                       if (confirm != null) {
                           try {
                           System.out.println("Responseeee"+confirm);
                               Log.i("paymentExample", confirm.toJSONObject().toString());

                            
                               JSONObject jsonObj=new JSONObject(confirm.toJSONObject().toString());
                              
                               String paymentId=jsonObj.getJSONObject("response").getString("id");
                               System.out.println("payment id:-=="+paymentId);
                               Toast.makeText(getApplicationContext(), paymentId, Toast.LENGTH_LONG).show(); 

                           } catch (JSONException e) {
                               Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
                           }
                       }
                   } else if (resultCode == Activity.RESULT_CANCELED) {
                       Log.i("paymentExample", "The user canceled.");
                   } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
                       Log.i("paymentExample", "An invalid Payment was submitted. Please see the docs.");
                   }
               }
              
               
         }




For any query or suggestion please post your feedback.

Thanks,



Friday, July 11, 2014

Android Multilevel Expandable ListView | Custom Expandable List-view Demo In Android | Android Expandable Listview Example with demo code

Hello Friends,

Today I am going to share code for multilevel  list-view or multilevel expandable list-view, means till 3 stage data selection from a list. like- Country->state->city

Country--India-------------Delhi
               England               Mumbai
               America              Gurganv

For this demo I am using Views, No core android list-view or expandable list-view. Hope it will help to some one.






MainActivity.java


package com.androidhub4you.multilevellistview;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.androidhub4you.multilevellistview.Product.SubCategory;
import com.androidhub4you.multilevellistview.Product.SubCategory.ItemList;
/**
 *
 * @author manish
 *
 */
public class MainActivity extends Activity {
      
       private ArrayList<Product>pProductArrayList;
       private ArrayList<SubCategory>pSubItemArrayList;
       private ArrayList<SubCategory>pSubItemArrayList2;
       private LinearLayout mLinearListView;
       boolean isFirstViewClick=false;
       boolean isSecondViewClick=false;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              mLinearListView = (LinearLayout) findViewById(R.id.linear_listview);
             
              /**
               *
               */
             
              ArrayList<ItemList> mItemListArray=new ArrayList<ItemList>();
              mItemListArray.add(new ItemList("Red", "20"));
              mItemListArray.add(new ItemList("Blue", "50"));
              mItemListArray.add(new ItemList("Red", "20"));
              mItemListArray.add(new ItemList("Blue", "50"));
             
              ArrayList<ItemList> mItemListArray2=new ArrayList<ItemList>();
              mItemListArray2.add(new ItemList("Pant", "2000"));
              mItemListArray2.add(new ItemList("Shirt", "1000"));
              mItemListArray2.add(new ItemList("Pant", "2000"));
              mItemListArray2.add(new ItemList("Shirt", "1000"));
              mItemListArray2.add(new ItemList("Pant", "2000"));
              mItemListArray2.add(new ItemList("Shirt", "1000"));
             
             
              /**
               *
               */
              pSubItemArrayList=new ArrayList<SubCategory>();
              pSubItemArrayList2=new ArrayList<SubCategory>();
              pSubItemArrayList.add(new SubCategory("Color", mItemListArray));
              pSubItemArrayList2.add(new SubCategory("Cloths", mItemListArray2));
              pSubItemArrayList.add(new SubCategory("Color", mItemListArray));
              pSubItemArrayList2.add(new SubCategory("Cloths", mItemListArray2));
              /**
               *
               */
             
              pProductArrayList=new ArrayList<Product>();
              pProductArrayList.add(new Product("Emotions", pSubItemArrayList));
              pProductArrayList.add(new Product("Garments", pSubItemArrayList2));
             
             
              /***
               * adding item into listview
               */
              for (int i = 0; i < pProductArrayList.size(); i++) {
                    
                     LayoutInflater inflater = null;
                     inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     View mLinearView = inflater.inflate(R.layout.row_first, null);
                    
                     final TextView mProductName = (TextView) mLinearView.findViewById(R.id.textViewName);
                     final RelativeLayout mLinearFirstArrow=(RelativeLayout)mLinearView.findViewById(R.id.linearFirst);
                     final ImageView mImageArrowFirst=(ImageView)mLinearView.findViewById(R.id.imageFirstArrow);
                     final LinearLayout mLinearScrollSecond=(LinearLayout)mLinearView.findViewById(R.id.linear_scroll);
                    
                     if(isFirstViewClick==false){
                     mLinearScrollSecond.setVisibility(View.GONE);
                     mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
                     }
                     else{
                           mLinearScrollSecond.setVisibility(View.VISIBLE);
                           mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
                     }
                    
                     mLinearFirstArrow.setOnTouchListener(new OnTouchListener() {
                          
                           @Override
                           public boolean onTouch(View v, MotionEvent event) {
                                 
                                  if(isFirstViewClick==false){
                                         isFirstViewClick=true;
                                         mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
                                         mLinearScrollSecond.setVisibility(View.VISIBLE);
                                        
                                  }else{
                                         isFirstViewClick=false;
                                         mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
                                         mLinearScrollSecond.setVisibility(View.GONE);  
                                  }
                                  return false;
                           }
                     });
                    
                    
                     final String name = pProductArrayList.get(i).getpName();
                     mProductName.setText(name);
                 
                     /**
                      *
                      */
                  for (int j = 0; j < pProductArrayList.get(i).getmSubCategoryList().size(); j++) {
                          
                           LayoutInflater inflater2 = null;
                           inflater2 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                           View mLinearView2 = inflater2.inflate(R.layout.row_second, null);
                 
                           TextView mSubItemName = (TextView) mLinearView2.findViewById(R.id.textViewTitle);
                           final RelativeLayout mLinearSecondArrow=(RelativeLayout)mLinearView2.findViewById(R.id.linearSecond);
                           final ImageView mImageArrowSecond=(ImageView)mLinearView2.findViewById(R.id.imageSecondArrow);
                           final LinearLayout mLinearScrollThird=(LinearLayout)mLinearView2.findViewById(R.id.linear_scroll_third);
                          
                           if(isSecondViewClick==false){
                                  mLinearScrollThird.setVisibility(View.GONE);
                                  mImageArrowSecond.setBackgroundResource(R.drawable.arw_lt);
                                  }
                                  else{
                                         mLinearScrollThird.setVisibility(View.VISIBLE);
                                         mImageArrowSecond.setBackgroundResource(R.drawable.arw_down);
                                  }
                                 
                           mLinearSecondArrow.setOnTouchListener(new OnTouchListener() {
                                        
                                         @Override
                                         public boolean onTouch(View v, MotionEvent event) {
                                               
                                                if(isSecondViewClick==false){
                                                       isSecondViewClick=true;
                                                       mImageArrowSecond.setBackgroundResource(R.drawable.arw_down);
                                                       mLinearScrollThird.setVisibility(View.VISIBLE);
                                                      
                                                }else{
                                                       isSecondViewClick=false;
                                                       mImageArrowSecond.setBackgroundResource(R.drawable.arw_lt);
                                                       mLinearScrollThird.setVisibility(View.GONE);   
                                                }
                                                return false;
                                         }
                                  });
                          
                          
                           final String catName = pProductArrayList.get(i).getmSubCategoryList().get(j).getpSubCatName();
                           mSubItemName.setText(catName);
                           /**
                            *
                            */
                             for (int k = 0; k < pProductArrayList.get(i).getmSubCategoryList().get(j).getmItemListArray().size(); k++) {
                                        
                                         LayoutInflater inflater3 = null;
                                         inflater3 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                         View mLinearView3 = inflater3.inflate(R.layout.row_third, null);
                              
                                         TextView mItemName = (TextView) mLinearView3.findViewById(R.id.textViewItemName);
                                         TextView mItemPrice = (TextView) mLinearView3.findViewById(R.id.textViewItemPrice);
                                         final String itemName = pProductArrayList.get(i).getmSubCategoryList().get(j).getmItemListArray().get(k).getItemName();
                                         final String itemPrice = pProductArrayList.get(i).getmSubCategoryList().get(j).getmItemListArray().get(k).getItemPrice();
                                         mItemName.setText(itemName);
                                         mItemPrice.setText(itemPrice);
                                        
                                         mLinearScrollThird.addView(mLinearView3);
                                        
                                         /**
                                          * set click
                                          */
                                        
                                         mLinearView3.setOnClickListener(new OnClickListener() {
                                               
                                                @Override
                                                public void onClick(View v) {
                                                       // TODO Auto-generated method stub
                                                      
                                                       Toast.makeText(MainActivity.this, "clicked item:"+itemName, Toast.LENGTH_SHORT).show();
                                                }
                                         });
                             }
                          
                           mLinearScrollSecond.addView(mLinearView2);
                 
                  }
                 
                  mLinearListView.addView(mLinearView);
              }            
       }

}




Your feedback awaited!
Thanks,
Manish