커스텀 스피너 (Custom Spinner)
1. 레이아웃 추가
<Spinner android:id="@+id/simpleSpinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /> |
2. custom_spinner_items.xml 레이아웃 생성
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/imageView" android:layout_width="50dp" android:layout_height="50dp" android:padding="5dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Demo" android:textColor="#000" /> </LinearLayout> |
3. 스피너 추가할 화면의 java 파일
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{ String[] countryNames={"India","China","Australia","Portugle","America","New Zealand"}; int flags[] = {R.drawable.india, R.drawable.china, R.drawable.australia, R.drawable.portugle, R.drawable.america, R.drawable.new_zealand}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner spin = (Spinner) findViewById(R.id.simpleSpinner); spin.setOnItemSelectedListener(this); CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),flags,countryNames); spin.setAdapter(customAdapter); } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) { Toast.makeText(getApplicationContext(), countryNames[position], Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } } |
4. CustomAdapter.java 생성
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class CustomAdapter extends BaseAdapter { Context context; int flags[]; String[] countryNames; LayoutInflater inflter; public CustomAdapter(Context applicationContext, int[] flags, String[] countryNames) { this.context = applicationContext; this.flags = flags; this.countryNames = countryNames; inflter = (LayoutInflater.from(applicationContext)); } @Override public int getCount() { return flags.length; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { view = inflter.inflate(R.layout.custom_spinner_items, null); ImageView icon = (ImageView) view.findViewById(R.id.imageView); TextView names = (TextView) view.findViewById(R.id.textView); icon.setImageResource(flags[i]); names.setText(countryNames[i]); return view; } } |
[출처] https://abhiandroid.com/ui/custom-spinner-examples.html
'프로그래밍 > Android (Java)' 카테고리의 다른 글
레이어가 겹칠때 하단 레이어는 터치 비활성화 시키기 (0) | 2019.12.24 |
---|---|
어플에 애드몹 광고 넣기 예제 (0) | 2019.12.20 |
ImageView 모서리 둥글게 하기 (0) | 2019.10.29 |
웹 브라우저 링크로 앱 실행 방법 (유사 딥링크, 동적링크) (0) | 2019.09.30 |
android 에서 기기에 저장된 파일 이름 변경하기 (0) | 2019.09.20 |