Android
안드로이드 Chip 레이아웃 - Chip + Chip Group
jhk-im
2020. 3. 24. 22:06
참고
https://medium.com/material-design-in-action/chips-material-components-for-android-46001664a40f
Chips: Material Components for Android
One of the cool widgets that you can use with the Material Design Components library is Chip. A Chip is a component that can represent…
medium.com

app - build.gradle
1
2
|
//chip
implementation 'com.google.android.material:material:1.1.0'
|
res -> values style.xml
1
2
3
4
5
6
7
8
9
10
11
|
<resources>
<!-- Base application theme. -->
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
|
스타일에서 디폴트로 셋팅된 AppCompat 테마로 되어있을 경우 에러가 발생하여 앱이 실행되지 않는다.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
에러
java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
다음과같이 MaterialComponents 테마 중 하나를 상속하도록 앱 테마를 설정하여야 한다.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
activity_chip.xml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".chip.ChipActivity">
android:id="@+id/chip_group"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:singleSelection="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
|
ChipActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
public class ChipActivity extends AppCompatActivity {
ChipGroup chipGroup;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chip);
chipGroup = (ChipGroup) findViewById(R.id.chip_group);
for(int i = 0; i<5; i++){
// Chip 인스턴스 생성
Chip chip = new Chip(ChipActivity.this);
// Chip 의 텍스트 지정
chip.setText("chip"+i);
// 체크 표시 사용 여부
chip.setCheckable(true);
// chip close 아이콘 이미지 지정
chip.setCloseIcon(getDrawable(R.drawable.ic_close));
// close icon 표시 여부
chip.setCloseIconVisible(true);
// chip group 에 해당 chip 추가
chipGroup.addView(chip);
// chip 인스턴스 클릭 리스너
chip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ChipActivity.this, "Check", Toast.LENGTH_SHORT).show();
}
});
// chip 인스턴스 close 버튼 클릭 리스너
chip.setOnCloseIconClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chipGroup.removeView(v);
}
});
}
}
}
|
cs |