Chip view control in android studio

Chip view là một control được phát triển bởi Material, tính ứng dụng khá cao và thú vị. Về mức độ tiện lợi và thẩm mỹ thì tuyệt hơn checkbox rất nhiều. 

Eng Sub: 
  


Step 1: Create a layout design such as:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<!--chips default-->
<com.google.android.material.chip.Chip
android:id="@+id/chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chips control"
tools:ignore="MissingConstraints"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

<!--chips checked-->
<com.google.android.material.chip.Chip
android:id="@+id/chip1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chips control"
tools:ignore="MissingConstraints"
android:checkable="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

<!--chips checked with close icon-->
<com.google.android.material.chip.Chip
android:id="@+id/chip2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chips control"
tools:ignore="MissingConstraints"
android:checkable="true"
app:closeIconVisible="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

<!--chips checked with close icon-->
<com.google.android.material.chip.Chip
android:id="@+id/chip3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chips control"
tools:ignore="MissingConstraints"
android:checkable="true"
app:closeIconVisible="true"
app:chipIcon="@drawable/cards_heart"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

</LinearLayout>

Step 2: Into java class we can code such as:
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Chip chip = findViewById(R.id.chip);
Chip chip1 = findViewById(R.id.chip1);
Chip chip2 = findViewById(R.id.chip2);
Chip chip3 = findViewById(R.id.chip3);

chip1.setOnClickListener(v -> {
Toast.makeText(MainActivity.this, "chip1 clicked", Toast.LENGTH_SHORT).show();
});

chip2.setOnCloseIconClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "chip2 close icon clicked", Toast.LENGTH_SHORT).show();
}
});

chip3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(MainActivity.this, "chip3 check change listener", Toast.LENGTH_SHORT).show();
}
});
}
}

Step 3: Result 



  

Đăng nhận xét

Mới hơn Cũ hơn