<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="안녕하세요"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18dp"/>
// width & height 는 주로 wrap_content 사용.
// color & style & size 설정 가능
inputType 에 따라 올라오는 자판이 달라짐
<!-- Password (Nemeric) xml Code -->
<EditText
android:id="@+id/editTextNumberPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Password (Numeric)"
android:inputType="numberPassword"/>
// hint 는, edittext 클릭하기 전 흐리게 적혀있는 글씨
| 종류 | 입력 가능한 값 | 설명 | inputType |
|---|---|---|---|
| Plain Text | 모든 문자, 기호, 숫자 | 기본 입력 | none |
| Password | 모든 문자, 기호, 숫자 | 비밀번호 입력. * 처리 | textPassword |
| Password (Numeric) | 숫자 | 비밀번호 숫자만 입력. * 처리 | numberPassword |
| 모든 문자, 기호, 숫자 | 비밀번호 입력. * 처리 안함 | textVisiblePassword | |
| 모든 문자, 기호, 숫자 | 이메일 입력 | textEmailAddress | |
| Phone | 모든 문자, 기호, 숫자 | 발음문자 입력 | textPhonetic |
| Postal Address | 모든 문자, 기호, 숫자 | 우편번호 입력 | textPostalAddress |
| Multiline Text | 모든 문자, 기호, 숫자 | 줄바꿈 설정 가능 | textMultiLine |
| Time | : 기호 숫자 | 시간 입력 | time |
| Date | - 기호 숫자 | 날짜 입력 | date |
| Number | 숫자 | 숫자 입력 | number |
| Number (Signed) | 숫자 | 부호있는 숫자 입력 | numberSigned |
| Number (Decimal) | . 기호 숫자 | 소수 입력 | numberDecimal |
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basic Button" />
btn.setOnClickListener {
// 버튼 클릭시 이벤트 코딩
}
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myimage" />
// src속성으로 drawable 폴더에 있는 이미지 불러오기 가능
android:scaleType
android:alpha
app:layout_constraintDimensionRatio
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
// name 속성에 Fragment 를 추가시킨다
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />ImageView와 속성 거의 유사. 사진을 fitting 하려면 자동으로 꽉차는 ImageView 와 달리 설정을 더 해줘야한다.
android:scaleType
android:alpha
Background 설정
android:background="@android:color/transparent"
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle"
android:textOff="Off"
android:textOn="On" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Off"
android:textOn="On"
android:checked="true" />


class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.togglebutton)
switch1.setOnCheckedChangeListener { button, ischecked ->
if(ischecked)
Toast.makeText(this, "Switch is checked", Toast.LENGTH_SHORT).show()
else
Toast.makeText(this, "Switch is unchecked", Toast.LENGTH_SHORT).show()
}
}
}
app 모듈의 build.gradle 에 코드 추가
implementation 'com.google.android.material:material:$version'
resource style 수정
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

<?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"
...>
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
app:chipSpacingHorizontal="30dp"
app:singleSelection="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.google.android.material.chip.Chip
style="@style/Widget.MaterialComponents.Chip.Choice"
android:id="@+id/chip01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipMinHeight="40dp"
app:chipStartPadding="20dp"
app:chipEndPadding="20dp"
app:chipBackgroundColor="@color/chip_check"
android:textSize="18sp"
android:textColor="@color/white"
android:text="Chip 1"/>
<com.google.android.material.chip.Chip
style="@style/Widget.MaterialComponents.Chip.Choice"
android:id="@+id/chip02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipStartPadding="20dp"
app:chipEndPadding="20dp"
app:chipStrokeColor="@color/chip_border_check"
app:chipStrokeWidth="2dp"
app:chipBackgroundColor="@color/chip_check"
android:textSize="18sp"
android:textColor="@color/white"
android:text="Chip 2"/>
<com.google.android.material.chip.Chip
style="@style/Widget.MaterialComponents.Chip.Choice"
android:id="@+id/chip03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipStartPadding="20dp"
app:chipEndPadding="20dp"
app:chipBackgroundColor="@color/chip_check"
android:textSize="18sp"
android:textColor="@color/white"
android:text="Chip 3"/>
</com.google.android.material.chip.ChipGroup>
</LinearLayout>
ChipGroup 속성
Chip 속성
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
android:checked="true"/>
// text 로 체크박스 오른쪽에 글을 표시할수 있다
// checked 로 최초 체크박스의 체크상태를 표시할 수 있다.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.checkbox)
// setOnClickListener 설정
checkBox.setOnClickListener{
// check 된다면
if(checkBox.isChecked)
Toast.makeText(this, "checked", Toast.LENGTH_SHORT).show()
// uncheck 된다면
else
Toast.makeText(this, "unchecked", Toast.LENGTH_SHORT).show()
}
}
}

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.radiobutton)
radioGroup.setOnCheckedChangeListener { group, checkedId ->
val radio: RadioButton = findViewById(checkedId)
Toast.makeText(applicationContext," On checked change :" + " ${radio.text}", Toast.LENGTH_SHORT).show()
}
}
fun radio_button_click(view: View){
val radio: RadioButton = findViewById(radioGroup.checkedRadioButtonId)
//Toast.makeText(applicationContext,"On click : ${radio.text}", Toast.LENGTH_SHORT).show()
}
}
여러개의 버튼이 겹쳐있다가, 애니메이션으로 나뉘는 효과 구현 가능
Style 적용을 AppCompat 이 아닌 MaterialComponents 로 변경 필요
app 모듈의 build.gradle 에 코드 추가
implementation 'com.google.android.material:material:$version'
resource style 수정
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">



<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_my_icon"
android:contentDescription="@string/submit"
app:rippleColor="@color/pink"/>
rippleColor
src
val fab: View = findViewById(R.id.fab)
fab.setOnClickListener { view ->
Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.show()
}
<WebView
android:id="@+id/wView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/pb_ingame_timebar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="120dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:progress="100"
android:progressBackgroundTint="@color/black"
android:progressTint="#DBA901"
android:scaleY="3" />
// style : 가로 ProgressBar를 진행하기 위해, 해당 스타일을 Horizontal로 적용시킨다
// progressBackgroundTint : 비어있는 진행바 부분 색상을 선택한다
// progressTint: 채워져있는 진행바 부분 색상을 선택한다
// scaleY : 진행바의 굵기를 선택한다
// progress : 진행바의 채워져있는 비율을 선택한다. 이부분을 시간에따라 변화시키면 된다
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:thumb="drawable/"
android:theme="@style/Widget.AppCompat.SeekBar.Discrete" />
// max : 바의 최댓값
// progress : 처음 보일때 진행도 설정
// thumb : 드래그 할때 커서모양 설정 가능

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none"/>
// scrollbars : 스크롤바 유무 / 방향 설정
// fillViewport : 스크롤뷰 하위 항목의 높이가 더 작은경우, 부모View 만큼 높이 확장 가능. 되도록 true로 놓는다
수평으로 스크롤 가능한 View
보통 ScrollView 안에 RelativeLayout 또는 LinearLayout(horizontal) 을 넣어 짝을 이룬다
반드시 하나의 자식만 가질 수 있다!!!!
속성은 ScrollView 와 같다
한 화면에 여러개의 스크롤을 사용할 수 있다.
ScrollView 안에 Recycler View 넣어야 할때 자주 사용됨
스크롤을 감지해서 변화를 주어야 할때, CoordinatorLayout / AppBarLayout과 함께 자주 사용됨
ScrollView 와 마찬가지로 하나의 자식만 가질 수 있다!!
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
. . .
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
같은 형태의 아이템을, 내용만 바꿔 여러번 사용해야될때 사용.
총 2가지 XML 파일이 필요하다
우선, RecyclerView 자체가 View로서 존재하기 떄문에, 이를 삽입할 layout이 필요하다
두번째로, 아이템을 RecyclerView에 띄우는 것이므로, 아이템 하나의 형태를정의하는 layout이 필요하다
// RecyclerView 의 XML
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/recycler_test"/>
// Item 의 XML
<?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/recycler_comment_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/img_btn"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@android:color/transparent"/>
</LinearLayout>
ListView의 단점 (View 재활용 없음)

위의 단점들을 보완한것이 RecyclerView 이다
Adapter 가 데이터를 View에 바인딩 한후, 이를 LayoutManager 에게 제공한다
ViewHolder에 화면에 맞는 아이템 뷰의 수만 할당하고, 스크롤할때 화면에서 사라지는 View 객체는, 사라지는게 아닌 위치만 이동하여 재사용 된다. View 객체만 재사용되는 것이지, View 객체 안에 담긴 데이터는 갱신된다 (ViewHolder 의 bind 함수를 통하여 갱신됨)
onCreatViewHolder 를 호출하여 View를 생성, 그후 onBindViewHolder 를 호출하여 View를 바인딩하고, 최종적으로 리턴
앱의 데이터에서 RecyclerView에 표시되는 아이템뷰에 바인딩을 제공. 각 아이템 뷰의 위치를 데이터 소스의 특정 위치에 연결할 수 있다
RecyclerView 내에 아이템을 어떻게 배치할지 설정 가능. Linear 또는 Grid 를 기본으로 사용 가능
기본 애니메이션이 제공되며, 이를 오버라이드하고 변경할 수 있다.
화면에 그리고 싶은 개별적인 아이템의 UI를 그릴 수 있도록 도와줌
implementation "androidx.viewpager2:viewpager2:1.0.0"


//activity_home.xml
<?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">
...
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/home_navigation"
android:layout_width="match_parent"
android:layout_height="60dp"
app:menu="@menu/navigation_home_menu"
app:itemTextColor="@drawable/menu_selector_color"
app:labelVisibilityMode="labeled"/>
</androidx.constraintlayout.widget.ConstraintLayout>
// menu : 메뉴아이콘들을 담은 파일을 연결하는 속성
labelVisibilityMode=”labeled” 로 해결
라벨없는 BtnV 는 “unlabeled” 로 설정
menu=”@menu폴더” 로 해결
메뉴 아이콘 설정 & 클릭시 버튼모양 바뀌는 설정
→ 아이콘 파일 생성
→ Selector 파일 생성
// selector_download_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/download_on" android:state_selected="true"/>
<item android:drawable="@drawable/download_off" android:state_selected="false"/>
</selector>
// selector_game_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/game_on" android:state_selected="true"/>
<item android:drawable="@drawable/game_off" android:state_selected="false"/>
</selector>
// selector_home_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/home_on" android:state_selected="true"/>
<item android:drawable="@drawable/home_off" android:state_selected="false"/>
</selector>
// selector_newhot_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/video_on" android:state_selected="true"/>
<item android:drawable="@drawable/video_off" android:state_selected="false"/>
</selector>
→ menu 파일 생성
// navigation_home_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/selector_home_icon"
android:title="홈" />
<item
android:id="@+id/navigation_game"
android:icon="@drawable/selector_game_icon"
android:title="게임" />
<item
android:id="@+id/navigation_newhot"
android:icon="@drawable/selector_newhot_icon"
android:title="NEW HOT" />
<item
android:id="@+id/navigation_stored"
android:icon="@drawable/selector_download_icon"
android:title="저장된 목록" />
</menu>
.itemIconTintList = null 로 해결
// HomeActivity
override fun onStart() {
val bnView = findViewById<BottomNavigationView>(R.id.home_navigation)
bnView.itemIconTintList = null
}

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"/>
// tabMode 를 scrollable 하게되면, Tab이 화면에 다 안나올정도로 많을때, scroll 가능해진다
// 기본값은 fixed
<com.google.android.material.tabs.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<com.google.android.material.tabs.TabItem
android:text="@string/tab_text"/>
<com.google.android.material.tabs.TabItem
android:icon="@drawable/ic_android"/>
</com.google.android.material.tabs.TabLayout>