Wednesday, 18 September 2013

Android Custom Multiple Choice ListView(Custom CheckBox)

In this i ll share how to create a Custom MultipleChoice ListView.























































Download full source code click CustomCheckBox
Later i will explain full source code.
Happy Coding..
Fell free to ask any question. or you can directly email me at amithigh.gupta@gmail.com

Tuesday, 28 May 2013

Android Section ListView























In this blog i am going to show you a very good custom ListView called Section ListView.

Download Complete Source code from SectionList.

I will update the full documentation later.

fell free to ask any query.
Email-d: amithigh.gupta@gmail.com

Thanks a lot.

Sunday, 10 March 2013

Android ListView with RadionGroup and TextView

In previous blog  i shown you ListView with RadioGroup, CheckBox.
Now i will use RadioGroup with TextView.
























Download full source code Click ListWithRadioGroupandTextView

Please leave your feedback to amithigh.gupta@gmail.com

So please fell free to ask any doubt, any query will be highly appreciated.
Thanks,

Thursday, 7 March 2013

Android Custon Single choice LsitView with Custom RadioButton



In Previous blog i posted Some ListView example related to TextView with Image, Slide Animation.
Now in this post i will use custom RadioButton i.e Single Choice ListView.


























Download Full Source code Click CustomSingleChoiceList

Later i will update full documentation on this.

Hope this post will really help you.
Please post your comment and ask any question.
I will be always for quick reply..
Thanks

Wednesday, 6 March 2013

Android Alert Dialog With Single choice list(Like Spinner)

We can use AlertDialog with not only "Yes" or "No" but also can represent like Spinner.

Screenshots:-























Screenshots:- After clicking Button a Alert Dialog will appear with Single choice list.























Screenshots:- Alert Dialog with single choice list and EditText
























Download Full Source code click AlertDialogLikeSpinner


Android ListView with Animation.

ListView is a very useful widget. Through animation we can show a ListView as ExpandibleListView.

Screenshots:- When you will click any list item item i view will open like sliding. if you click other list item then it will close the previous one and open the current item.
So it will behave like ExpandibleList.


Download Full source code click ListViewWithAnimation























Screenshots:2)























Steps are as follows:-

Step1) ExpandListItem.java


package com.list.animation;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout.LayoutParams;

public class ExpandListItem extends Animation {
    private View mAnimatedView;
    private LayoutParams mViewLayoutParams;
    private int mMarginStart, mMarginEnd;
    private boolean mIsVisibleAfter = false;
    private boolean mWasEndedAlready = false;

    /**
     * Initialize the animation
     * @param view The layout we want to animate
     * @param duration The duration of the animation, in ms
     */
    public ExpandListItem(View view, int duration) {

        setDuration(duration);
        mAnimatedView = view;
        mViewLayoutParams = (LayoutParams) view.getLayoutParams();

        // decide to show or hide the view
        mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);

        mMarginStart = mViewLayoutParams.bottomMargin;
        mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);

        view.setVisibility(View.VISIBLE);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);

        if (interpolatedTime < 1.0f) {

            // Calculating the new bottom margin, and setting it
            mViewLayoutParams.bottomMargin = mMarginStart
                    + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);

            // Invalidating the layout, making us seeing the changes we made
            mAnimatedView.requestLayout();

        // Making sure we didn't run the ending before (it happens!)
        } else if (!mWasEndedAlready) {
            mViewLayoutParams.bottomMargin = mMarginEnd;
            mAnimatedView.requestLayout();

            if (mIsVisibleAfter) {
                mAnimatedView.setVisibility(View.GONE);
            }
            mWasEndedAlready = true;
        }
    }
}


Step2) MainActivity .java

package com.list.animation;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity implements OnItemClickListener{

private List<String> data;
    ListAdapter adapter;
    int selected = -1;
    boolean firsttime = false;
    View bottomview1 = null, bottomview2 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        data = new ArrayList<String>();
        fillData();
        adapter = new ListAdapter(this, data);
        ListView lvMain = (ListView) findViewById(R.id.list);
        lvMain.setAdapter(adapter);
        lvMain.setOnItemClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    void fillData() {
        for (int i = 1; i <= 5; i++) {
            data.add("Heading" + i);
        }
    }

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
if (position == selected) {
if (firsttime) {
ExpandListItem expandAnim = new ExpandListItem(
bottomview2, 1000);
bottomview2.startAnimation(expandAnim);
} else {
ExpandListItem expandAni = new ExpandListItem(
bottomview1, 1000);
bottomview1.startAnimation(expandAni);
}
selected = -1;

} else if (selected >= 0) {

ExpandListItem expandAni = new ExpandListItem(bottomview1,
1000);
bottomview1.startAnimation(expandAni);
bottomview2 = view.findViewById(R.id.hidden);
ExpandListItem expandAnim = new ExpandListItem(bottomview2,
1000);
bottomview2.startAnimation(expandAnim);
selected = position;
bottomview1 = bottomview2;
firsttime = true;
}else
{
bottomview1 = view.findViewById(R.id.hidden);
ExpandListItem expandAni = new ExpandListItem(bottomview1,
1000);
bottomview1.startAnimation(expandAni);
selected = position;
}
}

}

Step3) layout/main.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" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

Step4) list_item.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:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:padding="20dip" />

    <LinearLayout
        android:id="@+id/hidden"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:layout_marginBottom="-50dip"
        android:visibility="gone" >

        <Button
            android:id="@+id/doSomething1"
            android:layout_width="wrap_content"
            android:layout_height="50dip"
            android:focusable="false"
            android:layout_marginLeft="50dp"
            android:focusableInTouchMode="false"
            android:text="@string/menu_item1" />

        <Button
            android:id="@+id/doSomething2"
            android:layout_width="wrap_content"
            android:layout_height="50dip"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="@string/menu_item2" />

    </LinearLayout>

</LinearLayout>


Step5) ListAdapter .java


package com.list.animation;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {
Context ctx;
    LayoutInflater lInflater;
    List<String> data;

    ListAdapter(Context context, List<String> data) {
        ctx = context;
        this.data = data;
        lInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.list_item, parent, false);
        }
        ((TextView) view.findViewById(R.id.title)).setText(data.get(position));
       
        return view;
    }
}

Thanks,
feel free to ask any thing and post your comments.


Android ListView with Alternate list item background and hover.

Now i will show how to make a ListView with Alternate color i.e for odd position having different background and for even position of  List item having different background as well as different hover color.

Screenshots:-
























These are the following steps to do show.

Step1.1) Use two selector for odd and even postion list item

artists_list_backgroundcolor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item
 android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@color/grey" />
<item android:state_pressed="true"
    android:drawable="@color/itemselected" />
<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@color/itemselected" />
</selector>


Step 1.2)
artists_list_background_alternate.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item
 android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@color/sign_out_color" />
<item android:state_pressed="true"
    android:drawable="@color/login_hover" />
<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@color/login_hover" />
</selector>

Step2)
colors.xml

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

    <color name="survey_toplist_item">#EFEDEC</color>
    <color name="survey_alternate_color">#EBE7E6</color>
    <color name="grey">#ffffff</color>
    <color name="itemselected">#EDEDED</color>
    <color name="login_hover">#E5F5FA</color>
    <color name="sign_out_color">#e84040</color>

</resources>

Step 3.1)
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"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp" />

</RelativeLayout>

Step3.2) res/layout/listitem,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" >

    <TextView
        android:id="@+id/heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:padding="15dp" />

</LinearLayout>

Step4)
MainActivity.java

package com.amit.listalternate;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {
    private List<String> data;
    ListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        data = new ArrayList<String>();
        fillData();
        adapter = new ListAdapter(this, data);
        ListView lvMain = (ListView) findViewById(R.id.list);
        lvMain.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    void fillData() {
        for (int i = 1; i <= 20; i++) {
            data.add("Heading" + i);
        }
    }

}

Step 5) Adapter
ListAdapter.java



package com.amit.listalternate;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter{
    Context ctx;
    LayoutInflater lInflater;
    List<String> data;

    ListAdapter(Context context, List<String> data) {
        ctx = context;
        this.data = data;
        lInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.listitem, parent, false);
        }

        if (position % 2 == 0) {
            view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
        } else {
            view.setBackgroundResource(R.drawable.artists_list_background_alternate);
        }

        ((TextView) view.findViewById(R.id.heading)).setText(data.get(position));
       
        return view;
    }
}

Download full source code click ListViewWithAlternateColor