3

On click of a button , a check mark icon should be displayed on the leftmost corner of the button, when reclicked on the same button , the check mark icon should disppear. Could some on help me out in this case?

3 Answers3

8

While this is already answered, here's an alternative solution: add a unicode checkmark symbol. There are two of them: \u2713 and \u2714. Just add them to your strings:

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

Of course, you can put this directly into your layout code, too:

<Button
   ...
   android:text="\u2713 on"
   />
SMBiggs
  • 11,034
  • 6
  • 68
  • 83
3

You can add an ImageView (lets say tick.png) with visibility Gone, at the left of the Button. And set its visibilty. 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, on Button click event you set its visibilty:

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);
                }
            }
        });
Khawar
  • 5,197
  • 4
  • 28
  • 52
  • 1
    But I want the tick mark to be overlapped on the button , not beside the button –  Mar 12 '12 at 07:03
  • You can also use an ImageButton. With appropriate Images for Checked and normal states. And change them on Click event. – Khawar Mar 12 '12 at 10:33
0

Checkout the CheckBox widget.

neevek
  • 11,760
  • 8
  • 55
  • 73
  • I dont want a checkbox . The UI should have only a button and when clicked, a tick mark icon should appear on the left corner of the button –  Mar 12 '12 at 05:18
  • You can tailor the CheckBox widget to look like a tick. Anyway, if you don't(though I think you should) want to use the CheckBox widget, you can add a tick image to the left of the button with its initial visibility set to *gone*, and set it to *visibile* when the button is clicked. – neevek Mar 12 '12 at 05:24
  • I couldnt get the latter part of your solution. Could you please let me know as how could you add an image to button and then alter its visibility? –  Mar 12 '12 at 05:26
  • It's two widgets, a tick image(ImageView) and a button(Button). Layout them consecutively from left to right. – neevek Mar 12 '12 at 05:31