Android Multiple Choice ListView Using Custom Adapter.This listview you can use when you send sms to more than one friend or any notification to your friends.

So using Custom Adapeter we have to create row.xml layout which has checkbox.

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

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:layout_toLeftOf="@+id/checkbox"
        android:text="title"
        android:textColor="@android:color/black"
        android:textSize="20dp"
        android:textStyle="bold" >
    </TextView>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dip"
        android:background="@drawable/chk"
        android:button="@null"
        android:focusable="false"
        android:focusableInTouchMode="false" >
    </CheckBox>
</RelativeLayout>

 Now Using Adapter we can bind data to ListView.But we need to learn easy way to bind data with custom ListView and also hadle CheckBox Listener For Each Row.

So here i have RSS Feed to get data from web and then i add it in List<NameBean>.NameBean is bean class which contains value of Rss Feed Element and we have to maintain one boolean for checkbox state either checked or not.And generate Getters And Setters.

NameBean.java

public class NameBean {

 private String name;
 private boolean selected;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public boolean isSelected() {
 return selected;
 }

 public void setSelected(boolean selected) {
 this.selected = selected;
 }

}

Now when you get data through XML Parsing you have to create Object of NameBean and you can set data and add Object to List.

for (int temp = 0; temp < nList.getLength(); temp++) {

 Node nNode = nList.item(temp);

                  if (nNode.getNodeType() == Node.ELEMENT_NODE) {

    Element eElement = (Element) nNode;

    NameBean objItem = new NameBean();

    objItem.setName(getTagValue("name", eElement));
                 
    list.add(objItem);

 }
 }


Now list  is fill up with NameBean Objects.And you can retrive data in Adapter so when you set ListView you just retrieve data in getView(…) method of Adapter.

In getView(…) method put checkBox listener to listen checkbox is check or not, if its checked then set boolean true for that object of NameBean And if its not checked then setSelected false to Namebean object.

So when you start this example in listview and its get data from webservice and bind data to listview using NameAdapter Class.

NameAdapter.java



import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class NamesAdapter extends ArrayAdapter<NameBean> {

 private List<NameBean> list;
 private LayoutInflater inflator;

 public NamesAdapter(Activity context, List<NameBean> list) {
 super(context, R.layout.row, list);
 this.list = list;
 inflator = context.getLayoutInflater();
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {

 ViewHolder holder = null;
 if (convertView == null) {
 convertView = inflator.inflate(R.layout.row, null);
 holder = new ViewHolder();
 holder.title = (TextView) convertView.findViewById(R.id.title);
 holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
 holder.chk
 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

 @Override
 public void onCheckedChanged(CompoundButton view,
 boolean isChecked) {
 int getPosition = (Integer) view.getTag();
 list.get(getPosition).setSelected(view.isChecked());

 }
 });
 convertView.setTag(holder);
 convertView.setTag(R.id.title, holder.title);
 convertView.setTag(R.id.checkbox, holder.chk);
 } else {
 holder = (ViewHolder) convertView.getTag();
 }
 holder.chk.setTag(position);

 holder.title.setText(list.get(position).getName());
 holder.chk.setChecked(list.get(position).isSelected());

 return convertView;
 }

 static class ViewHolder {
 protected TextView title;
 protected CheckBox chk;
 }
}

When your listView is bind with data you can see checkbox in row with name and now you can get Selected name using NameBean Object and you can retrieve it from list.

                // Retrive Data from list Using for-each loop
                StringBuffer sb = new StringBuffer();
 for (NameBean bean : items) {

 if (bean.isSelected()) {
 sb.append(bean.getName());
 sb.append(",");
 }
 }

Now you get All Selected name from ListView.So its so easy to write Multiple Choice ListView in Android.

You Can Download Full Source Code

Android View Pager is Layout Manager you can change it Left and Right.And you can see more than one view using swapping.And its works like image galllery.

First insert ViewPager in your xml  layout file ::

 <android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/button1"
android:layout_margin="5dp" />

Now View Pager is working as a horizontal ScrollView.And View Pager have rows for each view.So create row for each view in layout,

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

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="desc" />

<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />

</FrameLayout> 

Now at Java Coding side create object of ViewPager and give References from XML file,And ViewPager set  Adapter for pager.

ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ImagePagerAdapter(imageUrls));

Now extends PagerAdapter Class For  View Pager’s Adapter.Now we have override method which is inflate each row in pager and create view for Pager

@Override
public Object instantiateItem(View view, int position) {
final View imageLayout = inflater.inflate(
R.layout.item_pager_image, null);
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout
.findViewById(R.id.loading);
}

instantiateItem Method is working as like as getView method in listView.

Now View Pager has PageChangeListener to listen page changing.You get current page using getCurrentPage from DetailOnPageChangeListener class.

pager.setOnPageChangeListener(new DetailOnPageChangeListener());

public class DetailOnPageChangeListener extends
ViewPager.SimpleOnPageChangeListener {

private int currentPage;

@Override
public void onPageSelected(int position) {

currentPage = position;
}

public int getCurrentPage() {
return currentPage;
}
    }

you get Current page using button click.So when you click on button get current page From Pager Listener. also and you can download below full source code:

Download Android View Pager

Android provides default UI Button for button view.But sometimes we need better UI for application and new colorful button rather than default.so we need to create nice UI for button which looks awesome with application design.

First you add button in your xml layout file.Then create xml in drawable folder for button background.

in xml drawable file add selector for button.

then add item tag for button state.which has different state like state_pressed,state_focused,state_enabled.

So here we used only state_pressed and check its true or false,then we add other property for both state

now  create two <item ></item> tag and put condition state_pressed or not.

After we can set corner radius for button and when we set radius its looks like rounded box.

Now we have <solid> tag for button inner color.so you can set color for both state.

Using <padding> tab you can put inner spacing.and its considering space from corner of button,

And the last tag is <stroke> which is used for rounded colorful border.and you can set thickness of border using width and its have color also for colorful border.

Now just set your button background in xml layout flie.

android:background="@drawable/btn"

So using below code you can create custom and if you want to set other color select your hex string and past in android:color in solid tag.

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

<item android:state_pressed="true"><shape android:shape="rectangle">
 <corners android:radius="10dp" />

<solid android:color="#0c76ab" />

<padding android:bottom="10.0dip" android:left="10.0dip" android:right="10.0dip" android:top="10.0dip" />

<stroke android:width="1.5dip" android:color="#222" />
 </shape></item>
 <item android:state_pressed="false"><shape android:shape="rectangle">
 <corners android:radius="10dp" />

<solid android:color="#d9d9d9" />

<padding android:bottom="10.0dip" android:left="10.0dip" android:right="10.0dip" android:top="10.0dip" />

<stroke android:width="1.5dip" android:color="#222" />
 </shape></item>

</selector>

Download Full Source Code 

1) Create New Facebook App:

https://developers.facebook.com/apps/

2) Add Facebook Id in AndoridManifest file:

if facebook app id is 123456 then put it as below, change it as per your app id.

<application>

 <meta-data
 android:name="com.facebook.sdk.ApplicationId"
 android:value="123456" />

<provider
 android:name="com.facebook.FacebookContentProvider"
 android:authorities="com.facebook.app.FacebookContentProvider123456"
 android:exported="true" />

 </application>

3)  Java Code (Get full code from here: https://goo.gl/ALHTzF)

public class MyActivity extends AppCompatActivity  {

CallbackManager callbackManager;
 Button btnLogin;

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

initUI();
 }

private void initUI() {
 callbackManager = CallbackManager.Factory.create();

LoginManager.getInstance().registerCallback(callbackManager,
 new FacebookCallback<LoginResult>() {
 @Override
 public void onSuccess(LoginResult loginResult) {
 Log.d("Success", "Login");

if (loginResult.getAccessToken() != null) {
 String strAccessToken = loginResult.getAccessToken().getToken();

Log.d("Token is", strAccessToken);
 getProfile(loginResult.getAccessToken());

}
 }

@Override
 public void onCancel() {
 showToast("Login Cancel");
 }

@Override
 public void onError(FacebookException exception) {
 showToast(exception.getMessage());
 }
 });

btnLogin = (Button) findViewById(R.id.btnlogin);
 btnLogin.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 LoginManager.getInstance().logInWithReadPermissions(MyActivity.this, Arrays.asList("public_profile", "user_friends"));
 }
 });
 }

public void getProfile(AccessToken accessToken) {

GraphRequest request = GraphRequest.newMeRequest(
 accessToken,
 new GraphRequest.GraphJSONObjectCallback() {
 @Override
 public void onCompleted(
 JSONObject object,
 GraphResponse response) {

//retrive email and name from here...
 Log.d("JSON OBJECT OF PROFILE", object.toString());
 Log.v("GraphResponse : ", response.toString());

}
 });
 Bundle parameters = new Bundle();
 parameters.putString("fields", "id,name,email,gender, birthday");
 request.setParameters(parameters);
 request.executeAsync();
 }

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if (callbackManager.onActivityResult(requestCode, resultCode, data)) {
 return;
 }
 }
 } 

4) You can use it in Fragment too.

btnLogin.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 LoginManager.getInstance().logInWithReadPermissions(MyFragment.this, Arrays.asList("public_profile", "user_friends"));
 }
 });