Wednesday 17 August 2016

CheckedTextview


An extension to TextView that supports the Checkable interface and displays.
This is useful when used in a ListView where the setChoiceMode has been set to something other than CHOICE_MODE_NONE.
http://i.stack.imgur.com/2qCx9.png
Code -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/LinearLayout1"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
>
    <
CheckedTextView
       
android:id="@+id/simpleCheckedTextView"
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
       
android:checked="true"
       
android:gravity="center"
       
android:checkMark="@drawable/checked"
       
android:text="Checked Text View" />
</
LinearLayout>

XML attributes
android:checkMark  
Drawable used for the check mark graphic. Must be a reference to another resource.
android:checkMark="@drawable/checked"
android:checkMarkTint          
Tint to apply to the check mark.  (If you want to change the color of image then use tint)
Must be a color value.
android:checkMarkTint="@color/colorAccent"

android:checkMarkTintMode            
Blending mode used to apply the check mark tint.
android:checkMarkTintMode="multiply"

Constant
Value
Description
src_over
3
The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc]
src_in
5
The tint is masked by the alpha channel of the drawable. The drawable’s color channels are thrown out.
[Sa * Da, Sc * Da]
src_atop
9
The tint is drawn above the drawable, but with the drawable’s alpha channel masking the result.
 [Da, Sc * Da + (1 - Sa) * Dc]
multiply
14
Multiplies the color and alpha channels of the drawable with those of the tint.
 [Sa * Da, Sc * Dc]
screen
15
[Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
add
16
Combines the tint and drawable color and alpha channels, clamping the result to valid color values.
 Saturate(S + D)

android:checked       
Indicates the initial checked state of this text.
Must be a boolean value, either "true" or "false".
android:checked="true"

Public methods

boolean               isChecked()
checkedTextView=(CheckedTextView)findViewById(R.id.checkedTextView);



if(checkedTextView.isChecked() == true)

{

    Toast.makeText(getApplicationContext(),"Checked State",Toast.LENGTH_SHORT).show();

}else

{

    Toast.makeText(getApplicationContext(),"Checked State",Toast.LENGTH_SHORT).show();

}


void       setCheckMarkDrawable(Drawable d)
Set the check mark to the specified drawable.
checkedTextView.setCheckMarkDrawable(R.drawable.checked);


void       setChecked(boolean checked)
Sets the checked state of this view.
checkedTextView.setChecked(true);

void       setVisibility(int visibility)
Set the enabled state of this view.
checkedTextView.setVisibility(View.INVISIBLE);
 
other Value are
 
View.VISIBLE
 
View.GONE

void       toggle()
Change the checked state of the view to the inverse of its current state
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    >

    <CheckedTextView

        android:id="@+id/checkedTextView"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:gravity="center"

        android:text="Checked Text View" />

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Toggle"

        android:onClick="goToggle"

        />

</LinearLayout>

MainActivity.java
public class MainActivity extends AppCompatActivity {



    CheckedTextView checkedTextView;



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        checkedTextView=(CheckedTextView)findViewById(R.id.checkedTextView);



        checkedTextView.setChecked(true);

        checkedTextView.setCheckMarkDrawable(R.drawable.checked);

    }

    public  void goToggle(View view)

    {

        if(checkedTextView.isChecked())

        {

            checkedTextView.setChecked(false);

            checkedTextView.setCheckMarkDrawable(R.drawable.ic_delete);

        }else

        {

            checkedTextView.setChecked(true);

            checkedTextView.setCheckMarkDrawable(R.drawable.checked);

        }

    }

}











































22

No comments:

Post a Comment