activity_main.xml

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="focus event"
android:textSize="20dp"
android:background="#ff0"
android:gravity="center"
android:padding = "10dp"/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="put id"
android:padding="10dp"
android:background="@drawable/focus_selector"
android:textColorHint = "#ff0"
android:textSize="20dp"/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="put password"
android:padding="10dp"
android:background="@drawable/focus_selector"
android:textColorHint = "#ff0"
android:textSize="20dp"/>

focus_selector.xml

//res\drawable

<item android:state_focused = "true"
android:drawable="@drawable/red"/>
<item android:drawable="@drawable/blue"/>

MainActivity.java

'android studio' 카테고리의 다른 글

방향전환 event로 activity 유지하기  (0) 2019.08.13
방향전환시 상태값 저장  (0) 2019.08.12
touch event, gesture event  (0) 2019.08.12
카드뷰(CardView)  (0) 2019.08.12
버튼 style변경(onDraw(), invalidate())  (0) 2019.08.12

activity_main.xml

<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_dark"/>
<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_light"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>

MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView textView;

GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView);

View view = findViewById(R.id.view1);

view.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent mE){
int action = mE.getAction();//어떤 action인지 알기위해
float x = mE.getX();
float y = mE.getY();

if(action == MotionEvent.ACTION_DOWN){
output("touched"+ x +", "+y+"\n");
//append는 계속해서 내용을 추가한다
}else if(action == MotionEvent.ACTION_MOVE){
output("moved"+ x +", "+y+"\n");
}else if(action == MotionEvent.ACTION_UP){
output("gone"+ x +", "+y+"\n");
}
return true;
}
});

//getsture event
View view2 = findViewById(R.id.view2);
view2.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent mE){
gestureDetector.onTouchEvent(mE);
return true;
}
});
gestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener(){
public boolean onDown(MotionEvent mE){
output("onDown() call");
return true;
}
//화면에 터치했다가 떨어질때 발생하는 event
public void onShowPress(MotionEvent mE){
output("onShowPress() call");
}
//한손으로 터치했다 떨어지는 event 처리
public boolean onSingleTapUp(MotionEvent mE){
output("onSingleTapUp() call");
return true;
}
//손가락으로 오랫동안 터치하는 event 처리
public void onLongPress(MotionEvent mE){
output("onLongPress() call");
}
//터치된 채 일정한 속도와 방향으로 움직였다 떨어지는 event
public boolean onScroll(MotionEvent mE, MotionEvent mE2, float distanceX, float distanceY){
output("onScroll() call"+distanceX+", "+distanceY);
return true;
}
//터치된채 가속도를 내서 움직였다 떨어지는 event처리
public boolean onFling(MotionEvent mE, MotionEvent mE2, float vX, float vY){
output("onFling() call"+vX+", "+vY);
return true;
}
});
}
public void output(String data){
textView.append(data+"\n");
}
}

'android studio' 카테고리의 다른 글

방향전환시 상태값 저장  (0) 2019.08.12
xml을 이용한 focus event  (0) 2019.08.12
카드뷰(CardView)  (0) 2019.08.12
버튼 style변경(onDraw(), invalidate())  (0) 2019.08.12
toast message, style  (0) 2019.08.11

activity_main.xml

<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="icon change"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="icon change"/>

<com.example.myapplication.Layout01
android:id="@+id/layout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

layout.xml

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor = "#eeeeee"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="10dp"
app:srcCompat="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
android:textSize = "26sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="contents"
android:textColor="#00f"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>

Layout01.java

public class Layout01 extends LinearLayout {
ImageView imageView;
TextView textView;
TextView textView2;

public Layout01(Context context){
super(context);
init(context);
//생성자가 생성될때 inflation이 같이 되도록 하기위해
}//생성자
public Layout01(Context context, AttributeSet attrs){
super(context, attrs);
init(context);
}//xml속성을 활용하기위해

private void init(Context context){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.layout, root: this, attachToRoot: true);
imageView = findViewById(R.id.imageView);
textView = findViewById(R.id.textView);
textView2 = findViewById(R.id.textView2);
} //초기화작업을 생성자를 통해 하기위해 init method를 이용한다
public void setImage(int resId){
imageView.setImageResource(resId);
}
public void setTitle(String title){
textView.setText(title);
}
public void setComment(String comment){
textView2.setText(comment);
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

Layout01 layout01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

layout01 = findViewById(R.id.layout01);
layout01.setImage(R.drawable.ic_launcher_foreground);
layout01.setTitle("hahahoho");
layout01.setComment("hihihihi");

Button btn = findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
layout01.setImage(R.drawable.icon);
}
});
Button btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
layout01.setImage(R.drawable.icon);
}
});
}
}

'android studio' 카테고리의 다른 글

xml을 이용한 focus event  (0) 2019.08.12
touch event, gesture event  (0) 2019.08.12
버튼 style변경(onDraw(), invalidate())  (0) 2019.08.12
toast message, style  (0) 2019.08.11
inflation  (0) 2019.08.11

onDraw()는 view가 화면에 보일때 자동으로 호출되는

method로 재정의를 통해 Button의 style을 지정할수있다

 

invalidate()는 onDraw() method를 호출하면서 

Button을 이동한 좌표에 지정된 style을 적용할수있다

 

activity_main.xml

<!--파일 위치를 불러온다-->
<com.example.myapplication.DemoButton
android:id="@+id/btn"
android:layout_width="250dp"
android:layout_height="100dp"
android:text="button style change"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

dimens.xml

//res\values\dimens.xml

<dimen name="text_size">10sp</dimen>
<!--getResources() method를 이용해 Resource 객채를 얻어오고
Resource 객체에 getDimension() method를 이용해 참조할수있다
setTextSize() method를 이용해 float pixel값으로 return받는다 -->

DemoButton.java

public class DemoButton extends AppCompatButton {

//소스코드에서 new연산자를 이용하여 생성하는경우 사용하는 생성자
public DemoButton(Context context){
super(context);

init(context);
} //생성자 생성
//안드로이드 UI 객체를 생성시에는
//반드시 Context를 인자로 사용한다

//view를 xml layout에 추가하는경우 사용하는 생성자
public DemoButton(Context context, AttributeSet attrs){
super(context, attrs);

init(context);
} //생성자 생성
//AttributeSet을통해 xml에 추가된 속성을 전달 받는다

//초기화를 위한 method
private void init(Context context){
setBackgroundColor(Color.CYAN);
setTextColor(Color.GREEN);
//text size 지정
float size = getResources().getDimension(R.dimen.text_size);
setTextSize(size);
}
//view가 화면에 그려질때 자동으로 호출되는 method
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Log.d("DemoButton", "onDraw()");
//onDraw호출되는 시점을 출력하기위해
}
//view가 터치될때 호출되는 method
public boolean onTouchEvent(MotionEvent event){
Log.d("DemoButton", "onTouchEvent()");
int action = event.getAction();

switch(action){
case MotionEvent.ACTION_DOWN:
setBackgroundColor(Color.GREEN);
setTextColor(Color.MAGENTA);
break;
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
setBackgroundColor(Color.MAGENTA);
setTextColor(Color.GREEN);
break;
}
invalidate();
//설정된 값으로 다시 그려준다(onDraw() 호출)
return true;
}
}

 

'android studio' 카테고리의 다른 글

touch event, gesture event  (0) 2019.08.12
카드뷰(CardView)  (0) 2019.08.12
toast message, style  (0) 2019.08.11
inflation  (0) 2019.08.11
GridView  (0) 2019.08.11

activity_main.xml

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="26sp"
android:hint="x value"
android:inputType="numberSigned"/>
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="26sp"
android:hint="y value"
android:inputType="numberSigned"/>
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="toast meesage"
android:textSize="20sp"
android:onClick="onBtnClicked"/>
</LinearLayout>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "toast shape change"
android:onClick="onBtn2Clicked"
android:layout_gravity="center"
android:textSize="20sp"/>

toast_boarder.xml

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:padding="10dp"
android:background="@drawable/toast_background"/>

toast_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="5dp"
android:color="#00f"/>
<solid
android:color="#0ff"/>
<padding
android:left="20dp"
android:top="20dp"
android:right="20dp"
android:bottom="20dp"/>
<corners
android:radius="20dp"/>
</shape>

MainActivity.java

public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;

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

editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
}
public void onBtnClicked(View v){
try {
Toast toast = Toast.makeText(context: this, text: "hahaha", Toast.LENGTH_LONG);
int xValue = Integer.parseInt(editText1.getText().toString());
int yValue = Integer.parseInt(editText2.getText().toString());
//입력된 좌표값을 가져온다
toast.setGravity(gravity: Gravity.TOP| Gravity.TOP, xValue, yValue);
toast.show();
}catch(NumberFormatException e){
e.printStackTrace();
}
}
//toast message 모양 바꾸기
public void onBtn2Clicked(View v){
LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.toast_boarder, (ViewGroup)findViewById(R.id.toast_layout));

TextView textView = layout.findViewById(R.id.textView);

Toast toast = new Toast(context: this);
textView.setText("style changed toast");
toast.setGravity(Gravity.CENTER, xOffset: 0, yOffset: -50);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);

toast.show();
}
}

'android studio' 카테고리의 다른 글

카드뷰(CardView)  (0) 2019.08.12
버튼 style변경(onDraw(), invalidate())  (0) 2019.08.12
inflation  (0) 2019.08.11
GridView  (0) 2019.08.11
Spinner  (0) 2019.08.10

layout은 화면구성을 정의해놓은 파일로

화면에 보여지도록 하기위해 java code가 필요하다

*.xml에서 정의한 layout을 Inflater를 통해 객체화를하고

객체화를 한후 *.java를 통해 화면에 보이도록 한다.

 

setContentView(R.layout.*) method를통해

화면에 보여줄 view를 지정(매개변수) 하고

객체화(memory loading)을 한다

R은 app/res를 의미한다

 

setContentView 는 전체화면만 객체화를하고

LayInflater를 통해 부분작업 객체화를 한다

activity_main.xml

<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inflation test"
android:layout_marginTop = "30dp"
android:textSize = "30sp"
android:textColor = "#a63"
android:layout_gravity = "center"/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text = "add layout"
android:textSize = "30sp"
android:onClick="onClickedBtn"/>
<LinearLayout
android:id="@+id/addLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>

add_layout.xml

<Button
android:id="@+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "test"
android:textSize = "20sp"
android:textStyle = "bold"/>
<RadioGroup
android:id="@+id/radioGroup"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<RadioButton
android:id="@+id/radioBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sport"
android:textColor="#00f"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginRight="5dp"/>
<RadioButton
android:id="@+id/radioBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="travel"
android:textColor="#00f"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginRight="5dp"/>
<RadioButton
android:id="@+id/radioBtn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reading"
android:textColor="#00f"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginRight="5dp"/>
<RadioButton
android:id="@+id/radioBtn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gaming"
android:textColor="#00f"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginRight="5dp"/>
</RadioGroup>
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_margin="10dp"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="accept"
android:textSize="20sp"
android:layout_margin="10dp"
android:textColor="#f00"/>
<CheckBox
android:id="@+id/agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//전체화면에 대한 뷰를 지정하고, inflation을 한다
}
//when button is clicked, add layout
public void onClickedBtn(View v){
addLayout();
}
//add_layout.sml을 activity_main.xml에 추가하는 기능의 method
private void addLayout(){
LinearLayout parentLayout = findViewById(R.id.addLayout);

//inflation
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.add_layout, parentLayout, true);

Button btnTest = findViewById(R.id.btnTest);
final CheckBox agree = findViewById(R.id.agree);

btnTest.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(agree.isChecked()){
agree.setChecked(false);
}else{
agree.setChecked(true);
}
}
});
}
}

'android studio' 카테고리의 다른 글

버튼 style변경(onDraw(), invalidate())  (0) 2019.08.12
toast message, style  (0) 2019.08.11
GridView  (0) 2019.08.11
Spinner  (0) 2019.08.10
AdapterView(ListView)  (0) 2019.08.10

activity_main.xml

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:padding="10dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:gravity="center">
</GridView>

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
//BaseAdapter를 상속받는다
private Context mContext;
private int[] data;

public CustomAdapter(Context mContext, int[] data){
this.mContext = mContext;
this.data = data;
}

public int getCount(){
return data.length;
}//getCount method를 통해 data의 갯수를 얻어온다
public Object getItem(int position){
return data[position];
}
public long getItemId(int position){
return position;
}

public View getView(int position, View convertView, ViewGroup parent){
ImageView imageView;

if(convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(w: 150, h: 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(left: 5, top: 5, right: 5, bottom: 5);
}else{
imageView = (ImageView)convertView;
}
imageView.setImageResource(data[position]);
return imageView;
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private int[] imgData = {R.drawable.apple, R.drawable.apple,
R.drawable.apple, R.drawable.apple, R.drawable.apple, R.drawable.apple,
R.drawable.apple, R.drawable.apple, R.drawable.apple, R.drawable.apple,
R.drawable.apple, R.drawable.apple, R.drawable.apple, R.drawable.apple};
private CustomAdapter adapter;
private GridView gridView;

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

adapter = new CustomAdapter(mContext: this, imgData);

gridView = findViewById(R.id.gridView);
gridView.setAdapter(adapter);
}
}

--------------------

activity_main.xml

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:padding="10dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:gravity="center">
</GridView>

Grid_item.java

public class Grid_item {
String apple;
int resId;

public Grid_item(String apple, int resId){
this.apple = apple;
this.resId = resId;
}

public String getApple(){
return apple;
}
public void setApple(String apple){
this.apple = apple;
}
public int getResId(){
return resId;
}
public void setResId(int resId){
this.resId = resId;
}
}

item_style.xml

<ImageView
android:id="@+id/imgView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/apple"/>
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="apple"
android:textColor="#5505ff"
android:textStyle="bold"
android:layout_gravity="center"/>

GridItemView.java

public class GridItemView extends LinearLayout {
ImageView imageView;
TextView textView;

public GridItemView(Context context){
super(context);
init(context);
}
public void init(Context context){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.item_style, this, true);

imageView = findViewById(R.id.imgView);
textView = findViewById(R.id.txtView);
}
public void setImage(int resId){
imageView.setImageResource(resId);
}
public void setApple(String apple){
textView.setText(apple);
}
}

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
//BaseAdapter를 상속받는다
private Context mContext;
ArrayList<Grid_item> data = new ArrayList<Grid_item>();

public CustomAdapter(Context mContext){
this.mContext = mContext;
}

public void addItem(Grid_item grid_item){
data.add(grid_item);
}

public int getCount(){
return data.size();
}//getCount method를 통해 data의 갯수를 얻어온다
public Object getItem(int position){
return data.get(position);
}
public long getItemId(int position){
return position;
}

public View getView(int position, View convertView, ViewGroup parent){

GridItemView view = new GridItemView(mContext);
Grid_item item = data.get(position);

view.setImage(item.getResId());
view.setApple(item.getApple());

return view;
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private CustomAdapter adapter;
private GridView gridView;

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

gridView = findViewById(R.id.gridView);

adapter = new CustomAdapter(this);
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));
adapter.addItem(new Grid_item("apple", R.drawable.apple));

gridView.setAdapter(adapter);
}
}

'android studio' 카테고리의 다른 글

toast message, style  (0) 2019.08.11
inflation  (0) 2019.08.11
Spinner  (0) 2019.08.10
AdapterView(ListView)  (0) 2019.08.10
view에 도형, 텍스트 그리기  (0) 2019.08.10

activity_main.xml

<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80f0"
android:gravity="center"
android:text="country"
android:textSize="30dp" />
<Spinner
android:id="@+id/sp01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" />

MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView textView;
Spinner spinner;
ArrayAdapter<String> adapter = null;

String[] items = {"korea", "china", "japan", "france"};

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

textView = findViewById(R.id.txt);
spinner = findViewById(R.id.sp01);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);
}
}

---seperate data---

activity_main.xml

<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80f0"
android:gravity="center"
android:text="country"
android:textSize="30dp" />
<Spinner
android:id="@+id/sp01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" />

country.xml

<string-array name="country">
<item>korea</item>
<item>china</item>
<item>japan</item>
<item>france</item>
</string-array>

MainActivity.java

public class MainActivity extends AppCompatActivity {

Spinner spinner;
ArrayAdapter<CharSequence> adapter = null;

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

spinner = findViewById(R.id.sp01);
adapter = ArrayAdapter.createFromResource(this, R.array.country, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);
}
}

'android studio' 카테고리의 다른 글

inflation  (0) 2019.08.11
GridView  (0) 2019.08.11
AdapterView(ListView)  (0) 2019.08.10
view에 도형, 텍스트 그리기  (0) 2019.08.10
Button, RadioButton, CheckBox, ImageView, ImageButton  (0) 2019.08.10

+ Recent posts