Java – how to make the foreground attribute of a button work under API 23?
I have two buttons nested in LinearLayout. Between these buttons are two textviews. In XML, I have set the foreground as the image of each button
It runs API 23 on my device. But on other devices below API 23, foreground images will not be displayed, but lead to the default white solid color. Is there any way to make these images display the foreground below API 23?
We tried FrameLayout, but it didn't do as we wanted. Will imagebuttons be a better way to solve this problem?
One of the core functions of our application is that each time the user clicks a button, the size will increase and the image will extend accordingly. This is done dynamically in the code. If I want to use imagebuttons, I need to set the layout parameters for the height and width each time, rather than a line of code for setting the height
Any tips will be appreciated!
Edit: code I'm using –
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="11"
android:background="@android:color/black">
<Button
android:layout_weight="5"
android:id="@+id/firstP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:foreground="@drawable/icebutton"
android:scaleX="1"
android:scaleY="1"/>
<TextView
android:layout_weight="0.5"
android:id="@+id/firstPlayer"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotation="180"
android:textColor="@android:color/white"
android:background="@android:color/transparent"/>
<TextView
android:layout_weight="0.5"
android:id="@+id/secondPlayer"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:background="@android:color/transparent"/>
<Button
android:layout_weight="5"
android:id="@+id/secondP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:foreground="@drawable/firebutton"
android:scaleX="1"
android:scaleY="1"/>
</LinearLayout>
resolvent:
We found that there are two problems that make the image unable to be displayed. 1. The size of the image file is too large, resulting in OUTOFMEMORY error, which makes the button unable to display the image. 2. The foreground attribute is not applicable to API 22 and earlier
Steps to solve these problems: 1. We reduced the size of the image file. We replaced the button with imagebutton. 3. In the XML file, we deleted the foreground attribute, added a black background, and added the image through the SRC attribute. The following is a fragment
<ImageButton
android:layout_weight="5"
android:id="@+id/firstP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:src="@drawable/icebutton"
android:scaleType="fitXY"
android:background="@android:color/black"/>
>Then, we must change our code by setting layoutparams to dynamically adjust the height of the button to match the new image button with the help of this link: how to change size of button dynamic in Android
Now everything is perfect!