Android

When the button is pressed, a checkmark icon is displayed in the far left corner of the button; when pressed again, a checkmark icon should appear on the same button. Can anyone help me in this case?

+3


source to share


3 answers


You can add an ImageView (say tick.png) with visibility Gone, to the left of the button. And set its visibility. Here is the code:

<LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ImageView
            android:id="@+id/iv_tick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:src="@drawable/tick"/>
        <Button 
            android:id="@+id/btn_tick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Press"/>
    </LinearLayout>

      



Now, when you click the click button, you set its visibility:

Button btn_tick = (Button)findViewById(R.id.btn_tick);
    btn_tick.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                ImageView iv_tick = (ImageView)findViewById(R.id.iv_tick);
                int visibility = iv_tick.getVisibility();
                if(visibility == View.VISIBLE)
                {
                    iv_tick.setVisibility(View.GONE);
                }
                else
                {
                    iv_tick.setVisibility(View.VISIBLE);
                }
            }
        });

      

+3


source


Although this has already been answered, here's an alternative solution: add the unicode checkmark character. There are two of them: \ u2713 and \ u2714. Just add them to your lines:

<string name="button_label_on">\u2713 on</string>
<string name="button_label_off">off</string>

      



You can of course put this right in your layout code:

<Button
   ...
   android:text="\u2713 on"
   />

      

+7


source


Checkout the CheckBox widget .

0


source







All Articles