Thursday, 6 July 2023

JetPack Compose : ListView Example with ItemClick Listener

 

 1. Add the required dependencies to your project's build.gradle file:

 

In root Project gradle file

buildFeatures {
        compose true
 

In app module gradle file

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.compose.ui:ui:1.0.4'
    implementation 'androidx.compose.material:material:1.2.1'
    implementation 'androidx.compose.ui:ui-tooling:1.0.4'
}

 2. Create a new Composable function in your activity or fragment

package com.rajeshvijayakumr.jetpackcompose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

class ListViewActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val items = listOf(
"Item 1", "Item 2", "Item 3",
"Item 4", "Item 5", "Item 6"
)

setContent {
MyScreen(items = items)
}
}
}

@Composable
fun MyScreen(items: List<String>) {
LazyColumn {
items(items) { item ->
ListItem(item = item)
}
}
}

@Composable
fun ListItem(item: String) {
val context = LocalContext.current
// Use Clickable modifier to handle item click
Column(
modifier = Modifier
.clickable { showToast(context, item) }
.fillMaxWidth()
.fillMaxWidth()
.padding(6.dp)
.background(
colorResource(id = android.R.color.holo_blue_dark),
shape = RectangleShape
),
verticalArrangement = Arrangement.Center,
) {

Text(
text = item,
modifier = Modifier
.fillMaxWidth()
.padding(9.dp)
.height(60.dp)
.wrapContentSize(align = Alignment.CenterStart),
color = Color.White,
textAlign = TextAlign.Start

)
}
}

fun showToast(context: Context, item: String) {
Toast.makeText(
context,
"Clicked: $item",
Toast.LENGTH_SHORT
).show()
}
 

In the above example, MyScreen is a composable that takes a list of items and creates a LazyColumn with items to iterate over the list. For each item, it calls the ListItem composable, passing the item as a parameter.

ListItem is a custom composable that represents each item in the list. You can define the content and layout for each item inside this composable. In this example, it's a Column with a Text component displaying the item's text. You can add additional custom content for each item as needed.

In the MainActivity, the list of items is provided to the MyScreen composable, which sets the content view of the activity using setContent.

When you run the app, you should see a list of items displayed vertically, and you can customize the content and layout of each item in the ListItem composable based on your requirements.

 Output ScreenShot




 

 

 

 

 

 

 

 

 

Source Code : Click Here to Github Link

Thursday, 22 June 2023

JetPack Compose : Simple ListView Example

 1. Add the required dependencies to your project's build.gradle file:

 

In root Project gradle file

buildFeatures {
        compose true
 

In app module gradle file

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.compose.ui:ui:1.0.4'
    implementation 'androidx.compose.material:material:1.2.1'
    implementation 'androidx.compose.ui:ui-tooling:1.0.4'
}

 2. Create a new Composable function in your activity or fragment

package com.rajeshvijayakumr.jetpackcompose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

class ListViewActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val items = listOf("Item 1", "Item 2", "Item 3","Item 4", "Item 5",
                               "Item 6","Item 7", "Item 8", "Item 9")

setContent {
MyScreen(items)
}
}
}

@Composable
fun MyScreen(items: List<String>) {
LazyColumn {
items(items) { item ->
ListItem(item = item)
}
}
}

@Composable
fun ListItem(item: String) {
Column(
modifier = Modifier
.padding(16.dp)
.fillMaxWidth()
) {
Text(text = item)
// Add other custom content for each item
}
}



In the above example, MyScreen is a composable that takes a list of items and creates a LazyColumn with items to iterate over the list. For each item, it calls the ListItem composable, passing the item as a parameter.

ListItem is a custom composable that represents each item in the list. You can define the content and layout for each item inside this composable. In this example, it's a Column with a Text component displaying the item's text. You can add additional custom content for each item as needed.

In the MainActivity, the list of items is provided to the MyScreen composable, which sets the content view of the activity using setContent.

When you run the app, you should see a list of items displayed vertically, and you can customize the content and layout of each item in the ListItem composable based on your requirements.

 Output ScreenShot



Source Code : Click Here to Github Link

Friday, 8 July 2016

Horizontal List using recycler view in android

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:layout_gravity="center"
/>

item_view.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"
android:background="#efefef"
android:layout_margin="5dp">
<ImageView
android:id="@+id/item_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="@android:drawable/ic_notification_overlay"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/item_icon"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:padding="5dp"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/item_text"
android:layout_width="match_parent"
android:layout_height="26dp"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="16sp"/>
<TextView
android:id="@+id/item_text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=":)"
android:textColor="@android:color/holo_blue_bright"
android:textSize="12sp"/>
</LinearLayout>
</RelativeLayout>
  

MyRecyclerHolder.java

 package com.rajeshvijayakumar.recycler;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MyRecyclerHolder extends RecyclerView.ViewHolder {
 
public TextView textView;
public ImageView imageView;
public TextView textView2;
 
    public MyRecyclerHolder(View view) {
      super(view);
      this.imageView = (ImageView) view.findViewById(R.id.item_icon);
      this.textView = (TextView) view.findViewById(R.id.item_text);
      this.textView2 =(TextView) view.findViewById(R.id.item_text2);
    }
}

MyRecyclerAdapter.java

package com.rajeshvijayakumar.recycler;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerHolder> {

String[] menus;
Context adapContext;
public MyRecyclerAdapter(Context context, String[] menu) { adapContext = context; menus = menu;
}
@Override
public MyRecyclerHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.item, null);
MyRecyclerHolder viewHolder = new MyRecyclerHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyRecyclerHolder viewHolder, int i) {
// int length = (menus != null && menus.length > 0) ? menus.length : 0; if (i < length) {
viewHolder.textView.setText(menus[i]);
}
}
@Override
public int getItemCount() {
return (null != menus ? menus.length : 0);
}
//
}

RecyclerDemoActivity.java

package com.rajeshvijayakumar.recycler;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class RecyclerDemoActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private MyRecyclerAdapter adapter;
private String[] menus = { "item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10",
"item11", "item12", "item13", "item14", "item15", "item16", "item17", "item18", "item19", "item20"};

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

// Initialize recycler view mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(layoutManager);
adapter = new MyRecyclerAdapter(this, menus);
mRecyclerView.setAdapter(adapter);
}
}



Output :





Veritcal List using RecyclerView Example in Android

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:layout_gravity="center"
/>

item_view.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"
android:background="#efefef"
android:layout_margin="5dp">
<ImageView
android:id="@+id/item_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="@android:drawable/ic_notification_overlay"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/item_icon"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:padding="5dp"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/item_text"
android:layout_width="match_parent"
android:layout_height="26dp"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="16sp"/>
<TextView
android:id="@+id/item_text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=":)"
android:textColor="@android:color/holo_blue_bright"
android:textSize="12sp"/>
</LinearLayout>
</RelativeLayout>
  

MyRecyclerHolder.java

 package com.rajeshvijayakumar.recycler;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MyRecyclerHolder extends RecyclerView.ViewHolder {
 
public TextView textView;
public ImageView imageView;
public TextView textView2;
 
    public MyRecyclerHolder(View view) {
      super(view);
      this.imageView = (ImageView) view.findViewById(R.id.item_icon);
      this.textView = (TextView) view.findViewById(R.id.item_text);
      this.textView2 =(TextView) view.findViewById(R.id.item_text2);
    }
}

MyRecyclerAdapter.java

package com.rajeshvijayakumar.recycler;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerHolder> {

String[] menus;
Context adapContext;
public MyRecyclerAdapter(Context context, String[] menu) { adapContext = context; menus = menu;
}
@Override
public MyRecyclerHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.item, null);
MyRecyclerHolder viewHolder = new MyRecyclerHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyRecyclerHolder viewHolder, int i) {
// int length = (menus != null && menus.length > 0) ? menus.length : 0; if (i < length) {
viewHolder.textView.setText(menus[i]);
}
}
@Override
public int getItemCount() {
return (null != menus ? menus.length : 0);
}
//
}

RecyclerDemoActivity.java

package com.rajeshvijayakumar.recycler;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class RecyclerDemoActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private MyRecyclerAdapter adapter;
private String[] menus = { "item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10",
"item11", "item12", "item13", "item14", "item15", "item16", "item17", "item18", "item19", "item20"};

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

// Initialize recycler view mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
adapter = new MyRecyclerAdapter(this, menus);
mRecyclerView.setAdapter(adapter);
}
}


Output :






Sunday, 2 August 2015

Uber Like Setting Location by dragging a map in Android

activity_main.xml

<RelativeLayout 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" >

    <fragment
        android:id="@+id/map"
        android:name="com.rajeshvijayakumar.map.demo.ui.CustomMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <include
        android:id="@+id/marker_view_incl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/marker_view" />

    <include
        android:id="@+id/location_display_incl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="7dp"
        layout="@layout/location_display_view" />

</RelativeLayout>

 marker_view.xml 

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

    <ImageView
        android:id="@+id/marker_icon_view"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerInParent="true"
        android:layout_marginTop="162dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

location_display_view.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="60dp"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Set Location"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/location_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_view"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Set Location"
        android:textColor="@android:color/black"
        android:textSize="14sp" />

</RelativeLayout>

MapWrapperLayout.java

 package com.rajeshvijayakumar.map.demo.wrapper;

import android.content.Context;
import android.view.MotionEvent;
import android.widget.FrameLayout;

public class MapWrapperLayout extends FrameLayout {

    public interface OnDragListener {
        public void onDrag(MotionEvent motionEvent);
    }

    private OnDragListener mOnDragListener;

    public MapWrapperLayout(Context context) {
        super(context);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mOnDragListener != null) {
            mOnDragListener.onDrag(ev);
        }
        return super.dispatchTouchEvent(ev);
    }

    public void setOnDragListener(OnDragListener mOnDragListener) {
        this.mOnDragListener = mOnDragListener;
    }
}

CustomMapFragment.java

 package com.rajeshvijayakumar.map.demo.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.MapFragment;
import com.rajeshvijayakumar.map.demo.wrapper.MapWrapperLayout;

public class CustomMapFragment extends MapFragment {

    private View mOriginalView;
    private MapWrapperLayout mMapWrapperLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mOriginalView = super.onCreateView(inflater, container, savedInstanceState);

        mMapWrapperLayout = new MapWrapperLayout(getActivity());
        mMapWrapperLayout.addView(mOriginalView);

        return mMapWrapperLayout;
}

    @Override
    public View getView() {
        return mOriginalView;
    }

    public void setOnDragListener(MapWrapperLayout.OnDragListener onDragListener) {
        mMapWrapperLayout.setOnDragListener(onDragListener);
    }
}

MainActivity.java

package com.rajeshvijayakumar.map.demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.
rajeshvijayakumar.map.demo.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.model.LatLng;
import com.rajeshvijayakumar.map.demo.ui.CustomMapFragment;
import com.rajeshvijayakumar.map.demo.wrapper.MapWrapperLayout.OnDragListener;

public class MainActivity extends Activity implements OnDragListener {

    // Google Map
    private GoogleMap googleMap;
    private CustomMapFragment mCustomMapFragment;

    private View mMarkerParentView;
    private ImageView mMarkerImageView;

    private int imageParentWidth = -1;
    private int imageParentHeight = -1;
    private int imageHeight = -1;
    private int centerX = -1;
    private int centerY = -1;

    private TextView mLocationTextView;

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

        // InitializeUI
        initializeUI();

    }

    private void initializeUI() {

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
        mLocationTextView = (TextView) findViewById(R.id.location_text_view);
        mMarkerParentView = findViewById(R.id.marker_view_incl);
        mMarkerImageView = (ImageView) findViewById(R.id.marker_icon_view);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

        imageParentWidth = mMarkerParentView.getWidth();
        imageParentHeight = mMarkerParentView.getHeight();
        imageHeight = mMarkerImageView.getHeight();

        centerX = imageParentWidth / 2;
        centerY = (imageParentHeight / 2) + (imageHeight / 2);
    }

    private void initilizeMap() {
        if (googleMap == null) {
            mCustomMapFragment = ((CustomMapFragment) getFragmentManager()
                    .findFragmentById(R.id.map));
            mCustomMapFragment.setOnDragListener(MainActivity.this);
            googleMap = mCustomMapFragment.getMap();
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
        // CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng,
        // 10);
        // googleMap.animateCamera(cameraUpdate);
        // locationManager.removeUpdates(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onDrag(MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            Projection projection = (googleMap != null && googleMap
                    .getProjection() != null) ? googleMap.getProjection()
                    : null;
            //
            if (projection != null) {
                LatLng centerLatLng = projection.fromScreenLocation(new Point(
                        centerX, centerY));
                updateLocation(centerLatLng);
            }
        }
    }

    private void updateLocation(LatLng centerLatLng) {
        if (centerLatLng != null) {
            Geocoder geocoder = new Geocoder(MainActivity.this,
                    Locale.getDefault());

            List<Address> addresses = new ArrayList<Address>();
            try {
                addresses = geocoder.getFromLocation(centerLatLng.latitude,
                        centerLatLng.longitude, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (addresses != null && addresses.size() > 0) {

                String addressIndex0 = (addresses.get(0).getAddressLine(0) != null) ? addresses
                        .get(0).getAddressLine(0) : null;
                String addressIndex1 = (addresses.get(0).getAddressLine(1) != null) ? addresses
                        .get(0).getAddressLine(1) : null;
                String addressIndex2 = (addresses.get(0).getAddressLine(2) != null) ? addresses
                        .get(0).getAddressLine(2) : null;
                String addressIndex3 = (addresses.get(0).getAddressLine(3) != null) ? addresses
                        .get(0).getAddressLine(3) : null;

                String completeAddress = addressIndex0 + "," + addressIndex1;

                if (addressIndex2 != null) {
                    completeAddress += "," + addressIndex2;
                }
                if (addressIndex3 != null) {
                    completeAddress += "," + addressIndex3;
                }
                if (completeAddress != null) {
                    mLocationTextView.setText(completeAddress);
                }
            }
        }
    }
}
Expected Output :












 

Saturday, 20 June 2015

PayuMoney Integration Example in Android

PayUMoneyActivity.java


@SuppressLint("SetJavaScriptEnabled")
public class PayUMoneyActivityextends Activity {

    WebView webView;

    String merchant_key = "JBZaLc";
    String salt = "GQs7yium";
    String action1 = "";
    String base_url = "https://test.payu.in";
    // int error = 0;
    // String hashString = "";
    // Map<String, String> params;
    String txnid = "TXN8367286482920";
    String amount = "1000";
    String productInfo = "";
    String firstName = "Rajesh";
    String emailId = "rajeshmcashc10@gmail.com";

    private String SUCCESS_URL = "<Your Transaction SuccessPage Url>";
    private String FAILED_URL = "<Your Transaction FailedPage URL>";
    private String phone = "<Your Mobile No>";
    private String serviceProvider = "payu_paisa";
    private String hash = "";

    Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        webView = new WebView(this);
        setContentView(webView);

        JSONObject productInfoObj = new JSONObject();
        JSONArray productPartsArr = new JSONArray();
        JSONObject productPartsObj1 = new JSONObject();
        JSONObject paymentIdenfierParent = new JSONObject();
        JSONArray paymentIdentifiersArr = new JSONArray();
        JSONObject paymentPartsObj1 = new JSONObject();
        JSONObject paymentPartsObj2 = new JSONObject();
        try {
            // Payment Parts
            productPartsObj1.put("name", "abc");
            productPartsObj1.put("description", "abcd");
            productPartsObj1.put("value", "1000");
            productPartsObj1.put("isRequired", "true");
            productPartsObj1.put("settlementEvent", "EmailConfirmation");
            productPartsArr.put(productPartsObj1);
            productInfoObj.put("paymentParts", productPartsArr);

            // Payment Identifiers
            paymentPartsObj1.put("field", "CompletionDate");
            paymentPartsObj1.put("value", "31/10/2012");
            paymentIdentifiersArr.put(paymentPartsObj1);

            paymentPartsObj2.put("field", "TxnId");
            paymentPartsObj2.put("value", txnid);
            paymentIdentifiersArr.put(paymentPartsObj2);

            paymentIdenfierParent.put("paymentIdentifiers",
                    paymentIdentifiersArr);
            productInfoObj.put("", paymentIdenfierParent);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        productInfo = productInfoObj.toString();

        Log.e("TAG", productInfoObj.toString());
      
        Random rand = new Random();
        String rndm = Integer.toString(rand.nextInt())
                + (System.currentTimeMillis() / 1000L);
        txnid = hashCal("SHA-256", rndm).substring(0, 20);
      
        hash = hashCal("SHA-512", merchant_key + "|" + txnid + "|" + amount
                + "|" + productInfo + "|" + firstName + "|" + emailId
                + "|||||||||||" + salt);

        action1 = base_url.concat("/_payment");

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                // TODO Auto-generated method stub
                Toast.makeText(activity, "Oh no! " + description,
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onReceivedSslError(WebView view,
                    SslErrorHandler handler, SslError error) {
                // TODO Auto-generated method stub
                Toast.makeText(activity, "SslError! " + error,
                        Toast.LENGTH_SHORT).show();
                handler.proceed();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Toast.makeText(activity, "Page Started! " + url,
                        Toast.LENGTH_SHORT).show();
                if (url.equals(SUCCESS_URL)) {
                    Toast.makeText(activity, "Success! " + url,
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(activity, "Failure! " + url,
                            Toast.LENGTH_SHORT).show();
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
            //
            // @Override
            // public void onPageFinished(WebView view, String url) {
            // super.onPageFinished(view, url);
            //
            // Toast.makeText(PayMentGateWay.this, "" + url,
            // Toast.LENGTH_SHORT).show();
            // }
        });

        webView.setVisibility(View.VISIBLE);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setCacheMode(2);
        webView.getSettings().setDomStorageEnabled(true);
        webView.clearHistory();
        webView.clearCache(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setUseWideViewPort(false);
        webView.getSettings().setLoadWithOverviewMode(false);

        webView.addJavascriptInterface(new PayUJavaScriptInterface(activity),
                "PayUMoney");
        Map<String, String> mapParams = new HashMap<String, String>();
        mapParams.put("key", merchant_key);
        mapParams.put("hash", hash);
        mapParams.put("txnid", txnid);
        mapParams.put("service_provider", "payu_paisa");
        mapParams.put("amount", amount);
        mapParams.put("firstname", firstName);
        mapParams.put("email", emailId);
        mapParams.put("phone", phone);

        mapParams.put("productinfo", productInfo);
        mapParams.put("surl", SUCCESS_URL);
        mapParams.put("furl", FAILED_URL);
        mapParams.put("lastname", "Vijayakumar");

        mapParams.put("address1", "");
        mapParams.put("address2", "");
        mapParams.put("city", "");
        mapParams.put("state", "");

        mapParams.put("country", "");
        mapParams.put("zipcode", "");
        mapParams.put("udf1", "");
        mapParams.put("udf2", "");

        mapParams.put("udf3", "");
        mapParams.put("udf4", "");
        mapParams.put("udf5", "");
        // mapParams.put("pg", (empty(PayMentGateWay.this.params.get("pg"))) ?
        // ""
        // : PayMentGateWay.this.params.get("pg"));
        webview_ClientPost(webView, action1, mapParams.entrySet());

    }

    public class PayUJavaScriptInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        PayUJavaScriptInterface(Context c) {
            mContext = c;
        }

        public void success(long id, final String paymentId) {

            mHandler.post(new Runnable() {

                public void run() {
                    mHandler = null;
                    Toast.makeText(PayMentGateWay.this, "Success",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    public void webview_ClientPost(WebView webView, String url,
            Collection<Map.Entry<String, String>> postData) {
        StringBuilder sb = new StringBuilder();

        sb.append("<html><head></head>");
        sb.append("<body onload='form1.submit()'>");
        sb.append(String.format("<form id='form1' action='%s' method='%s'>",
                url, "post"));
        for (Map.Entry<String, String> item : postData) {
            sb.append(String.format(
                    "<input name='%s' type='hidden' value='%s' />",
                    item.getKey(), item.getValue()));
        }
        sb.append("</form></body></html>");
        Log.d(tag, "webview_ClientPost called");
        webView.loadData(sb.toString(), "text/html", "utf-8");
    }

    public boolean empty(String s) {
        if (s == null || s.trim().equals(""))
            return true;
        else
            return false;
    }

    public String hashCal(String type, String str) {
        byte[] hashseq = str.getBytes();
        StringBuffer hexString = new StringBuffer();
        try {
            MessageDigest algorithm = MessageDigest.getInstance(type);
            algorithm.reset();
            algorithm.update(hashseq);
            byte messageDigest[] = algorithm.digest();

            for (int i = 0; i < messageDigest.length; i++) {
                String hex = Integer.toHexString(0xFF & messageDigest[i]);
                if (hex.length() == 1)
                    hexString.append("0");
                hexString.append(hex);
            }
        } catch (NoSuchAlgorithmException nsae) {
        }
        return hexString.toString();

    }

}

Expected Output :


Source Code in Github :

Coming Soon ...