profile image

L o a d i n g . . .

반응형


안드로이드 스피너에 이미지 넣기

Add the images to Spinner


스피너를 이용하면 드랍다운이 생겨 여러가지 선택지중 하나를 선택하는 것이 가능하다.
웹으로치면 드랍다운 버튼이다.
여기에 텍스트를 넣을 수도 있지만 이미지를 넣어 선택하게 할 수도 있다.

Use spinner, it's possible to choose one of several one of several options
It's dropdown button on the web
Spinner can put text, also add images

<Spinner
android:id="@+id/emotion_spinner"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@android:color/transparent"/>

xml은 간단하게 spinner만 있다.

배경을 투명으로 한 이유는

배경을 안 넣어버리면 빈공간으로 되어 레이아웃이 이상하게 되고

배경을 넣어버리면 선택되는 이미지랑 겹쳐버리기 때문이다


Xml is simply have Spinner

The reason transparent the background is

If i don't put the background in it'll be empty, making the layout weird

If i put the background in, because it overlaps with image chosen.


<?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="60dp"
android:layout_height="60dp" />
<!--Make sure image is present in Drawable folder-->

</LinearLayout>

spinner에 들어갈 이미지 xml이다.


Add the image xml in spinner


public class EmotionCustomAdapter extends BaseAdapter {

Context context;
int flags[];
LayoutInflater inflater;

public EmotionCustomAdapter(Context applicationContext, int[] flags) {
this.context = applicationContext;
this.flags = flags;
inflater = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
return flags.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

view = inflater.inflate(R.layout.custom_emotion_items, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
icon.setImageResource(flags[i]);
return view;
}
}

spinner에 들어갈 이미지를 담아둘 adapter을 하나 만들도록 한다.

여기서 위의 xml은 인플레이트를 이용하여 메모리에 올리도록 한다.


Create an adapter to hold images for the spinner

Where the above xml is to be placed in memory using an inflate


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

public class DiaryActivity extends AppCompatActivity
implements AdapterView.OnItemSelectedListener{

private Spinner emotion_Spinner;
private Spinner weather_Spinner;

int emotion[] = {R.drawable.emotion, R.drawable.icon1, R.drawable.icon2, R.drawable.icon3, R.drawable.icon4};
int weather[] = {R.drawable.weather, R.drawable.weather1, R.drawable.weather2, R.drawable.weather3};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diary);

emotion_Spinner = (Spinner) findViewById(R.id.emotion_spinner);
weather_Spinner = (Spinner) findViewById(R.id.weather_spinner);

emotion_Spinner.setOnItemSelectedListener(this);
weather_Spinner.setOnItemSelectedListener(this);

EmotionCustomAdapter EmotioncustomAdapter =
new EmotionCustomAdapter(getApplicationContext(), emotion);
emotion_Spinner.setAdapter(EmotioncustomAdapter);

WeatherCustomAdapter WeathercustomAdapter =
new WeatherCustomAdapter(getApplicationContext(), weather);
weather_Spinner.setAdapter(WeathercustomAdapter);
}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {}

@Override
public void onNothingSelected(AdapterView<?> parent) {}

}

클래스에서 implements apdaterView.OnItemSelectedListener을 한다.

두개 onItemSelected와 onNothingSelected를 오버라이딩 해준다

spinner의 아이디를 가져온 후 위에서 만든 아답터를 인스턴스한다.

그리고 스피너에 setAdapter을 이용하여 아답터를 붙여주도록 한다.


implements apdaterView.OnItemSelectedListener in class

Overriding the mothod that onItemSelected and onNothingSelected

Import the ID od the spinner and instantiate the adapter created above

Then, attach the adapter to the spinner using the setAdapter




The pursuit of Happyness

반응형
복사했습니다!