Reputation: 21
I'm wondering, how can I get any current view property? For example, I create a button, change it's properties (color, height, etc) in code and I want to get it back as a string.
Is there any chance to get any property I want, such as: visibility, background, layout_width, layout_height, gravity, minWidth etc.
How can I do it quickly and should I use listener for this purpose?
Upvotes: 1
Views: 240
Reputation:
You need to use the Listener action on a View and then fetch the properties that you would like to check when that view is clicked.
Example for a TextView and fetching it's properties upon a click on that TextView:
TextView tv = (TextView) findViewById(R.id.textView);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ColorDrawable cd = (ColorDrawable) tv.getBackground();
int colorCode = cd.getColor();//colorCode in integer
}
});
In case you want to access just the property of an View then you can just access it by calling the property you want to access, such as:
TextView tv = (TextView) findViewById(R.id.textView);
ColorDrawable cd = (ColorDrawable) tv.getBackground();
int colorCode = cd.getColor();//colorCode in integer
Upvotes: 0
Reputation: 552
If I understand you correctly, here is code, that allows you obtain some properties in string:
Button button = (Button) findViewById(R.id.button);
String visibility = String.valueOf(button.getVisibility());
String width = String.valueOf(button.getWidth());
String height = String.valueOf(button.getHeight());
Please note that getWidth() and getHeight() methods returns size in pixels (not dp).
Upvotes: 2