Ammy Dua Ammy Dua - 3 months ago 34
Android Question

Android - Custom ListView For Only One Single Row

I Want Something Like Shown In Image Below... As Item 3, Item 4 And Item 7 Has A Toggle Switch But Item 1, Item 2, Item 5, Item 6 Doesn't Have. Can Anyone Help Me To Make This Layout And Make Toggle Switch Work Too

I Want This (Made In Photoshop)

enter image description here

My Java File

import android.content.*;
import android.view.*;
import android.widget.*;

class CustomSettingsAdapter extends ArrayAdapter<String> {

String[] settingItems = {
"Themes",
"Entry Tune",
"Remember Last Location",
"About Us",
"Exit"
};


public CustomSettingsAdapter(Context context, String[] Items) {
super(context, R.layout.main_settings_listview, Items);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.main_settings_listview, parent, false);

String itemName = getItem(position);
TextView textView =(TextView) customView.findViewById(R.id.itemName);
Switch mButton = (Switch) customView.findViewById(R.id.Switch);

if (position == 1 || position == 2) {
mButton.setVisibility(View.VISIBLE);
}

textView.setText(settingItems[position]);
return customView;
}

}


** XML **

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

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:minHeight="48dp"
android:id="@+id/mainActivityListBackground"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Number"
android:id="@+id/itemName"
android:layout_marginLeft="5dp"
android:textSize="18sp"
android:layout_centerVertical="true"
/>

<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Switch"
android:visibility="invisible"
android:layout_alignParentRight="true"
/>

</RelativeLayout>
</RelativeLayout>

Answer

Use this code it help you.

item.xml

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/code"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_weight="1"
        android:textSize="16dp" />


    <Switch
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:gravity="center"
        android:text="Off"
        android:visibility="invisible" />

</LinearLayout>

and use this adapter

public class PhoneAdapter extends BaseAdapter {
    private Context context;

    public PhoneAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return 7;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

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

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {

        convertView = View.inflate(context, R.layout.item, null);


        TextView mCode = (TextView) convertView.findViewById(R.id.code);

        Switch mButton = (Switch) convertView.findViewById(R.id.toggleButton);

        mCode.setText("item"+i+1);
        if (i == 2 || i == 3 || i == 6)
            mButton.setVisibility(View.VISIBLE);

        return convertView;
    }}

and this is output:-

o

feel free to ask if you stuck anywhere in between.

EDIT:- getView() is use for identify which button is clicked so you don't want to care about it .In the getView() the i variable is used for identify which item is clicked. Just set your OnchangeListner inside getView() and your problem solve.