JLB85
JLB85

Reputation: 17

Android Studio ImageButton Tooltip has curious behavior if button is created in java source file

I'm coming back to Android programming now I'm retired, and have encountered many problems when preparing the skeleton of my application.

I finally created successfully imageButtons by dynamically coding in java.

buttons are placed inside an horizontal scroll view, and will be managed by the application (most of them will be configurable (visible or not) that's why I must create them dynamically.

I had to migrate an old sample project to AppCompat to make that work, since I want to be able to use SVG files, the application will manage vectorial drawings.

I'm using Android Studio 2021.1.1, AGP 7.1.3, Gradle 7.2

The problem at this time is that dynamic buttons has a tooltip which is not correct, the tooltip appears over a larger frame, while created in the xml file it is correct. This only occurs if I modify the Imagebutton Style with the coding below, otherwise the tootips are correct:

left image: tooltip ok, right one: not ok enter image description here

styles.xml:
<style name="ButtonJlb" parent="android:style/Widget.Holo.Light.ImageButton">
</style>
MainActivity.java
//create button with custom style
int buttonStyle = R.style.ButtonJlb;
ImageButton buttonView = new ImageButton(new ContextThemeWrapper(this, buttonStyle), null, buttonStyle);
//set image
buttonView.setImageResource(R.drawable.ic_android);
//set tooltip
btnSettings.setTooltipText("Settings");

Maybe my code is not correct somewhere ? I didn't find documentation enough...

I tried to use version 2022.3.1-19 of Android Studio, but I have only 8GB of memory in the computer, and when launching the emulator, AS crashes and disapears when memory used reaches 7.9GB.

I have seen people complaining about very bad performance (long response time) of the new version, is it possible that memory could be the problem ?)

I will purchase additional memory next week, but not sure it is an IDE issue.

thanks in advance for any idea or solution... Sorry if my post is not clear enough... let me know how to improve...

My API level is 17, I think, where can I find the current version in the settings ? here is the xml code for buttons, only the first one is created in this example:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/linearLayoutBtn_top"
            android:layout_width="wrap_content"
            android:layout_height="52dp"
            android:orientation="horizontal">

            <ImageButton
                android:id="@+id/imageButton"
                style="@style/ButtonJlb"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                app:srcCompat="@drawable/ic_android" />
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

and this is the source code for dynamic creation (btnSettings is a reference to the first dynamically created button for next usage)

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // scroll buttons creation
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutBtn_top);
        for (int i = 1; i <= 20; i++) {
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.MATCH_PARENT
            );
            int buttonStyle = R.style.ButtonJlb;
            ImageButton buttonView = new ImageButton(new ContextThemeWrapper(this, buttonStyle), null, buttonStyle);
            buttonView.setImageResource(R.drawable.ic_android);
            //change style of button
            // fix width (1dp = 4px)
            buttonView.setMinimumWidth(240);
            buttonView.setTooltipText("btn"+i);
            switch (i) {
                case 1: {
                    btnSettings = buttonView;
                    buttonView.setTooltipText("Settings");
                    buttonView.setImageResource(R.drawable.ic_nand_generated);
                    break;
                }
                case 2: btnMap = buttonView; buttonView.setTooltipText("Map");break;
            }
            ImageButton firstbutton = findViewById(R.id.imageButton);
            if (firstbutton != null) {
                firstbutton.setTooltipText("First button");
            }
            layout.addView(buttonView, p);
        };

        // Click event on first button (settings)
        btnSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startMapActivity();
            }
        });

Regarding Maurice Lam reply, if I just create buttons with this code

ImageButton buttonView = new ImageButton(this, null, buttonStyle);

this is what I get:

enter image description here

Upvotes: 0

Views: 53

Answers (0)

Related Questions