I've many LinearLayout. And I want to set width of all of them by global variable. Is it possible to do so. So How can I set width of a Layout by retrieving global variable in .xml and runtime too.
Asked
Active
Viewed 273 times
1 Answers
1
I'm not sure I understand your question completely, but in Android you can create a strings variable pretty easily that accomplishes what it sounds like you want. A universal variable for the size of a LinearLayout, say fill_parent.
To do this you would open the strings.xml file and write a string resource like so:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="global_width">fill_parent</string>
</resources>
Then in your layout file do something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_view"
android:layout_width="@string/global_width"
>
Then you can change them all at once using the string resource instead of going through and manually changing values.

ViaTech
- 2,143
- 1
- 16
- 51
-
Sorry sir. you didn't get me correctly. I want to do something like. set value of global variable to @string which changes width of my view. – Abdul Qadir Feb 18 '18 at 19:22
-
Can you post some code explaining what you are trying to accomplish? I'm still not sure I follow. If you just need a global variable via Java: https://stackoverflow.com/a/1945297/8382028 – ViaTech Feb 19 '18 at 19:58
-
And thanks for the link. I got another idea to do same job. – Abdul Qadir Feb 26 '18 at 19:36
-
Glad you figured something out... just FYI, most questions on this forum include better answers if you provide code. Seeing the code you're writing removes most misunderstandings as people can see directly what you are doing (or trying to do). Android development is fun (half the apps I use on my phone I built), good luck in the field! – ViaTech Feb 28 '18 at 21:58