-2

How can i do TextView UnClickable. ? When I tried with android:clickable="false" but, it doesn't working for me.

See below XML code :

<TextView
   android:id="@+id/tv_submit"
   android:layout_width="match_parent"
   android:layout_height="@dimen/btn_height_common"
   android:layout_marginTop="@dimen/_15sdp"
   android:background="@drawable/back_btn_yellow_rounded"
   android:gravity="center"
   android:textColor="@color/white"
   android:layout_marginRight="@dimen/_20sdp"
   android:layout_marginLeft="@dimen/_20sdp"
   android:textStyle="bold"
   android:alpha="0.50"
   android:clickable="false" <!--Hear you can see i set clickable="false-->
   android:textSize="@dimen/btn_label_inputtextsize"
   android:text="@string/submit"
   android:fontFamily="@font/sf_pro_display_bold"/>

Update:

Initialization of Checkbox :

        chk_credit_card =  findViewById(R.id.chk_credit_card);
        chk_debit_card = findViewById(R.id.chk_debit_card);
        chk_bank_deposit = findViewById(R.id.chk_bank_deposit);
        chk_in_app_purchase = findViewById(R.id.chk_in_app_purchase);

Work with checkbox code:

for (CheckBox checkBox : chk_credit_card) {
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @SuppressLint("Range")
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        // one of the checkbox is checked so enable button
                        tv_submit.setEnabled(true);
                        tv_submit.setAlpha(150);
                        tv_submit.setEnabled(true);
                       /* chk_debit_card.setChecked(false);
                        chk_bank_deposit.setChecked(false);
                        chk_in_app_purchase.setChecked(false);*/
                    } else {
                        // check if any of check box is checked
                        boolean isAnyChecked = false;
                        for (CheckBox box : chk_credit_card) {
                            if (box.isChecked()) isAnyChecked = true;
                        }
                        tv_submit.setEnabled(isAnyChecked);
                    }
                }
            });

I refer too much solution but it's not working for me. So please help. :)

Sandeep Patel
  • 202
  • 3
  • 16

5 Answers5

3

Try This, In Android Studio Package List There is a folder called 'res' under the 'res' folder select folder called values select styles.xml in given folder

Change it values from

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

To This

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

Hope it Works!

Melan Rashitha
  • 504
  • 2
  • 13
1
textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()){
                    // do your work
                }
            }
        });

Update

  • You have multiple checkbox
  • If any of checkbox is checked then button should be enabled
  • Checkbox should be listened live.

    ArrayList<CheckBox> checkBoxes = new ArrayList<>();
    checkBoxes.add(checkBox1);
    checkBoxes.add(checkBox2); // add all your checkbox
    for (CheckBox checkBox : checkBoxes) {
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // one of the checkbox is checked so enable button
                    textView.setEnabled(true);
                } else {
                    // check if any of check box is checked
                    boolean isAnyChecked = false;
                    for (CheckBox box : checkBoxes) {
                        if (box.isChecked()) isAnyChecked = true;
                    }
                    textView.setEnabled(isAnyChecked);
                }
            }
        });
    }
    

Suggestion

TextView has no ripple effect by default, use Button for a good UI. Check this answer for making background selector for enabled, disabled, pressed colors.

Update 2

After looking into your problem, you actually need RadioGroup, that allow to check only one RadioButton

if (radioGroup.getCheckedRadioButtonId() == -1){
  // no radio button is checked
} 
else {
  // one radio button is checked
}

Just use RadioGroup in your xml. Here is an example.

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkedButton="@id/rbMale"
    >

    <RadioButton
        android:id="@+id/rbMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male"
        />

    <RadioButton
        android:id="@+id/rbFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female"
        />
</RadioGroup>

You can do radioGroup.getCheckedRadioButtonId() == R.id.rbMale also.

Update 3

If you want CheckBox like RadioButton, its very easy

<RadioButton
    style="@android:style/Widget.CompoundButton.CheckBox"
    ...
    />

Contrats!, all your problem solved :)

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

You can add this property to your TextView to make it not clickable

From your XML

android:enabled="false"

In JAVA

             textView.setEnabled(checkBox.isChecked());
Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
0

As per your comment... you can follow this logic.

textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (if_checked) {
                    // go to another activity             
                }else{
                   //ignore
           }
        });
SRB Bans
  • 3,096
  • 1
  • 10
  • 21
0

setting onClickListener to TextView will change clickable property to true. If you don't want to textView clicked do not set it clickListener. If you want to textView will be clickable on some cases and don't want to other cases then you can set click listener or remove it like:

to enable clicks:

textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               //do your work
            }
        });

to disable clicks:

textView.setOnClickListener(null);
Axbor Axrorov
  • 2,720
  • 2
  • 17
  • 35