intent는 activity와 acivity사이의 data를 전달하기위해 사용된다

intent를 이용한 method에는 startActivity(), startActivityForResult()

startService(), bindServie(), broadcastIntent()가있다

 

intent의 기본구성요소에는 Action과 Data가 있다

Action은 수행기능을 의미하고 Data는 수행할대상의 data이다

Action에는 ACTION_VIEW, ACTION_EDIT가 있다

 

Intent 안에 여러 유형의 Data가있는데

system에서 data의 format에 맞게 activity를 보여준다

 

다양한 방법으로 Intent를 생성할수 있다

Intent()

Intetnt(Intent *)

Intent(action, uri)

Intent(context, class)

Intetnt(string action, uri, context pacakage, class())

 

명시적(Explicit) Intent는 class 객체나 컴포넌트 이름을 지정함으로 호출대상이 명료해지는경우

암시적(Implicit) Intent는 수행할 Action과 Data를 지정했더라도 호출대상이 지정되어있지않다

암시적 Intent의 anction, data는 MIME Type에의해 자동으로 activity를 찾아서 띄운다

암시적 Intent의 다른속성으로는 부가 데이터(Extras)가있고 getExtra, putExtra를 통해 data를 이동시킨다 

activity_main.xml

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint = "put the telephone(000-0000-0000)"
android:textSize = "20sp"
android:padding = "10dp"
android:layout_marginTop = "100dp"
android:layout_marginBottom="40dp"/>

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="after putting telephone, click the below button"
android:textSize = "15sp"
android:layout_marginBottom = "10dp"/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "call"
android:textSize="20sp"
android:textStyle="bold"
android:onClick = "clickBtn1"
android:layout_gravity="center_horizontal"/>

MainActivity.java

public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;

public static final int REQUEST_CODE=111;

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

editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
}
public void clickBtn1(View view){
String telNumber = editText.getText().toString();

// 암시적(implicit) Intent
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(telNumber));
startActivity(intent);

/* 명시적 intent 1.호출대상 객체(class)를 지정
Intent intent = new Intent(getApplicationContext(), Main2Activity.class)
startActivityForResult(intent, REQUEST_CODE);
*/
/* 명시적 intent 2.
Intent intent = new Intent();
ComponentName componentName = new ComponentName("com.example.myapplication", "come.example.myapplication.Main2Activity");

intent.setComponent(componentName);
startActivityForResult(intent, REQUEST_CODE);
*/
}
}

 

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

android internet check  (0) 2019.09.05
activity life cycle  (0) 2019.08.14
startActivityForResult()(화면전환)  (0) 2019.08.13
activity 추가하기  (0) 2019.08.13
Recycler View  (0) 2019.08.13

activity_main.xml

<Button
android:id="@+id/btn01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="AlertDialog" />

<Button
android:id="@+id/btn02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="CustomDialog" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="hahahohohehe"
android:textSize="20dp"/>
<TextView
android:id="@+id/tv01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold"/>
</LinearLayout>

custom.xml

<ImageButton
android:id="@+id/imgBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/sym_def_app_icon"/>
<EditText
android:id="@+id/editText01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint = "put expected one!!!"/>

MainActivity.java

public class MainActivity extends AppCompatActivity {
Button btn_alert;
Button btn_custom;
TextView textView;

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

btn_alert = findViewById(R.id.btn01);
btn_custom = findViewById(R.id.btn02);
textView = findViewById(R.id.tv01);

btn_alert.setOnClickListener(listener);
btn_custom.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener(){
public void onClick(View v){
switch (v.getId()){
case R.id.btn01:
new AlertDialog.Builder(MainActivity.this)
.setTitle("aabbccddeefff")
.setMessage("12312312313")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("YES", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
Toast.makeText(getBaseContext(), "YES", Toast.LENGTH_LONG).show();
textView.setText("ILikeEliphant");
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
Toast.makeText(getBaseContext(), "NO", Toast.LENGTH_LONG).show();
textView.setText("choose again");
}
})
.show();
break;
case R.id.btn02:
LinearLayout linearLayout = (LinearLayout)View.inflate(MainActivity.this, R.layout.custom, null);
final EditText editText = linearLayout.findViewById(R.id.editText01);

new AlertDialog.Builder(MainActivity.this)
.setTitle("hehehehehehehe")
.setView(linearLayout)
.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
Toast.makeText(getBaseContext(), "OK", Toast.LENGTH_LONG).show();
String winner = editText.getText().toString();
textView.setText(winner);
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
Toast.makeText(MainActivity.this, "CANCEL", Toast.LENGTH_LONG).show();
}
})
.show();
break;
}
}
};
}

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

activity 추가하기  (0) 2019.08.13
Recycler View  (0) 2019.08.13
alert dialog  (0) 2019.08.13
방향전환 event로 activity 유지하기  (0) 2019.08.13
방향전환시 상태값 저장  (0) 2019.08.12

--alert dialog--

activity_main.xml

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="open alert dialog"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="check"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;

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

textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
showDialog();
}
});
}
private void showDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("alert");
builder.setMessage("wanna finish?");
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setPositiveButton("yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
String msg = "you have put yes";
textView.setText(msg);
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn) {
String msg = "you have put no";
textView.setText(msg);
}
});
builder.setNeutralButton("cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int btn){
String msg = "you have put cancel";
textView.setText(msg);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}

--progress bar--

activity_main.xml

<ProgressBar
android:id = "@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text = "check message"
android:layout_weight="1"/>
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="close"
android:layout_weight="1"/>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
ProgressDialog dialog;

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

ProgressBar progressBar = findViewById(R.id.progressBar);
progressBar.setProgress(40);

Button button = findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("checking the progress");
dialog.show();
}
});
Button button2 = findViewById(R.id.btn2);
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if(dialog != null){
dialog.dismiss();
}
}
});
}
}

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

Recycler View  (0) 2019.08.13
custom dialog  (0) 2019.08.13
방향전환 event로 activity 유지하기  (0) 2019.08.13
방향전환시 상태값 저장  (0) 2019.08.12
xml을 이용한 focus event  (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

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

+ Recent posts