I need to design a screen with over 50 buttons. I can not create the buttons programmatically and should put them one by one into the XML layout manually since the sizes and positions of buttons are not regular. However, I designed XML layout with over 50 buttons and could inflate it successfully in the activity.
When I build the project, it looks like working smoothly but I can see this in the log.
I/Choreographer: Skipped 56 frames! The application may be doing too much work on its main thread.
Is there any good idea that can build the layout without this problem?
To clarify, I attached a screenshot that shows how the UI will look like.
As you can see, I need every leaves to be clickable and I should change their colors.
And sometimes it crashes when reloading this activity in the app. Here is the crash log.
FATAL EXCEPTION: main Process: com.au.therefreshproject.refreshandgogreen, PID: 14151 java.lang.OutOfMemoryError: Failed to allocate a 301476 byte allocation with 77600 free bytes and 75KB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080) at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635) at android.content.res.Resources.loadDrawable(Resources.java:2540) at android.content.res.TypedArray.getDrawable(TypedArray.java:870) at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:170) at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:115) at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1215) at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1124) at android.content.res.Resources.loadDrawableForCookie(Resources.java:2630) at android.content.res.Resources.loadDrawable(Resources.java:2540) at android.content.res.TypedArray.getDrawable(TypedArray.java:870) at android.view.View.(View.java:3948) at android.widget.TextView.(TextView.java:677) at android.widget.Button.(Button.java:109) at android.widget.Button.(Button.java:105) at android.support.v7.widget.AppCompatButton.(AppCompatButton.java:62) at android.support.v7.widget.AppCompatButton.(AppCompatButton.java:58) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109) at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:1008) at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1067) at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
Some parts of XML layout are here.
<RelativeLayout
android:layout_width="320dp"
android:layout_height="400dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tree_01"
android:layout_alignParentBottom="true"/>
<Button
android:id="@+id/treenBtn0"
android:layout_width="19dp"
android:layout_height="31dp"
android:layout_marginLeft="31dp"
android:layout_marginTop="90dp"
android:tag="28"
android:background="@drawable/tree_02"/>
<Button
android:id="@+id/treenBtn1"
android:layout_width="27dp"
android:layout_height="22dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="122dp"
android:tag="29"
android:background="@drawable/tree_03"/>
And the drawable resource is
drawable/tree_02.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/tree_02_on"/>
<item android:drawable="@drawable/tree_02_off"/>
</selector>
drawable/tree_03.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/tree_03_on"/>
<item android:drawable="@drawable/tree_03_off"/>
</selector>