1

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?

2 Answers2

2

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).

SolderingIronMen
  • 554
  • 1
  • 5
  • 20
  • Thank you! It works for me fine. But there is one more thing, when I ask for ID, I received int value, like "2131427422" instead of String value, which I set in properties. How to transfom it? – S. Plekhanov Aug 27 '17 at 16:06
  • For yor second question use this way: https://stackoverflow.com/a/14648102/6334198 – SolderingIronMen Aug 27 '17 at 16:45
0

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
  • The click listener is not necessary – OneCricketeer Aug 27 '17 at 14:58
  • I know. But I was wondering in case the Question asker want to know properties of a specific view when it is being selected by the user upon touching. AT least thats what I understood from the last sentence of the question. –  Aug 27 '17 at 15:55