Monday, February 8, 2021

Unit 5 Chapter 3Android Intro Hello World Button

package com.example.hellobuttonweek5;//this line is unique to your app
import androidx.appcompat.app.AppCompatActivity;

import android.content.res.Resources;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

}

private void initButton() {
Resources res= getResources();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText firstName = (EditText) findViewById(R.id.editTextTextPersonName);
TextView textView = (TextView) findViewById(R.id.textView);
String displayString=res.getString(R.string.hello,firstName.getText().toString());
textView.setText(displayString);
}
});
}

private void initButtonClear() {
Button clear = (Button) findViewById(R.id.button2);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText firstName = (EditText) findViewById(R.id.editTextTextPersonName);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Hello World!");
firstName.setText("");
}
});

}
}



Update 9/20/2021:

If you are on the latest version of Android Studio (Android Studio Arctic Fox Version 2020.3.1) and you are on JAVA version 11 or early you may get the following error when executing your (Newly created) Android application:

> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

SOLUTION:Make sure you load Java11 or later (I have version 15) -

Open the Project get to Files->Settings->Build,Execution,Deployment->Build Tools->Gradle-> Use the drop down for Gradle JDK on that page and select a Java version 11 or later (Which you may have just loaded). I picked JDK 15.

  





No comments:

Post a Comment

Note: Only a member of this blog may post a comment.