S
S
Sergey2016-02-10 17:14:06
Android
Sergey, 2016-02-10 17:14:06

How to programmatically change text color in an Android GridView cell?

I have a main activity that hosts a GridView. As the content of the GridView cell, a separate xml file is used, on which the TextView and ImageView are located. I want to programmatically change the color of a TextView in a GridView cell when a button is clicked on the main activity.
The project can be downloaded from the link: Here
At the moment I'm trying to implement it through the code:
View cell = findViewById(R.layout.cellgrid);
TextView cellText = (TextView) cell.findViewById(R.id.textpart);
cellText.setTextColor(Color.parseColor("#000000"));
But on the first line I get an error: Screenshot

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Dorofeev, 2016-02-10
@googlgan

You just need to use the GridView.getChildAt() method in a loop:

public void MyButtonClick(View view) {
        int count = gridView.getChildCount();
        for (int i = 0; i < count; i++) {
            View cell = gridView.getChildAt(i);
            TextView cellText = (TextView) cell.findViewById(R.id.textpart);
            cellText.setTextColor(Color.parseColor("#FF0033"));
        }
    }

A small note: your Adapter is a little crooked, so not all cells will change the text color. Try to fix the populating adapter code with an array.
The complete Activity code after a bit of tweaking:
public class MainActivity extends Activity {

private GridView gridView;
    private ImageTextAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adapter = new ImageTextAdapter(this);
        gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(gridViewOnItemClickListener);
    }

    private GridView.OnItemClickListener gridViewOnItemClickListener = new GridView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // пожалуйста, не комментируйте код так:

            // Sending image id to FullScreenActivity
            //Intent i = new Intent(getApplicationContext(),
            //        FullImageActivity.class);
            // passing array index
            //i.putExtra("id", position);
            //startActivity(i);
        }
    };


    public void MyButtonClick(View view) { // нарушение Code Convention в имени метода

        int count = gridView.getChildCount();
        for (int i = 0; i < count; i++) {

            View cell = gridView.getChildAt(i);
            TextView cellText = (TextView) cell.findViewById(R.id.textpart);
            cellText.setTextColor(Color.parseColor("#FF0033"));
        }


    }


}

Agree that looks better than:
public class MainActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageTextAdapter(this));

        gridview.setOnItemClickListener(gridviewOnItemClickListener);
    }

    private GridView.OnItemClickListener gridviewOnItemClickListener = new GridView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                                long id) {
            // TODO Auto-generated method stub

            // Sending image id to FullScreenActivity
            //Intent i = new Intent(getApplicationContext(),
            //        FullImageActivity.class);
            // passing array index
            //i.putExtra("id", position);
            //startActivity(i);
        }
    };


    public void MyButtonClick(View view)
    {
        View cell = findViewById(R.layout.cellgrid);
        TextView cellText = cell.findViewById(R.id.textpart);
                cellText.setTextColor(Color.parseColor("#000000"));
    }

}

T
Tiberal, 2016-02-10
@Tiberal

MyButtonClick inflates a new view and assigns a color to it. You need to get the view from your gridView and it will already change color. Well, after searching for the findViewById view, it needs to be cast to a TextView

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question