0

i have code,which one if clicked,my textview color are changed,but it only change 1 line.. i want to change all list item color enter image description here

here is my screenshoot this is my code i dont know what's wrong with this code,

PS:its not even i press the button,sometimes if i scroll listview,the color change by itself

    public void colorToggle(View view) {
    int[] attrs = {android.R.attr.popupBackground};
    TypedArray ta = obtainStyledAttributes(R.style.MyApp_PopupMenu, attrs);
    final LinearLayout propLayout = (LinearLayout) findViewById(R.id.leot);
    ListView listView = (ListView) findViewById(android.R.id.list);
    TextView textView = (TextView) findViewById(R.id.wilayah);
    switch (view.getId()) {
        case R.id.blueButton: {
            int holoBlue = getResources().getColor(R.color.holo_blue_light);
            mFab.setColor(holoBlue);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoBlue));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_blue_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoBlue));
            listView.setDividerHeight(1);
            textView.setTextColor(holoBlue);
            break;
        }
        case R.id.purpleButton: {
            int holoPurple = getResources().getColor(R.color.holo_purple);
            mFab.setColor(holoPurple);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoPurple));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_purple);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoPurple));
            listView.setDividerHeight(1);
            textView.setTextColor(holoPurple);
            break;
        }
        case R.id.greenButton: {
            int holoGreen = getResources().getColor(R.color.holo_green_light);
            mFab.setColor(holoGreen);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoGreen));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_green_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoGreen));
            listView.setDividerHeight(1);
            textView.setTextColor(holoGreen);
            break;
        }
        case R.id.orangeButton: {
            int holoOrange = getResources().getColor(R.color.holo_orange_light);
            mFab.setColor(holoOrange);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoOrange));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_orange_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoOrange));
            listView.setDividerHeight(1);
            textView.setTextColor(holoOrange);
            break;
        }
        case R.id.redButton: {
            int holoRed = getResources().getColor(R.color.holo_red_light);
            mFab.setColor(holoRed);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoRed));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_red_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoRed));
            listView.setDividerHeight(1);
            textView.setTextColor(holoRed);
            break;
        }
    }
    ta.recycle();    
}
Hafiz Ansh
  • 11
  • 5

3 Answers3

0

Try calling adapter's notifyDataSetChanged() after color change. It should redraw whole listview with new colors for every item.

arsena
  • 1,935
  • 19
  • 36
0

if i scroll ListView,the color change by itself

When you scroll, the items in ListView which are hiding will be recycled or re-used.

See this for detail explanation.

The way you are using the ListView seems to be incorrect. I recommend you using an adapter with a model and assign this adapter to the ListView.

See this for implementing ListView with custom adapter.

i want to change all list item color

Implement the custom adapter first. When color is changed, just loop through model's list and change the color of each item. And then call adapter's notifyDataSetChanged().

Hope this will help you to fix your problem.

Community
  • 1
  • 1
Faraz
  • 2,144
  • 1
  • 18
  • 28
0

Hopefully it will help you to achieve your output !

enter image description here

ThirdActivity.java (Including Adapter Class)

public class ThirdActivity extends Activity {

    View view_red, view_blue;

    ListView lst_data;

    int mySelectedColor;

    MyListAdapter myListAdapter;

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

        lst_data = (ListView) findViewById(R.id.lst_data);

        // Statically taken two views for color selection
        view_blue = findViewById(R.id.view_blue);
        view_red = findViewById(R.id.view_red);

        mySelectedColor = ContextCompat.getColor(this, android.R.color.holo_red_dark);

        myListAdapter = new MyListAdapter(this, mySelectedColor);
        lst_data.setAdapter(myListAdapter);

        view_blue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (myListAdapter == null) {

                    myListAdapter = new MyListAdapter(ThirdActivity.this, ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_blue_dark));
                    lst_data.setAdapter(myListAdapter);

                    return;
                }

                mySelectedColor = ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_blue_dark);

                myListAdapter.notifyDataSetChanged();

            }
        });

        view_red.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (myListAdapter == null) {

                    myListAdapter = new MyListAdapter(ThirdActivity.this, ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_red_dark));
                    lst_data.setAdapter(myListAdapter);

                    return;
                }

                mySelectedColor = ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_red_dark);

                myListAdapter.notifyDataSetChanged();

            }
        });


    }


    public class MyListAdapter extends BaseAdapter {


        private int selectedMyColor;
        private Context context;
        private LayoutInflater mLayoutInflater;

        public MyListAdapter(Context contetx, int mySelectedColor) {

            this.context = contetx;

            mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            this.selectedMyColor = mySelectedColor;

        }

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

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

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

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            ViewHolder holder = null;

            if (view == null) {

                //The view is not a recycled one: we have to inflate
                view = mLayoutInflater.inflate(R.layout.demo_3_row_layout, parent, false);

                holder = new ViewHolder();

                holder.txt_title = (TextView) view.findViewById(R.id.txt_title);


                view.setTag(holder);

            } else {

                holder = (ViewHolder) view.getTag();
            }

            holder.txt_title.setTextColor(mySelectedColor);

            return view;
        }


        class ViewHolder {

            TextView txt_title;

        }
    }

}
Rahul
  • 510
  • 3
  • 8