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

startActivityForResult(intent, resultCode)

resultCode를 이용해 activity 1 or activity 2에 request code를 보내고

setResult(응답코드, intent)을 이용해 activity로부터 response code를 받는다

finish() method는 request 와 response 처리가 끝난후 activity를 화면애서 없앨때 사용한다

//MainActivity에서 NewActivity로 startActivityForReuslt()를 이용해 requestCode를 보내고

NewActivity에서 setResult()를 이용해 reponseCode를 보낸후 finish()를이용해 화면을 없애준다

MainActivity에서 onActivityResult()를 이용해 responseCode를 처리한다

//onActivityResult(int RequestCode, int ResultCode, intent)

 

activity_main.xml

<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click the button to open new screen"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.288" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="256dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="btnClicked"
android:text="here"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

new_activity.xml

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="360dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="go back to main"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

AndroidManifest.xml

<activity android:name=".Main2Activity"
android:label="new activity"
android:theme="@style/Theme.AppCompat.Dialog"></activity>

Main2Activity.java

public class Main2Activity extends AppCompatActivity {

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

Button button = findViewById(R.id.btn2);
button.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener(){
public void onClick(View view){
Intent intent = new Intent();
intent.putExtra(name: "message", value: "transfered message");
//response
setResult(RESULT_OK, intent);
//activity close
finish();
}
};
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CODE = 111;

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

Button button = findViewById(R.id.btn1);
// button.setOnClickListener(listener);
}
//OnClickListener를 이용할수도있고 btnClicked method를 만들어서 작동할수도있다
// View.OnClickListener listener = new View.OnClickListener(){
// public void onClick(View view){
// Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
// startActivityForResult(intent, REQUEST_CODE);
// }
// };
public void btnClicked(View view){
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
startActivityForResult(intent, REQUEST_CODE);
}
//main화면으로 이동
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == REQUEST_CODE){
Toast.makeText(getApplicationContext(),
"onActivity() call, requestCode:"+requestCode+", responseCode: "+resultCode, Toast.LENGTH_LONG).show();
}
if(resultCode == RESULT_OK){
String responseMsg = data.getStringExtra(name: "message");
Toast.makeText(getApplicationContext(), text: "response message" +responseMsg, Toast.LENGTH_LONG).show();
}
}
}

 

 

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

activity life cycle  (0) 2019.08.14
intent  (0) 2019.08.14
activity 추가하기  (0) 2019.08.13
Recycler View  (0) 2019.08.13
custom dialog  (0) 2019.08.13

새로운 activity를 추가

1. Manifest file에 ativity tag가 자동으로 추가된다

2. 새로운 activity에 layout을 정의 

3. MainActivity에 새 Activity 띄우기(startActivity(), startActivityForResult())

4. 새 Activity에서 처리결과를 보내기(setResult())

5. MainActivity에서 onActivityResult() 를이용해 응답처리

//new\activity\empty activity

activity_main.xml

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="136dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="New activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

activity_main2.xml

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="260dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="new activity"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

AndroidManifest.xml

<activity android:name=".Main2Activity"
android:label = "new activity"
android:theme = "@style/Theme.AppCompat.Dialog.Alert"></activity>

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

Button button = findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
startActivity(intent);
}
});
}
}

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

intent  (0) 2019.08.14
startActivityForResult()(화면전환)  (0) 2019.08.13
Recycler View  (0) 2019.08.13
custom dialog  (0) 2019.08.13
alert dialog  (0) 2019.08.13

activity_main.xml

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>

item_style.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding = "5dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="5dp">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text = "agency name"
android:textSize = "30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="contacts"
android:textColor = "#f0f"
android:textSize = "24sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Agent.java

public class Agent {
String name;
String telNumber;

public Agent(String name, String telNumber){
this.name = name;
this.telNumber = telNumber;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getTelNumber(){
return telNumber;
}
public void setTelNumber(String telNumber){
this.telNumber = telNumber;
}
}

AgentAdapter.java

public class AgentAdapter extends RecyclerView.Adapter<AgentAdapter.ViewHolder>{
ArrayList<Agent> items = new ArrayList<Agent>();

public ViewHolder onCreateViewHolder(ViewGroup vg, int viewType){
LayoutInflater inflater = LayoutInflater.from(vg.getContext());
View itemView = inflater.inflate(R.layout.item_style, vg, false);

//view 객채를 담아서 새로생성된 ViewHolder 객체를 리턴한다
return new ViewHolder(itemView);
}
public void onBindViewHolder(ViewHolder vh, int position){
Agent item = items.get(position);
vh.setItem(item);
}//재사용할때 호출되는 method
public int getItemCount(){
return items.size();
}
public void addItem(Agent item){
items.add(item);
}
public void setItem(ArrayList<Agent> items){
this.items = items;
}
public Agent getItem(int position){
return items.get(position);
}
public void setItem(int position, Agent item){
items.set(position, item);
}
static class ViewHolder extends RecyclerView.ViewHolder{
TextView textView1;
TextView textView2;

public ViewHolder(View itemView){
super(itemView);

textView1 = itemView.findViewById(R.id.textView1);
textView2 = itemView.findViewById(R.id.textView2);
}
//item setting
public void setItem(Agent item){
textView1.setText(item.getName());
textView2.setText(item.getTelNumber());
}
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

RecyclerView recyclerView = findViewById(R.id.recyclerView);

LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);

AgentAdapter adapter = new AgentAdapter();

adapter.addItem(new Agent("busan", "123123123"));
adapter.addItem(new Agent("hoho", "123123123"));
adapter.addItem(new Agent("hoho", "123123123"));
adapter.addItem(new Agent("hoho", "123123123"));
adapter.addItem(new Agent("hoho", "123123123"));

recyclerView.setAdapter(adapter);
}
}

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

startActivityForResult()(화면전환)  (0) 2019.08.13
activity 추가하기  (0) 2019.08.13
custom dialog  (0) 2019.08.13
alert dialog  (0) 2019.08.13
방향전환 event로 activity 유지하기  (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

Manifest에 configChanges 속성을 설정한후

MainActivity에서 onConfigurationChanged() method를 overriding해준다

//manifests안에 AndroidManifest.xml파일에서 activity tag안에서 configChanges속석을 설정한다

AndroidManifest.xml

<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden">
<!--android:screenOrientation = "landscape"
가로상태로 고정되어 변하지 않는다-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

activity_main.xml

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="try to change the view direction"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onConfigurationChanged(Configuration configuration){
super.onConfigurationChanged(configuration);

if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE){
displayToast("landscape");
}else if(configuration.orientation == Configuration.ORIENTATION_PORTRAIT){
displayToast("portrait");
}
}
public void displayToast(String message){
Toast.makeText(context: this, message, Toast.LENGTH_LONG).show();
}
}

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

custom dialog  (0) 2019.08.13
alert dialog  (0) 2019.08.13
방향전환시 상태값 저장  (0) 2019.08.12
xml을 이용한 focus event  (0) 2019.08.12
touch event, gesture event  (0) 2019.08.12

res/new/android resource directory를 이용해

layout-land를 생성후 그 폴더안에 activity_main.xml을 생성

//가로방향으로 전환될때 사용하는 폴더

//onSaveInstanceState()를 호출하면서 전황시 상태값이 저장된다

activity_main.xml

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

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="41dp"
android:layout_marginBottom="29dp"
android:ems="10"
android:hint="text"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="166dp"
android:layout_marginLeft="166dp"
android:layout_marginTop="29dp"
android:layout_marginEnd="157dp"
android:layout_marginRight="157dp"
android:text="check"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

activity_main.xml

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="horizontal"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="text"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="242dp" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
tools:layout_editor_absoluteX="344dp"
tools:layout_editor_absoluteY="313dp"/>

MainActivity.java

public class MainActivity extends AppCompatActivity {
String inputText;
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayToast("onCreate() call");

//상태값 저장
editText = findViewById(R.id.editText);
button = findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
inputText= editText.getText().toString();
Toast.makeText(getApplicationContext(), "saved"+inputText, Toast.LENGTH_LONG).show();
} //onClick method를 overriding해준다
});
if(savedInstanceState != null){
inputText = savedInstanceState.getString("inputText");

Toast.makeText(getApplicationContext(), "saved value:" +inputText, Toast.LENGTH_LONG).show();
}
}

protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);

outState.putString("inputText", inputText);
}

// protected void onStart(){
// super.onStart();
// displayToast("onStart() call");
// }
protected void onStop(){
super.onStop();
displayToast("onStart() call");
}
protected void onDestroy(){
super.onDestroy();
displayToast("onDestroy() call");
}

public void displayToast(String message){
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}

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

alert dialog  (0) 2019.08.13
방향전환 event로 activity 유지하기  (0) 2019.08.13
xml을 이용한 focus event  (0) 2019.08.12
touch event, gesture event  (0) 2019.08.12
카드뷰(CardView)  (0) 2019.08.12

+ Recent posts