The com.android.support:percent:25.0.1 library allows you to work with percentages. So you can get the display size in pixels in xml. How ?

  • If it is not a secret, what do you want to do next and what problem to solve? How does the percentage display in xml help you solve this problem? - Mikhail Rebrov
  • You did not understand correctly. If using this library in xml you can set the size of the view for 30% of the display size, then the library in xml can get the display size in pixels. Now I will clarify a little question. - Turalllb
  • one
    From one the other does not follow. But even so: what can you do next with this data in xml? Isn't it easier to solve your problem in java code instead of xml? - Mikhail Rebrov
  • one
    Describe in the question what you want to achieve. It is possible that your task may have a solution that has eluded you. - Mikhail Rebrov
  • 2
    In general, working with pixels in android is not highly recommended, so any solution based on working with pixels (at least in xml, at least in activation), will leave much to be desired. - Mikhail Rebrov

2 answers 2

Colleague,

You don't seem to know exactly how the Android windowing system works.

XML is your wishes how you want the screen to look like. Then begins the sweep of your hotelok on the physical screen. Development is carried out in 2 stages:

  1. The dimensions are measured taking into account the wishes specified in XML and the restrictions specified by the parent. This part is executed by the View.measure() method, the method calls onMeasure() , which can be intercepted by the developer.
  2. Next, the actual sizes calculated during measure/onMeasure() are measure/onMeasure() - this is done by the View.layout() method, View.layout() is called during the size assignment, which can also be intercepted.

Sizing and assignment occurs dynamically, that is, runtime, respectively, there is no way to set static dimensions in XML.

Dynamically find the size of the screen of the device does not represent any complexity, code like

 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; //ширина экрана в пикселях int height = size.y; //высота экрана в пикселях 

In theory, you should use these dimensions in onMeasure()/onLayout() in order to give the custom size to your custom View

Google custom + view + android

    Working with px is not recommended, is this where it is said? Any dynamic manipulations in the code will take place in px, so often before a larger project a utility is written that generates px -> dp, for convenient operation, but in fact, almost all dynamic measurements in px. As for the question, the library and getting the sizes are two different things, the library is needed in order not to get these sizes, but to safely put interest, and she herself in the box considers everything else. Well, if you have already pulled the dimensions of your buddy, you do not need a lot of mind to divide this size by the size of your element and get the same percentages, or put a percentage of your size, to set the desired size in pixels, well, or if you return to You can start using your scrap to conveniently put in% and have dp on the output.

    PS Bonus : In such a situation, I recommend using FrameLayout, it is well suited exactly for such manipulations, and for lists it can give a significant performance boost, the pair is very large. Such examples can be seen in global OpenSource, in top messengers and file managers ...