-
Notifications
You must be signed in to change notification settings - Fork 48
Source
Ken Kousen edited this page Oct 16, 2018
·
3 revisions
MainActivity.java
package com.oreilly.hellosafari;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "XYZ";
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
Button helloButton = findViewById(R.id.helloButton);
// helloButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// sayHello(v);
// }
// });
helloButton.setOnClickListener(this::sayHello);
Log.i(TAG, "onCreate");
if (savedInstanceState != null) {
textView.setText(savedInstanceState.getString("greeting"));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("greeting", textView.getText().toString());
}
public void sayHello(View v) {
String name = editText.getText().toString();
textView.setText(String.format("Hello, %s!", name));
Intent intent = new Intent(this, WelcomeActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, name);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.say_hello:
sayHello(null);
return true;
case R.id.about:
Toast.makeText(this, "A simple Hello, World app", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart");
}
}WelcomeActivity.java
package com.oreilly.hellosafari;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class WelcomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Intent intent = getIntent();
String name = intent.getStringExtra(Intent.EXTRA_TEXT);
TextView welcomeView = findViewById(R.id.welcomeText);
welcomeView.setText(String.format("Hello, %s!", name));
}
// Send message via implicit intent
public void shareMessage(String message) {
// Create the text message with a string
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setType("text/plain");
// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendIntent);
}
}
// Open web site via implicit intent
private void viewSite() {
Intent intent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://www.safaribooksonline.com"));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.welcome, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
Toast.makeText(this, "A simple Hello, World app", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Hello World!" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="@string/name_hint"
tools:ignore="Autofill" />
<Button
android:id="@+id/helloButton"
android:text="@string/hello_button_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="sayHello"
android:text="@string/hi_button_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>activity_welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WelcomeActivity">
<TextView
android:id="@+id/welcomeText"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>strings.xml
<resources>
<string name="app_name">HelloSafari</string>
<string name="name_hint">Please enter your name</string>
<string name="hello_button_name">Say Hello</string>
<string name="hi_button_label">Say Hi</string>
</resources>AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.oreilly.hellosafari">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".WelcomeActivity"
android:parentActivityName=".MainActivity" />
</application>
</manifest>main.xml (in
src/main/res/menu)<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/say_hello"
android:title="@string/say_hello"
android:icon="@android:drawable/ic_media_play"
app:showAsAction="ifRoom|withText" />
<item android:id="@+id/about"
android:title="@string/about_app"
app:showAsAction="never" />
</menu>welcome.xml (in
src/main/res/menu)<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/about"
android:title="@string/about_app"
app:showAsAction="never" />
</menu>