이번 시간에는 토글버튼 쉽게 꾸미는
방법에 대하여 알아보겠습니다.
토글버튼은 둘 중 하나를 선택하는
간단한 모양의 버튼입니다.
1. 실행 화면
2. 라이브러리 등록
3. 메인 화면 구성 activity_main.xml
4. 메인 코드 구현 MainActivity.java
build.gradle(Module:프로젝트명:app)
dependencies 괄호 안에 아래 코드를 넣어주시면 됩니다.
implementation 'com.github.angads25:toggle:1.1.0'
참조문서
https://github.com/Angads25/android-toggle
GitHub - Angads25/android-toggle: Custom Switches for Android
Custom Switches for Android. Contribute to Angads25/android-toggle development by creating an account on GitHub.
github.com
<?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"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.github.angads25.toggle.widget.LabeledSwitch
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textSize="14sp"
app:on="true" />
<com.github.angads25.toggle.widget.LabeledSwitch
android:id="@+id/toggle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textSize="14sp"
app:on="true" />
<TextView
android:id="@+id/toggleState1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="끄기"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.github.angads25.toggle.widget.LabeledSwitch
android:id="@+id/toggle3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textSize="14sp"
app:colorBorder="#D50000"
app:colorOff="#EDE7F6"
app:colorOn="#673AB7"
app:on="true"
app:textOff="끄기"
app:textOn="켜기" />
<com.github.angads25.toggle.widget.LabeledSwitch
android:id="@+id/toggle4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textSize="14sp"
app:colorBorder="#D50000"
app:colorOff="#EDE7F6"
app:colorOn="#673AB7"
app:on="true"
app:textOff="끄기"
app:textOn="켜기" />
<TextView
android:id="@+id/toggleState3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
- 속성 -
setEnabled: true(활성화), false(비활성화)
setOn: true(On), false(Off)
public class MainActivity extends AppCompatActivity {
TextView toggleState1, toggleState3;
LabeledSwitch toggle1, toggle2, toggle3, toggle4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggle1 = findViewById(R.id.toggle1);
toggle2 = findViewById(R.id.toggle2);
toggle3 = findViewById(R.id.toggle3);
toggle4 = findViewById(R.id.toggle4);
toggleState1 = findViewById(R.id.toggleState1);
toggleState3 = findViewById(R.id.toggleState3);
toggle1.setOnToggledListener(new OnToggledListener() {
@Override
public void onSwitched(ToggleableView toggleableView, boolean isOn) {
if(isOn){
toggle2.setEnabled(true); //활성화
toggle2.setOn(true); //On으로 변경
toggleState1.setText("ON");
}else{
toggle2.setEnabled(false); //비활성화
toggle2.setOn(false); //Off로 변경
toggleState1.setText("OFF");
}
}
});
toggle3.setOnToggledListener(new OnToggledListener() {
@Override
public void onSwitched(ToggleableView toggleableView, boolean isOn) {
if(isOn){
toggle4.setEnabled(true);
toggle4.setOn(true);
toggleState3.setText("켜기");
}else{
toggle4.setEnabled(false);
toggle4.setOn(false);
toggleState3.setText("끄기");
}
}
});
}//onCreate
} //MainActivity
2020.12.12 - [안드로이드] - [안드로이드]스위치(Switch) On, Off 만들어 보기
[안드로이드]스위치(Switch) On, Off 만들어 보기
2020/12/11 - [안드로이드] - [안드로이드]달력(CalendarView) 만들어보기 이번 시간에는 스위치를 만들어보겠습니다. On, Off 기능을 가진 간단한 예제입니다. 1. 화면구현(activity_main.xml) <?xml version="1...
aries574.tistory.com
2020.12.18 - [안드로이드] - [안드로이드]TextSwitcher 글자변환 애니메이션 만들어보기
[안드로이드]TextSwitcher 글자변환 애니메이션 만들어보기
2020/12/17 - [안드로이드] - [안드로이드]화면계속켜짐 상태로 만들어보기 FLAG_KEEP_SCREEN_ON 이번 시간에는 글자변환에 애니메이션 효과를 쉽게 해주는 TextSwitcher를 이용해서 글자변환 애니메이션을
aries574.tistory.com
2020.06.11 - [안드로이드] - [안드로이드] 스위치(Switch) 텍스트뷰 색상변경
[안드로이드] 스위치(Switch) 텍스트뷰 색상변경
이번 시간에는 스위치를 통해 색상을 변경했다가 다시 원래대로 돌아가게 하는 화면을 만들어 보겠습니다. 안드로이드에서는 Switch태그를 사용합니다. 1. 메인화면(activity_main.xml) 색상을 바꿀 텍
aries574.tistory.com
[안드로이드] 색상 선택 색상표 쉽게 만드는 방법 (8) | 2022.02.02 |
---|---|
[안드로이드] 애니메이션 시계(ClockAnimationView) 쉽게 만드는 방법 (0) | 2022.02.01 |
[안드로이드] 통화코드 통화상징 가져오는 드롭다운메뉴(DropDown Menu) 쉽게 만드는 방법 (0) | 2022.01.30 |
[안드로이드] 메시지(Toast) 쉽게 꾸미는 방법 2탄 (0) | 2022.01.29 |
[안드로이드] 검색한 단어 하이라이트(highlight) 주는 방법 (0) | 2022.01.28 |
댓글 영역