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









Tuesday, 5 March 2013

Android Listview with CheckBox and a button to get the selected option.

A Listview may be single choice or Multiple Choice.

Now i am going to show you a list of Item with Checkbox, once you will select your answer by checking the checkbox and click the bottom Button it will display all the selected answer as well as Total Amount.


Screenshots:- 1) This is first screen once you run the application
























Screenshots-2) Select your answer here.
























Screenshots:3) Final 

























Now in order to achieve plz follow foloowing steps:-

Step1) res/layout/activity_main.xml

<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/lvMain"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:onClick="showResult"
        android:text="@string/get_answer" >
    </Button>

</LinearLayout>

Step2) res/layout/item.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="wrap_content"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/cbBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" >
    </CheckBox>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:orientation="vertical"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/tvDescr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text=""
            android:textSize="20sp" >
        </TextView>

        <TextView
            android:id="@+id/tvPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="" >
        </TextView>
    </LinearLayout>

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="right"
        android:contentDescription="@string/app_name" >
    </ImageView>

</LinearLayout>

Step3) src/Product.java

package com.amit.listview;

public class Product {
    String name;
      int price;
      int image;
      boolean box;
     

      Product(String _describe, int _price, int _image, boolean _box) {
        name = _describe;
        price = _price;
        image = _image;
        box = _box;
      }
    }
Step4) src/ListAdapter.java

package com.amit.listview;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {
    Context ctx;
    LayoutInflater lInflater;
    ArrayList<Product> objects;

    ListAdapter(Context context, ArrayList<Product> products) {
        ctx = context;
        objects = products;
        lInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return objects.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.item, parent, false);
        }

        Product p = getProduct(position);

        ((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
        ((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
        ((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);

        CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
        cbBuy.setOnCheckedChangeListener(myCheckChangList);
        cbBuy.setTag(position);
        cbBuy.setChecked(p.box);
        return view;
    }

    Product getProduct(int position) {
        return ((Product) getItem(position));
    }

    ArrayList<Product> getBox() {
        ArrayList<Product> box = new ArrayList<Product>();
        for (Product p : objects) {
            if (p.box)
                box.add(p);
        }
        return box;
    }

    OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            getProduct((Integer) buttonView.getTag()).box = isChecked;
        }
    };
}


Step 5) Your MainActivty.java

package com.amit.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    ArrayList<Product> products = new ArrayList<Product>();
    ListAdapter boxAdapter;

      /** Called when the activity is first created. */
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fillData();
        boxAdapter = new ListAdapter(this, products);

        ListView lvMain = (ListView) findViewById(R.id.lvMain);
        lvMain.setAdapter(boxAdapter);
      }

      void fillData() {
        for (int i = 1; i <= 20; i++) {
          products.add(new Product("Product " + i, i * 100,
              R.drawable.ic_launcher, false));
        }
      }

      public void showResult(View v) {
        String result = "Selected Product are :";
        int totalAmount=0;
        for (Product p : boxAdapter.getBox()) {
          if (p.box){
            result += "\n" + p.name;
            totalAmount+=p.price;
          }
        }
        Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show();
      }
    }

Step6) res/values/strings.xml

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

    <string name="app_name">ListViewWithButton</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
     <string name="get_answer">Click to get Selected Answer</string>

</resources>

Download full source code click ListWithButton