Answer the question
In order to leave comments, you need to log in
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
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"));
}
}
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"));
}
}
}
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"));
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question