Wednesday, July 27, 2011

HorizontalScroll

Horizontal Scroll:-
Here is the implementation of Swipe and ScrollView working together: 

 HoriScroll.java

 package com.HoriScroll;

import android.app.Activity;
import android.os.Bundle;

public class HoriScroll extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        FlingAndScrollViewer flingAndScrollViewer = (FlingAndScrollViewer) findViewById(R.id.flingScrollViewer);
        flingAndScrollViewer.setInitialPosition(0);
    }
}


FlingAndScrollViewer.java
package com.HoriScroll;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;

public class FlingAndScrollViewer extends ViewGroup {
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
 private int mScrollX = 0;
private int mCurrentScreen = 0;
 private float mLastMotionX;
 private static final String LOG_TAG = "FlingAndScrollViewer";
 private static final int SNAP_VELOCITY = 1000;
 private final static int TOUCH_STATE_REST = 0;
private final static int TOUCH_STATE_SCROLLING = 1;
 private int mTouchState = TOUCH_STATE_REST;
 private int mTouchSlop = 0;
 public FlingAndScrollViewer(Context context) {
    super(context);
    mScroller = new Scroller(context);
     mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
     this.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.FILL_PARENT));
}
 public FlingAndScrollViewer(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = getContext().obtainStyledAttributes(attrs,
            R.styleable.FlingAndScrollViewer);
    mCurrentScreen = a.getInteger(
            R.styleable.FlingAndScrollViewer_default_screen, 0);
     mScroller = new Scroller(context);
     mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
     this.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.FILL_PARENT));
 }
 public void setInitialPosition(int initialPosition){
    mCurrentScreen = initialPosition;
}
 @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onTouchEvent will be called and we do the actual
     * scrolling there.
     */
     /*
     * Shortcut the most recurring case: the user is in the dragging state
     * and he is moving his finger. We want to intercept this motion.
     */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
        return true;
    }
     final float x = ev.getX();
     switch (action) {
    case MotionEvent.ACTION_MOVE:
        /*
         * mIsBeingDragged == false, otherwise the shortcut would have
         * caught it. Check whether the user has moved far enough from his
         * original down touch.
         */
         /*
         * Locally do absolute value. mLastMotionX is set to the y value of
         * the down event.
         */
        final int xDiff = (int) Math.abs(x - mLastMotionX);
         boolean xMoved = xDiff > mTouchSlop;
         if (xMoved) {
            // Scroll if the user moved far enough along the X axis
            mTouchState = TOUCH_STATE_SCROLLING;
        }
        break;
     case MotionEvent.ACTION_DOWN:
        // Remember location of down touch
        mLastMotionX = x;
         /*
         * If being flinged and user touches the screen, initiate drag;
         * otherwise don't. mScroller.isFinished should be false when being
         * flinged.
         */
        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
                : TOUCH_STATE_SCROLLING;
        break;
     case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        // Release the drag
        mTouchState = TOUCH_STATE_REST;
        break;
    }
     /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    return mTouchState != TOUCH_STATE_REST;
}
 @Override
public boolean onTouchEvent(MotionEvent event) {
     if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
     final int action = event.getAction();
    final float x = event.getX();
     switch (action) {
    case MotionEvent.ACTION_DOWN:
        Log.i(LOG_TAG, "event : down");
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
         // Remember where the motion event started
        mLastMotionX = x;
        break;
    case MotionEvent.ACTION_MOVE:
        // Log.i(LOG_TAG,"event : move");
        // if (mTouchState == TOUCH_STATE_SCROLLING) {
        // Scroll to follow the motion event
        final int deltaX = (int) (mLastMotionX - x);
        mLastMotionX = x;
         // Log.i(LOG_TAG, "event : move, deltaX " + deltaX + ", mScrollX " +
        // mScrollX);
         if (deltaX < 0) {
            if (mScrollX > 0) {
                scrollBy(Math.max(-mScrollX, deltaX), 0);
            }
        } else if (deltaX > 0) {
            final int availableToScroll = getChildAt(getChildCount() - 1)
                    .getRight() - mScrollX - getWidth();
            if (availableToScroll > 0) {
                scrollBy(Math.min(availableToScroll, deltaX), 0);
            }
        }
        // }
        break;
    case MotionEvent.ACTION_UP:
        Log.i(LOG_TAG, "event : up");
        // if (mTouchState == TOUCH_STATE_SCROLLING) {
        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000);
        int velocityX = (int) velocityTracker.getXVelocity();
         if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
            // Fling hard enough to move left
            snapToScreen(mCurrentScreen - 1);
        } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
            // Fling hard enough to move right
            snapToScreen(mCurrentScreen + 1);
        } else {
            snapToDestination();
        }
         if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        // }
        mTouchState = TOUCH_STATE_REST;
        break;
    case MotionEvent.ACTION_CANCEL:
        Log.i(LOG_TAG, "event : cancel");
        mTouchState = TOUCH_STATE_REST;
    }
    mScrollX = this.getScrollX();
     return true;
}
 private void snapToDestination() {
    final int screenWidth = getWidth();
    final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth;
    Log.i(LOG_TAG, "from des");
    snapToScreen(whichScreen);
}
 public void snapToScreen(int whichScreen) {
    Log.i(LOG_TAG, "snap To Screen " + whichScreen);
    mCurrentScreen = whichScreen;
    final int newX = whichScreen * getWidth();
    final int delta = newX - mScrollX;
    mScroller.startScroll(mScrollX, 0, delta, 0, Math.abs(delta) * 2);
    invalidate();
}
 public void setToScreen(int whichScreen) {
    Log.i(LOG_TAG, "set To Screen " + whichScreen);
    mCurrentScreen = whichScreen;
    final int newX = whichScreen * getWidth();
    mScroller.startScroll(newX, 0, 0, 0, 10);
    invalidate();
}
 @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int childLeft = 0;
     final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != View.GONE) {
            final int childWidth = child.getMeasuredWidth();
            child.layout(childLeft, 0, childLeft + childWidth,
                    child.getMeasuredHeight());
            childLeft += childWidth;
        }
    }
 }
 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     final int width = MeasureSpec.getSize(widthMeasureSpec);
    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    if (widthMode != MeasureSpec.EXACTLY) {
        throw new IllegalStateException("error mode.");
    }
     final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    if (heightMode != MeasureSpec.EXACTLY) {
        throw new IllegalStateException("error mode.");
    }
     // The children are given the same width and height as the workspace
    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
    }
    Log.i(LOG_TAG, "moving to screen " + mCurrentScreen);
    scrollTo(mCurrentScreen * width, 0);
}
 @Override
public void computeScroll() {
    if (mScroller.computeScrollOffset()) {
        mScrollX = mScroller.getCurrX();
        scrollTo(mScrollX, 0);
        postInvalidate();
    }
}
 /**
 * Return the parceable instance to be saved
 */
@Override
protected Parcelable onSaveInstanceState() {
    final SavedState state = new SavedState(super.onSaveInstanceState());
    state.currentScreen = mCurrentScreen;
    return state;
}
 /**
 * Restore the previous saved current screen
 */
@Override
protected void onRestoreInstanceState(Parcelable state) {
    SavedState savedState = (SavedState) state;
    super.onRestoreInstanceState(savedState.getSuperState());
    if (savedState.currentScreen != -1) {
        mCurrentScreen = savedState.currentScreen;
    }
}
 // ========================= INNER CLASSES ==============================
 public interface onViewChangedEvent {
    void onViewChange(int currentViewIndex);
}
 /**
 * A SavedState which save and load the current screen
 */
public static class SavedState extends BaseSavedState {
    int currentScreen = -1;
     /**
     * Internal constructor
     *
     * @param superState
     */
    SavedState(Parcelable superState) {
        super(superState);
    }
     /**
     * Private constructor
     *
     * @param in
     */
    private SavedState(Parcel in) {
        super(in);
        currentScreen = in.readInt();
    }
     /**
     * Save the current screen
     */
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeInt(currentScreen);
    }
     /**
     * Return a Parcelable creator
     */
    public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
        public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
        }
         public SavedState[] newArray(int size) {
            return new SavedState[size];
        }
    };
}
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello"
        android:gravity="center" />
 <com.HoriScroll.FlingAndScrollViewer
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:id="@+id/flingScrollViewer">
        <ScrollView android:id="@+id/scrollView1"
            android:background="#FF8000" android:layout_height="fill_parent"
            android:layout_width="fill_parent">
            <LinearLayout android:id="@+id/linearLayout1"
                android:layout_height="fill_parent" android:layout_width="fill_parent"
                android:orientation="vertical">
                <TextView android:id="@+id/TextView01" android:text="1"
                    android:textColor="#ffffff" android:layout_width="fill_parent"
                    android:layout_weight="1" android:gravity="center"
                    android:layout_height="wrap_content" android:textSize="500dip"></TextView>
            </LinearLayout>
        </ScrollView>
     <ScrollView android:id="@+id/scrollView2"
            android:background="#0080FF" android:layout_height="fill_parent"
            android:layout_width="fill_parent">
            <LinearLayout android:id="@+id/linearLayout2"
                android:layout_height="fill_parent" android:layout_width="fill_parent"
                android:orientation="vertical">
                <TextView android:id="@+id/TextView02" android:text="2"
                    android:textColor="#ffffff" android:layout_width="fill_parent"
                    android:layout_weight="1" android:gravity="center"
                    android:layout_height="wrap_content" android:textSize="500dip"></TextView>
            </LinearLayout>
        </ScrollView>
     <ScrollView android:id="@+id/scrollView3"
            android:background="#00FF00" android:layout_height="fill_parent"
            android:layout_width="fill_parent">
            <LinearLayout android:id="@+id/linearLayout3"
                android:layout_height="fill_parent" android:layout_width="fill_parent"
                android:orientation="vertical">
                <TextView android:id="@+id/TextView03" android:text="3"
                    android:textColor="#ffffff" android:layout_width="fill_parent"
                    android:layout_weight="1" android:gravity="center"
                    android:layout_height="wrap_content" android:textSize="500dip"></TextView>
            </LinearLayout>
        </ScrollView>
 </com.HoriScroll.FlingAndScrollViewer>
</LinearLayout>

attrs.xml ( res/value)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FlingAndScrollViewer">
        <attr name="default_screen" format="integer"/>
    </declare-styleable>
</resources>




Click Here to Download HoriScroll Project .


Wednesday, April 20, 2011

Fields

In java file
Fields.java




package Fields.app;


import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;


public class Field_Act extends Activity implements TextWatcher {
TextView selection;
AutoCompleteTextView edit;
String[] items = { "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "thirteen", "fouteen", "fifteen",
"sixteen", "seventeen", "eighteen", "ninteen", "twenty",
"twenty one", "twenty two", "twenty three", "twenty four",
"twenty five", "twenty six", "twenty seven", "twenty eight",
"twenty nine", "thirty"};


@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

selection = (TextView) findViewById(R.id.selection);
edit = (AutoCompleteTextView) findViewById(R.id.edit);
edit.addTextChangedListener(this);
edit.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, items));
}


public void onTextChanged(CharSequence s, int start, int before, int count) {
selection.setText(edit.getText());
}


public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// needed for interface, but not used
}


public void afterTextChanged(Editable s) {
// needed for interface, but not used
}
}


In xml file
main.xml




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<AutoCompleteTextView 
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"/>

</LinearLayout>


Source Code







Grid View

In java file
GirdView_Act.java




package GirdView.app;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;


public class GirdView_Act extends Activity implements
AdapterView.OnItemSelectedListener {
TextView selection;
String[] items = { "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "thirteen", "fouteen", "fifteen",
"sixteen", "seventeen", "eighteen", "ninteen", "twenty",
"twenty one", "twenty two", "twenty three", "twenty four",
"twenty five", "twenty six", "twenty seven", "twenty eight",
"twenty nine", "thirty", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven", "thirteen",
"fouteen", "fifteen", "sixteen", "seventeen", "eighteen",
"ninteen", "twenty", "twenty one", "twenty two", "twenty three",
"twenty four", "twenty five", "twenty six", "twenty seven",
"twenty eight", "twenty nine", "thirty", };


@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection = (TextView) findViewById(R.id.selection);
GridView g = (GridView) findViewById(R.id.grid);
g.setAdapter(new FunnyLookingAdapter(this,
android.R.layout.simple_list_item_1, items));
g.setOnItemSelectedListener(this);
}


public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
selection.setText(items[position]);
}


public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}


private class FunnyLookingAdapter extends ArrayAdapter {
Context ctxt;


@SuppressWarnings("unchecked")
FunnyLookingAdapter(Context ctxt, int resource, String[] items) {
super(ctxt, resource, items);
this.ctxt = ctxt;
}


public View getView(final int position, View convertView,
ViewGroup parent) {
TextView label = (TextView) convertView;
if (convertView == null) {
convertView = new TextView(ctxt);
label = (TextView) convertView;
}
label.setText(items[position]);
return (convertView);
}
}
}

In xml files
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="35px"
android:horizontalSpacing="5px"
android:numColumns="auto_fit"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>



Spinner

In java file
Spinner.java




package Spinner.app;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;


public class Spinner_Act extends Activity implements AdapterView.OnItemSelectedListener {
    /** Called when the activity is first created. */
   
    TextView selection;
    String[] items={"one", "two", "three", "four", "five",
    "six", "seven", "eight", "nine", "ten",
    "eleven", "thirteen", "fouteen", "fifteen"};
    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    selection=(TextView)findViewById(R.id.selection);
    Spinner spin=(Spinner)findViewById(R.id.spinner);
    spin.setOnItemSelectedListener(this);
    ArrayAdapter<String> aa=new ArrayAdapter<String>(this,
     android.R.layout.simple_spinner_item,
     items);
     aa.setDropDownViewResource(
     android.R.layout.simple_spinner_dropdown_item);
     spin.setAdapter(aa);
     }
    public void onItemSelected(AdapterView<?> parent,
     View v, int position, long id) {
     selection.setText(items[position]);
     }
    public void onNothingSelected(AdapterView<?> parent) {
     selection.setText("");
     }
     }


In xml file
main.xml




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Spinner 
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
/>
</LinearLayout>


Source Code







Tuesday, April 19, 2011

List View ( using EfficientAdapter + bundle)

In First java file
my_listview.java


package ListView.app;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class my_listview extends Activity {
ListView itemList = null;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview2);
// TODO Auto-generated method stub
itemList = (ListView) findViewById(R.id.itemList);
itemList.setAdapter(new EfficientAdapter(this));
}


String[] Qitems = {
"Q1:What are the advantages of Object Oriented Programming Languages (OOPL)?",
"Q2:How do you express an ‘is a’ relationship and a ‘has a’ relationship or explain inheritance and composition? What is the difference between composition and aggregation?",
"Q3:What do you mean by polymorphism, inheritance, encapsulation, and dynamic binding?",
"Q4:What is the difference between an abstract class and an interface and when should you use them?",
"Q5:How would you communicate between applets and servlets?",
"Q6:Why use LDAP when you can do the same with relational database (RDBMS)?",
"Q7:Explain the RMI architecture?",
"Q8:What is a remote object? Why should we extend UnicastRemoteObject?",
"Q9:How will you pass parameters in RMI?",
"Q10:What is the difference between final, finally and finalize() in Java?",
"Q11:Why use LDAP when you can do the same with relational database (RDBMS)?",
"Q12:Explain the RMI architecture?",
"Q13:What is a remote object? Why should we extend UnicastRemoteObject?",
"Q14:How will you pass parameters in RMI?",
"Q15:What is the difference between final, finally and finalize() in Java?"};

String[] Aitems = {  "The Object Oriented Programming Languages directly represent the real life objects like Car,",
"The ‘is a’ relationship is expressed with inheritance and ‘has a’ relationship is expressed with ",
"Polymorphism – means the ability of a single variable of a given type to be used to reference objects",
"In design, you want the base class to present only an interface for its derived classes. This means, ",
"We can use the java.net.URLConnection and java.net.URL classes to open a standard HTTP connection and",
"Java Remote Method Invocation (RMI) provides a way for a Java program on one machine to communicate ",
"A remote object is one whose methods can be invoked from another JVM (i.e. another process). A remote",
"The client process initiates the invocation of the remote method by calling the method on the stub.",
"final - constant declaration. Refer Q31 in Java section. finally - handles exception. The finally block ",
"Java Remote Method Invocation (RMI) provides a way for a Java program on one machine to communicate with",
"A remote object is one whose methods can be invoked from another JVM (i.e. another process). A remote",
"The client process initiates the invocation of the remote method by calling the method on the stub.",
"final - constant declaration. Refer Q31 in Java section. finally - handles exception. The finally block",
"Java Remote Method Invocation (RMI) provides a way for a Java program on one machine to communicate",
"In design, you want the base class to present only an interface for its derived classes. "};

public class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public Context ctxt;


public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.ctxt = context;
}
public int getCount() {
return Qitems.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;

if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_listview, null);
holder = new ViewHolder();
holder.titletxt = (TextView) convertView.findViewById(R.id.titletxt);
holder.ratingtxt = (TextView) convertView.findViewById(R.id.ratingtxt);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ratingtxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(my_listview.this,page2.class);
i.putExtra("Q",Qitems[position]);
i.putExtra("A",Aitems[position]);
startActivity(i);
}
});

holder.titletxt.setText(Qitems[position]);
holder.ratingtxt.setText("Answer");
return convertView;
}


class ViewHolder {
TextView titletxt;
TextView ratingtxt;
}
}
}

In Second java file
page2.java

package ListView.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class page2 extends Activity {
TextView tvQ = null;
TextView tvA = null;
Button btnback = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
Bundle bunble = getIntent().getExtras();
tvQ = (TextView)findViewById(R.id.TxtViewQuestion);
tvA = (TextView)findViewById(R.id.TxtViewAnswer);
btnback = (Button)findViewById(R.id.back);
tvQ.setText(bunble.getString("Q"));
tvA.setText(bunble.getString("A"));
btnback.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onBackPressed();
}
});
}

}

In first xml file
custom_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="@+id/titletxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textColor="#ffffff"
android:textSize="18sp"
android:text="fasdsadsad"
android:textStyle="bold">
</TextView>
<TextView
android:id="@+id/ratingtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:textColor="#ffffff"
android:text="123"
android:layout_below="@+id/titletxt"
android:textSize="10sp">
</TextView>
</RelativeLayout>
</LinearLayout>

In second xml file
listview2.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/ImageView1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="@drawable/icon">
</ImageView>
<ListView
android:layout_below="@+id/ImageView1"
android:id="@+id/itemList"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:background="#20000000">
</ListView>
</RelativeLayout>

In third xml file
page2.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:background="#FFFFFF"
  android:orientation="vertical"
  >
   <Button 
   android:text="Back" 
   android:id="@+id/back" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content"
   >
   </Button>
   <TextView
   android:id="@+id/TxtViewQuestion"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:textColor="#000000"
   android:text=""
   />
   <TextView
   android:id="@+id/TxtViewAnswer"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:textColor="#0000A0"
   android:text=""
   />
</LinearLayout>


Note :- Question and Answers in String Qitem and Aitems are just for example.