-2

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>
Ammy Dua
  • 63
  • 1
  • 1
  • 10
  • use custom Adapter with List View and hide the Visible the toggle which item you needed ....... – sushildlh Aug 25 '16 at 07:11
  • and how Can I do that? i mean how to hide item in a particular row? – Ammy Dua Aug 25 '16 at 07:22
  • Use logic in either the `bindView` or `geView` method of custom adpater. e.g. `if (position % 2 == 0) { view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewroweven)); } else { view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewrowodd)); }` is working code that alternates the background colour. – MikeT Aug 25 '16 at 07:33
  • @AmmyDua have look on the example – sushildlh Aug 25 '16 at 07:38
  • This `shophdrll.setVisibility(View.VISIBLE);` makes the **shophdrll** view visible. There are two alternatives INVISIBLE or GONE (the latter makes the display act as if the view isn't there, whilst the former only hides the display of the view). – MikeT Aug 25 '16 at 07:42

2 Answers2

0

You can use recycler view.You can do this by creating two xml for two different designs,and on basis of condition you can set view in layout inflater.Use these methods for extra views.

@Override
public int getViewTypeCount() {
    return VIEW_TYPE_COUNT;
}

@Override
public int getItemViewType(int position) {
    return position;
}

You can also refer to this link.

Community
  • 1
  • 1
shalini
  • 355
  • 4
  • 17
  • Or simply can pass a Boolean variable and handle the Switch button Visibility inside the custom adapter. – Kunu Aug 25 '16 at 07:23
0

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.

sushildlh
  • 8,986
  • 4
  • 33
  • 77
  • it worked for making layout but how can I work on these buttons? I mean `mButton.setOnClickListener` it requires a specific "ID" so how to give that? – Ammy Dua Aug 25 '16 at 08:16
  • i didn't get your question ?? – sushildlh Aug 25 '16 at 08:20
  • In settings of my app I have "Themes", "Entry Tune", "Remember History" So themes doesn't need any switch i want entry tune and remember history to be turned on or off as user wants, so how can i do that? – Ammy Dua Aug 25 '16 at 08:35
  • so you wanted to apply theme using toogle button ? – sushildlh Aug 25 '16 at 08:38
  • If entry tune is on it write "1" to sharedPrefrences and if it's off it will write "0" so how to do that? `if (entryTune.isChecked){SharedPreferences sharedPreferences = getSharedPreferences("Settings", MODE_PRIVATE); int Theme = sharedPreferences.getInt("Entry_Tune", 1);} else{SharedPreferences sharedPreferences = getSharedPreferences("Settings", MODE_PRIVATE); int Theme = sharedPreferences.getInt("Entry_Tune", 0);}` – Ammy Dua Aug 25 '16 at 08:39
  • no i want to turn entry tune on or off using switch not toggle button i mentioned that wrong – Ammy Dua Aug 25 '16 at 08:41
  • then use `onChangeListner` on the switch and use `onChangeListner` `boolean` variable to switch for your conditions . I think it help you .... – sushildlh Aug 25 '16 at 08:48
  • I added Some Code, Ya I know i should use onClickListener but how to know which switch is pressed? is it on item 2 or item 3? – Ammy Dua Aug 25 '16 at 08:49
  • please have look on answer i edit. Try on it and let me know if there is some problem ........ – sushildlh Aug 25 '16 at 08:54