Home > Mobile >  How do I change the color texts in a listview android studio using JAVA?
How do I change the color texts in a listview android studio using JAVA?

Time:12-03

I need using JAVA, change colors of all textviews from LISTVIEW.

MainActivity.java:

public class MainActivity extends AppCompatActivity {

String[] programName = {"ex1", "ex2", "ex3"}

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvProgram = findViewById(R.id.listView);


ProgramAdapter programAdapter = new ProgramAdapter(this, programName);
lvProgram.setAdapter(programAdapter);

}}


ProgramAdapter.java:

public class ProgramAdapter extends ArrayAdapter<String> {
        Context context;
        String[] programName;

        public ProgramAdapter(Context context, String[] programName) {
            super(context, R.layout.single_item2, R.id.titulo, programName);
            this.context = context;
            this.programName = programName;
        }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View singleItem = convertView;
        ProgramViewHolder holder = null;
        if(singleItem == null){
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            singleItem = layoutInflater.inflate(R.layout.single_item2, parent, false);
            holder = new ProgramViewHolder(singleItem);
            singleItem.setTag(holder);
        }
        else{
            holder = (ProgramViewHolder) singleItem.getTag();
        }
        holder.programTitle.setText(programName[position]);


        }


}

MY ATTEMPT:

findViewById(R.id.TextView1).setBackgroundColor(Color.BLACK);

My attempt even worked, but it only changed the color of some textviews and at random times, it didn't change all the way I wanted.

CodePudding user response:

When you need to change the color of the text view -

holder.programTitle.setTextColor(Color.RED)

When you need to change the background color of the text view -

holder.programTitle.setBackgroundColor(Color.RED)

CodePudding user response:

Try this

holder.programTitle.setBackgroundColor(Color.BLACK);

  • Related