Asked  7 Months ago    Answers:  5   Viewed   86 times

I'm getting a view from the XML with the code below:

Button view = (Button) LayoutInflater.from(this).inflate(R.layout.section_button, null);

I would like to set a "style" for the button how can I do that in java since a want to use several style for each button I will use.

 Answers

30

Generally you can't change styles programmatically; you can set the look of a screen, or part of a layout, or individual button in your XML layout using themes or styles. Themes can, however, be applied programmatically.

There is also such a thing as a StateListDrawable which lets you define different drawables for each state the your Button can be in, whether focused, selected, pressed, disabled and so on.

For example, to get your button to change colour when it's pressed, you could define an XML file called res/drawable/my_button.xml directory like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:state_pressed="true"
    android:drawable="@drawable/btn_pressed" />
  <item
    android:state_pressed="false"
    android:drawable="@drawable/btn_normal" />
</selector>

You can then apply this selector to a Button by setting the property android:background="@drawable/my_button".

Tuesday, June 1, 2021
 
Saxophlutist
answered 7 Months ago
91

More generally, my question is, are these 3 folders working as parent and child?

Those folders work with a "most specific" matching system meaning it will match the closest(lower) API level values folder:

  • values-v14 targets APIs >= 14(it will not be selected at all for versions below 14)
  • values-v11 targets APIs between(and including) 11 and 13 if values-v14 is present otherwise it will match every version starting with 11 and above((it will not be selected at all for versions below 11))
  • values is the default folder and it will be the last to be matched, covering other APIs levels not covered by another values-xx folder. You should always(as with all resources folders) have this folder in your app

If it is indeed the way it is working, does it make sense then to setup the maximum of styles in the parent folder values and add only specific ones in v11 or v14?

Yes, this is how the Android project template is built(when you use Create new project...), it actually tells you to use the values-xx folders for customization(different look, use of newer styles, attributes):

 <!-- in the styles.xml from the v-14 values folder: -->
 <!-- API 14 theme customizations can go here. -->
Wednesday, July 28, 2021
 
DMTintner
answered 4 Months ago
31

Styles do not support multiple inheritance (at least not as of Android 3.2).

The official docs say:

If you use the dot notation to extend a style, and you also include the parent attribute, then the parent styles override any styles inheritted through the dot notation.

Wednesday, August 11, 2021
 
wael
answered 4 Months ago
46

You have to measure the children before trying to get its height and width. And in onLayout, you are decide the position of the child. After measuring, you will have the height and width of all children, then with the help of it, you can position the children. You can set its left, right, top, bottom points based on it height and width. For example, if you want to position the child at (0,0), then in onLayout

child.layout(0,0,child.getMeasuredWidth(), child.getMeasuredHeight());

If right and bottom points are mentioned wrong, the the view may cut/enlarge/shrink.

Monday, August 30, 2021
 
Günter Zöchbauer
answered 3 Months ago
12

You should be using ViewPager.getAdapter().getCount() The getAdapter() method returns the object that supplies the pages for the ViewPager.

Saturday, September 25, 2021
 
Kyle
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share