Issue
I'm using dimens.xml
file to assign values for different layout sizes(i.e dimens.xml@values, dimens.xml@values-sw600dp).
When trying to set a dimen as wrap_Content
or match_parent
I'm using the following way:
<resources>
<item name="match_parent" format="integer" type="dimen">-1</item>
<dimen name="popup_width">@dimen/match_parent</dimen>
</resources>
and it works perfectly when using this dimen inside the layout xml file, but when trying to use it programmatically like when declaring a popup window:
PopupWindow pw = new PopupWindow(layout, mContext.getResources().getDimension(R.dimen.popup_width),
ViewGroup.LayoutParams.MATCH_PARENT, true);
pw.showAtLocation(layout, Gravity.CENTER, 40, 0);
it doesn't work at all and it considers like if I passed a -1dp
values to it(it performs direct referencing), so the layout is ruined at all.
So how can I declare the match_parent dimen in the dimens.xml
file in order to use it efficiently in my code? or how even can I use the current dimen form efficiently in my code?
Solution
I solved my problem using -1.0px
for representing match_parent
, and -2.0px
for representing wrap_content
as dp
is scaling with different screens density unlike px
which is not reacting to different screen densities(and that what I need in my solution):
<dimen name="popup_width">-1.0px</dimen> <!-- for match_parent -->
<dimen name="popup_height">-2.0px</dimen> <!-- for wrap_content -->
and then I used it programmatically like:
PopupWindow pw = new PopupWindow(layout, (int)mContext.getResources().getDimension(R.dimen.popup_width),
(int)mContext.getResources().getDimension(R.dimen.popup_height), true); // notice that they have to be casted as int
Answered By - Muhammed Refaat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.