I need to get the background color of an item programmatically, in Java, to do more things. Is there any possibility to do so?
This is my code:
gridView = findViewById(R.id.gridViewServices);
listView = findViewById(R.id.listViewServices);
// Grid View
GridAdapter adapter = new GridAdapter(ServicesActivity.this, serviceList);
txtService = findViewById(R.id.textServices);
gridView.setAdapter(adapter);
// List View
serviceListAdd = new ArrayList<String>();
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, serviceListAdd);
listView.setAdapter(arrayAdapter);
ColorDrawable gridViewColor = (ColorDrawable) gridView.getBackground();
int colorId = gridViewColor.getColor();
System.out.println("Colour: " + colorId);
Edit: Your solution, @majuran, throws Exception:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.ColorDrawable.getColor()' on a null object reference
To be honest, I don't know why, because I am using gridView before this line to set the Adapter and it works fine.