Android ListView에 바닥글(Footer) 추가하기
이 튜토리얼에서는 Android 앱의 ListView 하단에 바닥글(Footer)을 추가하는 방법을 단계별로 살펴봅니다. 바닥글을 활용하면 목록 끝에 저작권 표기, '더 보기' 버튼, 로딩 인디케이터 등 다양한 요소를 손쉽게 배치할 수 있습니다.
1단계 — 새 프로젝트 생성
Android Studio에서 File → New Project를 선택하고, 프로젝트 생성에 필요한 모든 정보를 입력하여 새 프로젝트를 만듭니다.
2단계 — 메인 레이아웃 작성(activity_main.xml)
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" > </ListView> </RelativeLayout>
3단계 — 바닥글 레이아웃 파일 생성(listview_footer.xml)
새 레이아웃 파일을 하나 만들고 이름을 listview_footer.xml로 지정한 뒤, 다음 코드를 입력합니다.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Footer: End of the ListView!" android:textAppearance="?android:attr/textAppearanceLarge" android:padding="16sp" /> </LinearLayout>
4단계 — MainActivity.java 코드 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listView;
String[] country = new String[] {
"India",
"Australia",
"South Africa",
"North America",
"South America",
"Iceland",
"GreenLand"
};
List<String> LISTSTRING;
LayoutInflater layoutInflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
LISTSTRING = new ArrayList<>(Arrays.asList(country));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, LISTSTRING);
layoutInflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) layoutInflater.inflate(R.layout.listview_footer, listView, false);
listView.addFooterView(footer);
listView.setAdapter(adapter);
}
}
핵심 포인트: addFooterView()는 반드시 setAdapter()를 호출하기 전에 실행해야 합니다. 호출 순서가 바뀌면 일부 기기나 이전 Android 버전에서 바닥글이 정상적으로 표시되지 않거나 오류가 발생할 수 있습니다.
5단계 — AndroidManifest.xml 설정
androidManifest.xml 파일에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 볼 차례입니다. 실제 Android 스마트폰을 컴퓨터에 연결했다고 가정하겠습니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 상단 툴바의 Run(실행) 아이콘을 클릭하세요. 실행 대상으로 자신의 모바일 기기를 선택하면, 기기 화면에서 ListView 맨 아래에 바닥글이 함께 표시되는 것을 확인할 수 있습니다.
