Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created May 25, 2015 23:16
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save udacityandroid/97f10520ed4ed662cf77 to your computer and use it in GitHub Desktop.
Save udacityandroid/97f10520ed4ed662cf77 to your computer and use it in GitHub Desktop.
Android Development for Beginners : Healthy Living Example
public class MainActivity extends AppCompatActivity {
int healthLevel;
String message;
...
public void yes(View view) {
healthLevel = healthLevel + 1;
message = "You answered yes, current health level is " + healthLevel;
display(message);
}
public void no(View view) {
healthLevel = healthLevel - 1;
message = "You answered no, current health level is " + healthLevel;
display(message);
}
public void sometimes(View view) {
healthLevel = healthLevel + 0;
message = "You answered sometimes, current health level is " + healthLevel;
display(message);
}
...
}
@RajashekarRaju
Copy link

screenshot_20170123-113805
Run this code on Android Studio :

activity_main.xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Health Check"
    android:paddingBottom="30dp"
    android:textSize="25sp"
    android:textColor="#2196F3"
    android:id="@+id/Health_text_view"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="Do You Have a Supportive Social Netwrok ?"
    android:textSize="25sp"
    android:background="@color/colorAccent"
    android:layout_below="@id/Health_text_view"
    android:textColor="#000000"
    android:layout_centerHorizontal="true" />

<Button
    android:text="Y e s"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:textSize="20sp"
    android:textColor="#FFFFFF"
    android:background="@color/colorPrimaryDark"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/button" />

<Button
    android:text=" S o m e t i m e s "
    android:layout_width="wrap_content"
    android:textSize="20sp"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:layout_below="@+id/button"
    android:textColor="#FFFFFF"
    android:background="@color/colorPrimaryDark"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dp"
    android:id="@+id/button2" />

<Button
    android:text="N O"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:background="@color/colorPrimaryDark"
    android:textSize="20sp"
    android:id="@+id/button3"
    android:textStyle="bold"
    android:layout_marginTop="25dp"
    android:layout_below="@+id/button2"
    android:layout_alignLeft="@+id/button"
    android:layout_alignStart="@+id/button" />

MainActivity.java

public class MainActivity extends AppCompatActivity {

int healthLevel;
String message;

public void yes(View view) {
    healthLevel = healthLevel + 1;
    message = "You answered yes, current health level is " + healthLevel;
    display(message);
}

public void no(View view) {
    healthLevel = healthLevel - 1;
    message = "You answered no, current health level is " + healthLevel;
    display(message);
}

public void sometimes(View view) {
    healthLevel = healthLevel + 0;
    message = "You answered sometimes, current health level is " + healthLevel;
    display(message);
}

private void display(String message) {
    TextView priceTextView = (TextView) findViewById(R.id.displayMessage);
    priceTextView.setText(message);
}

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

}

THANKS FOR UDACITY

@juch11111
Copy link

juch11111 commented Feb 7, 2017

Full working code:

RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
    android:id="@+id/displayMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your message.!!"
    android:textSize="34sp"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Yes..!!"
    android:onClick="yes"
    android:id="@+id/button"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sometimes"
    android:onClick="sometimes"
    android:id="@+id/button3"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Never"
    android:onClick="no"
    android:id="@+id/button2"
    android:layout_below="@+id/button3"
    android:layout_alignLeft="@+id/button"
    android:layout_alignStart="@+id/button"
    android:layout_marginTop="16dp" />
_________________________________________________________________________________________________________ java code

package com.example.android.practiceset2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import static android.R.attr.name;
public class MainActivity extends AppCompatActivity {

int healthLevel;
String message;

public void yes(View view) {
    healthLevel = healthLevel + 1;
    message = "You answered yes, current health level is " + healthLevel;
    display(message);
}

public void no(View view) {
    healthLevel = healthLevel - 1;
    message = "You answered no, current health level is " + healthLevel;
    display(message);
}

public void sometimes(View view) {
    healthLevel = healthLevel + 0;
    message = "You answered sometimes, current health level is " + healthLevel;
    display(message);
}

private void display(String message) {
    TextView priceTextView = (TextView) findViewById(R.id.displayMessage);
    priceTextView.setText(message);
}

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

}

@natansalda
Copy link

Great ;) Thanks guys for this code!

@evoxtorm
Copy link

private void display(String message) {
TextView priceTextView = (TextView) findViewById(R.id.displayMessage);
priceTextView.setText(message);
}

why there is error in displayMessage ??????
code is not running
any solutions????

@krishsam02
Copy link

Yes
have you passed String value when you call the method. Method must contain one String value as an arguments

@Nkiru-O
Copy link

Nkiru-O commented May 17, 2017

was really fun working with this...... thanks guys

capture

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/images2"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/icon_app"
        android:src="@drawable/card1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dp"
        android:text="@string/question"
        android:textColor="#263238"
        android:textSize="13sp"/>

    <TextView
        android:id="@+id/display_text_view"
        android:layout_width="250dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/border"
        android:padding="20dp"
        android:text=""
        android:textColor="#263238"
        android:textSize="16sp" />

    <Button
        android:id="@+id/display_text_view_2"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="25dp"
        android:background="@color/yes"
        android:onClick="yes"
        android:text="@string/opinion_yes"
        android:textSize="12sp" />

    <Button
        android:id="@+id/display_text_view_3"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="8dp"
        android:background="@color/sometimes"
        android:onClick="sometimes"
        android:text="@string/opinion_sometimes"
        android:textSize="12sp" />

    <Button
        android:id="@+id/display_text_view_4"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="8dp"
        android:background="@color/no"
        android:onClick="no"
        android:text="@string/opinion_no"
        android:textSize="12sp" />
</LinearLayout>

MainActivity.java

package com.example.android.practiceset2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // PASTE CODE YOU WANT TO TEST HERE

    }

    int healthLevel = 100;
    String message;

    public void display(String text) {
        TextView t = (TextView) findViewById(R.id.display_text_view);
        t.setText(text);
    }

    public void display1(String text) {
        display(text);
    }

    public void yes(View view) {

        message = "You answered yes, current health level is " + healthLevel +"%";
        display(message);
    }

    public void sometimes(View view) {

        message = "You answered sometimes, current health level is " + (healthLevel-25) +"%";
        display(message);
    }

    public void no(View view) {

        message = "You answered no, current health level is " + (healthLevel/2) +"%";
        display(message);
    }

    /**
     * Display methods that allow the text to appear on the screen. Don't worry if you don't know
     * how these work yet. We'll be covering them in lesson 3.
     */



}

.

@Vendrio
Copy link

Vendrio commented Sep 13, 2017

Here Is Correct Code With No Error-------
--------------------------------------------------------------------XML CODE------------------------------------------------------------

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Health Check"
    android:paddingBottom="30dp"
    android:textSize="25sp"
    android:textColor="#2196F3"
    android:id="@+id/health_check"
    android:layout_gravity="center"/>
<TextView
    android:layout_width="300dp"
    android:layout_height="100dp"
    android:text="Do You Have a Supportive Social Netwrok ?"
    android:textSize="25sp"
    android:background="@color/colorAccent"
    android:layout_below="@id/health_check"
    android:textColor="#000000"
    android:layout_gravity="center" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:background="@android:color/holo_green_light"
    android:text="Y e s"
    android:onClick="yes"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:textStyle="bold" />

<Button
    android:text=" S o m e t i m e s "
    android:onClick="sometimes"
    android:layout_width="wrap_content"
    android:textSize="20sp"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:layout_below="@+id/button"
    android:textColor="#FFFFFF"
    android:background="@color/colorPrimaryDark"
    android:layout_gravity="center"
    android:layout_marginTop="25dp"
    android:id="@+id/button2" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button"
    android:layout_alignStart="@+id/button"
    android:layout_below="@+id/button2"
    android:layout_gravity="center"
    android:layout_marginTop="25dp"
    android:background="@android:color/holo_red_dark"
    android:text="N O"
    android:onClick="no"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:textStyle="bold" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your Health Score"
    android:paddingBottom="30dp"
    android:layout_marginTop="25dp"
    android:textSize="25sp"
    android:textColor="#FF9FF3"
    android:id="@+id/displayMessage"
    android:layout_gravity="center"/>

------------------------------------------------------------------JAVA CODE-----------------------------------------------------------------
package com.example.android.practiceset2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
int healthLevel;
String message;

public void yes(View view) {
    healthLevel = healthLevel + 1;
    message = "You answered yes, current health level is " + healthLevel;
    display(message);
}

public void no(View view) {
    healthLevel = healthLevel - 1;
    message = "You answered no, current health level is " + healthLevel;
    display(message);
}

public void sometimes(View view) {
    healthLevel = healthLevel + 0;
    message = "You answered sometimes, current health level is " + healthLevel;
    display(message);
}

private void display(String message) {
    TextView priceTextView = (TextView) findViewById(R.id.displayMessage);
    priceTextView.setText(message);
}

}

@Mradulgoel
Copy link

MainActivity

public class MainActivity extends AppCompatActivity {

int healthLevel;
String message;
String message1;

public void yes(View view) {
    healthLevel = healthLevel + 1;
    message = "You answered yes, current health level is " + healthLevel;
    display(message);
    message1= "Score : " + healthLevel;
    display1(message1);
}

public void no(View view) {
    healthLevel = healthLevel - 1;
    message = "You answered no, current health level is " + healthLevel;
    display(message);
    message1= "Score : " + healthLevel;
    display1(message1);
}

public void sometimes(View view) {
    healthLevel = healthLevel + 0;
    message = "You answered sometimes, current health level is " + healthLevel;
    display(message);
    message1= "Score : " + healthLevel;
    display1(message1);
}




private void display(String message) {
    TextView priceTextView = (TextView) findViewById(R.id.displayMessage);
    priceTextView.setText(message);
}

private void display1(String message1) {
    TextView priceTextView = (TextView) findViewById(R.id.health_check);
    priceTextView.setText(message1);
}



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

ActivityMain

<TextView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:padding="20dp"
    android:text="Health Check"
    android:gravity="center"
    android:textSize="50sp"/>


<TextView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/question_text_view"
    android:text="Question: Do you Excercise Daily ?"
    android:padding="10dp"
    android:textSize="25sp"
    android:gravity="center"
    android:background="#FF5252"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/displayMessage"
    android:padding="10dp"
    android:textSize="25sp"
    android:text="Answer !"
    android:gravity="center"
    android:background="#E6EE9C"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id= "@+id/health_check"
    android:padding="20dp"
    android:text="0 % "
    android:gravity="center"
    android:textSize="50sp"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp"
    android:gravity="center" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="left"
        android:text="yes"
        android:onClick="yes"
        android:textAllCaps="true"
        android:textSize="25sp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="left"
        android:text="no"
        android:onClick="no"
        android:textAllCaps="true"
        android:textSize="25sp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="left"
        android:text="May be"
        android:onClick="sometimes"
        android:textAllCaps="true"
        android:textSize="25sp"/>


</LinearLayout>

@Muneera-Salah
Copy link

XML Code ( activity_main.xml )

`

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="Health"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check"
        android:textSize="20sp" />

</LinearLayout>

<TextView
    android:id="@+id/displayMessage"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginLeft="100dp"
    android:layout_marginRight="100dp"
    android:background="@android:drawable/editbox_background"
    android:text="" />

<Button
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="#388E3C"
    android:onClick="yes"
    android:text="Yes" />

<Button
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="#E64A19"
    android:onClick="sometimes"
    android:text="Sometime" />

<Button
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="#D32F2F"
    android:onClick="no"
    android:text="No" />
`

Java code ( MainActivity.java )
` package com.example.anrdoid.healthyliving;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int healthLevel;
String message;

public void yes(View view) {
    healthLevel = healthLevel + 1;
    message = "You answered yes, current health level is " + healthLevel;
    display(message);
}

public void no(View view) {
    healthLevel = healthLevel - 1;
    message = "You answered no, current health level is " + healthLevel;
    display(message);
}

public void sometimes(View view) {
    healthLevel = healthLevel + 0;
    message = "You answered sometimes, current health level is " + healthLevel;
    display(message);
}

private void display(String message) {
    TextView priceTextView = (TextView) findViewById(R.id.displayMessage);
    priceTextView.setText(message);
}

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

}
`

ScreenShot :
image

@freryksl
Copy link

freryksl commented Jan 6, 2018

Screenshot

ekran alintisi

MainActivity

package com.example.android.practiceset2;
import android.view.View;
import android.widget.TextView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

int healthLevel;
String message;

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

}

public void yes(View view) {
    healthLevel = healthLevel + 1;
    message = "You answered yes, current health level is " + healthLevel;
    display(message);
}

public void no(View view) {
    healthLevel = healthLevel - 1;
    message = "You answered no, current health level is " + healthLevel;
    display(message);
}

public void sometimes(View view) {
    healthLevel = healthLevel + 0;
    message = "You answered sometimes, current health level is " + healthLevel;
    display(message);
}

private void display(String message) {
    TextView t = (TextView) findViewById(R.id.displayMessage);
    t.setText(message);
}

}

Activity_Main

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Health Check"
    android:textColor="#2196f3"
    android:textSize="34sp" />


<TextView
    android:id="@+id/displayMessage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="80dp"
    android:gravity="center"
    android:text="Do you appear to doctor in every six months?"
    android:textSize="24sp" />

<Button
    android:layout_width="match_parent"
    android:layout_height="110dp"
    android:background="#02AE0A"
    android:layout_marginTop="180dp"
    android:layout_centerHorizontal="true"
    android:onClick="yes"
    android:text="Yes"
    android:textColor="#fff" />

<Button
    android:layout_centerHorizontal="true"
    android:layout_width="match_parent"
    android:layout_height="110dp"
    android:background="#F9FD00"
    android:onClick="sometimes"
    android:text="Sometımes"
    android:textColor="#fff"
    android:layout_marginTop="290dp"
     />

<Button
    android:layout_centerHorizontal="true"
    android:layout_width="match_parent"
    android:layout_height="110dp"
    android:background="#FD0004"
    android:onClick="no"
    android:text="Never"
    android:textColor="#fff"
    android:layout_marginTop="400dp"/>

@MuhammadHassann
Copy link

next question came randomly?

@osamasarhan
Copy link

good lesson

@Maxwe1
Copy link

Maxwe1 commented Feb 1, 2018

Liked the lesson and here is what I made:
q59tfoobr8mjxvhoicvbxq

@koteLovesYou
Copy link

public class MainActivity extends AppCompatActivity {
int healthLevel;
String message;
...
public void yes(View view) {
healthLevel = healthLevel + 1;
message = "You answered yes, current health level is " + healthLevel;
display(message);
}

public void no(View view) {
    healthLevel = healthLevel - 1;
    if (healthLevel <= 0)        {
        message = "You are DEAD!";
        chekCounter(counter);
                                }
        else                         {
    message = "You answered no, current health level is " + healthLevel;
    display(message);
}

public void sometimes(View view) {
    healthLevel = healthLevel + 0;
    message = "You answered sometimes, current health level is " + healthLevel;
    display(message);
}
...

}

@aboelkhair1
Copy link

<TextView
    android:id="@+id/result_text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#9E9D24"
    android:gravity="center"
    android:text="do you drink coffe more than two in day?"
    android:textSize="34sp" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="16dp"
    android:background="#FF8A65"
    android:onClick="yes"
    android:padding="16dp"
    android:text="Yes..!!"
    android:textStyle="bold" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="16dp"
    android:background="#FFCCBC"
    android:onClick="sometimes"
    android:padding="16dp"
    android:text="Sometimes"
    android:textStyle="bold" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="16dp"
    android:background="#FBE9E7"
    android:onClick="no"
    android:padding="16dp"
    android:text="Never"
    android:textStyle="bold" />

@aboelkhair1
Copy link

package com.example.android.healthyliving;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

int healthLevel = 0;
String message;

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

/**
 * This method is called when the yes button is clicked.
 */
public void yes(View view) {
    healthLevel = healthLevel + 1;
    String message = "good!! your health level is " + (healthLevel);
    displayResult(message);
}

/**
 * This method is called when the no button is clicked.
 */
public void no(View view) {
    healthLevel = healthLevel - 1;
    String message = "ohh!! your health level is " + (healthLevel);
    displayResult(message);
}

/**
 * This method is called when the never button is clicked.
 */
public void sometimes(View view) {
    healthLevel = healthLevel;
    String message = "your health level is " + (healthLevel);
    displayResult(message);
}

private void displayResult(int number) {
    TextView resultTextView = (TextView) findViewById(R.id.result_text_view);
    resultTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}

/**
 * This method displays the given text on the screen.
 */
private void displayResult(String message) {
    TextView resultTextView = (TextView) findViewById(R.id.result_text_view);
    resultTextView.setText(message);
}

}

@aboelkhair1
Copy link

image

@abdahma01
Copy link

Done

@kalnaji
Copy link

kalnaji commented Feb 23, 2018


screen shot 2018-02-23 at 10 03 25 am
I'm getting this error please help

Thaak you

@burobot2016
Copy link

i like it
i success

thaaaaanks aloooot
image

@louayeldin
Copy link

image

@WebFlat
Copy link

WebFlat commented Jun 17, 2018

3

@WebFlat
Copy link

WebFlat commented Jun 17, 2018

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:text="Android Check"
    android:textColor="@android:color/black"
    android:textSize="30sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/displayMessage"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:layout_below="@+id/text"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:background="#DB03DAC6"
    android:padding="10dp"
    android:text="Do you know JAVA?"
    android:textColor="@android:color/black"
    android:textSize="40sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/displayMessage"
    android:orientation="vertical"
    android:paddingLeft="40dp"
    android:paddingRight="40dp">

    <Button
        android:id="@+id/btn_Yes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="@android:color/holo_green_dark"
        android:onClick="yes"
        android:text="Yes"
        android:textSize="18sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btn_Partially"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_Yes"
        android:layout_marginTop="20dp"
        android:background="@android:color/holo_orange_dark"
        android:onClick="sometime"
        android:text="Partially"
        android:textSize="18sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btn_No"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_Partially"
        android:layout_marginTop="20dp"
        android:background="@android:color/holo_red_light"
        android:onClick="no"
        android:text="NO"
        android:textSize="18sp"
        android:textStyle="bold" />
</LinearLayout>

@amujica24
Copy link

I am getting the "no element found" error message, am I suppose to be getting this? Thanks

screen shot 2018-07-10 at 7 51 14 am

screen shot 2018-07-10 at 7 52 13 am

@mhmdpro
Copy link

mhmdpro commented Sep 2, 2018

ok !
I'm try it
excellent
I work in it for devoloping

@ulrichquatess
Copy link

Hello guys , Thank you all for your codes very helpful.

@shiva-karthick
Copy link

thanks

@omeshkumar65
Copy link

In video 8, Lesson 3, there is a quiz to ask that the message variable should be global or not. And answer given is no.
Why is it, message variable is use in all function So , it should be gobal. Please clarify the answer.

@deepak9962
Copy link

Screenshot_20200704-233215
Screenshot_20200704-233232
Screenshot from 2020-07-04 23-31-55

XML code :

activity_main.xml
`

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:background="@drawable/background_rounded_corner"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:padding="15dp">

    <TextView
        android:id="@+id/heading1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Health"
        android:textColor="#3c9"
        android:textSize="35sp" />

    <ImageView
        android:id="@+id/check_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_baseline_check_24" />

    <TextView
        android:id="@+id/heading1.2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check"
        android:textColor="#c35"
        android:textSize="35sp" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:layout_marginRight="20dp"
    android:background="@drawable/background_rounded_corner"
    android:padding="50dp">

    <TextView
        android:id="@+id/question_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#ddd"
        android:textSize="30sp" />

</LinearLayout>

<TextView
    android:id="@+id/current_score"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="30dp"
    android:layout_marginTop="10dp"
    android:text=""
    android:textAllCaps="true"
    android:textColor="#ddd"
    android:textSize="15sp" />

<Button
    android:id="@+id/vote_no"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="70dp"
    android:layout_marginTop="10dp"
    android:background="@drawable/button_background_radius"
    android:onClick="voteNo"
    android:text="@string/no"
    android:textColor="#fff"
    android:textSize="20sp" />

<Button
    android:id="@+id/vote_sometimes"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="70dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/button_background_orange"
    android:onClick="voteSometimes"
    android:text="@string/sometimes"
    android:textColor="#fff"
    android:textSize="20sp" />

<Button
    android:id="@+id/vote_yes"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="70dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/button_background_red"
    android:onClick="voteYes"
    android:text="@string/yes"
    android:textColor="#fff"
    android:textSize="20sp" />

<Button
    android:id="@+id/check_final_result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="50dp"
    android:layout_marginBottom="10dp"
    android:onClick="checkResult"
    android:padding="15dp"
    android:text="check result"
    android:textColor="#277"
    android:textSize="20sp" />

<TextView
    android:id="@+id/final_result_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:text=""
    android:textColor="#ddd"
    android:textSize="25sp" />

<TextView
    android:id="@+id/final_result_result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="5dp"
    android:text=""
    android:textColor="#fff"
    android:textSize="20sp" />

`

MainActivity.java
`package com.example.heathcheckup;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

int scored = 0;
int totalScore = 2;

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

    String questions;
    questions = "Do you feel like fever?";

    displayQuestionOne(questions);
}

public void displayQuestionOne(String text) {
    TextView question = (TextView) findViewById(R.id.question_1);
    question.setText(text);
}

public void checkResult(View view) {
    displayFinalResultText();
    displayScored(scored);
}

public void voteYes(View view) {
    String questions = "Do you feel like cold?";
    scored = scored - 1;
    displayCurrentScore(scored);
    displayQuestionOne(questions);
}

public void voteSometimes(View view) {
    String questions = "Do you feel like cold?";
    displayCurrentScore(scored);
    displayQuestionOne(questions);
}

public  void voteNo(View view) {
    String questions = "Do you feel like cold?";
    scored = scored + 1;
    displayCurrentScore(scored);
    displayQuestionOne(questions);
}


private void displayScored(int i) {
    TextView scored = (TextView) findViewById(R.id.final_result_result);
    scored.setText("You scored " + i + "/" + totalScore + ".");
}

private void displayCurrentScore(int i) {
    TextView currentScore = (TextView) findViewById(R.id.current_score);
    currentScore.setText("Your current score is " + i + ".");
}

private void displayFinalResultText() {
    TextView finalResultText = (TextView) findViewById(R.id.final_result_text);
    finalResultText.setText("Final Result :");
}

}`

@Chinex-Boroja
Copy link

Chinex-Boroja commented Sep 16, 2021

The XML

    <LinearLayout
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@drawable/border"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:padding="2dp">

        <TextView
            android:id="@+id/header_title1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/baloo"
            android:text="Health"
            android:textColor="#32cd32"
            android:textSize="24sp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_baseline_check_circle_24"/>

        <TextView
            android:id="@+id/title_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/baloo"
            android:text="Check"
            android:textColor="#fa7f27"
            android:textSize="24sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/border1"
        android:padding="30dp">

        <TextView
            android:id="@+id/display_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/network"
            android:fontFamily="@font/almendra_sc"
            android:textColor="#fff"
            android:textSize="18sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/current_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="30dp"
        android:layout_marginTop="10dp"
        android:fontFamily="@font/almendra_sc"
        android:text=""
        android:textAllCaps="true"
        android:textColor="#2f4f4f"
        android:textSize="14sp" />

    <Button
        android:id="@+id/click_no"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="70dp"
        android:layout_marginTop="10dp"
        android:backgroundTint="#8b0000"
        android:onClick="no"
        android:text="No"
        android:textAllCaps="false"
        android:textColor="#fff"
        android:textSize="14sp" />

    <Button
        android:id="@+id/click_sometimes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="70dp"
        android:layout_marginTop="5dp"
        android:backgroundTint="@color/chinex"
        android:onClick="sometimes"
        android:text="Sometimes"
        android:textAllCaps="false"
        android:textColor="#fff"
        android:textSize="14sp" />

    <Button
        android:id="@+id/click_yes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="70dp"
        android:layout_marginTop="5dp"
        android:backgroundTint="#32cd32"
        android:onClick="yes"
        android:text="Yes"
        android:textAllCaps="false"
        android:textColor="#fff"
        android:textSize="14sp" />

    <Button
        android:id="@+id/check_final_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"
        android:onClick="checkResult"
        android:padding="15dp"
        android:backgroundTint="#F0FFFF"
        android:text="Check Result"
        android:textColor="#277"
        android:fontFamily="@font/baloo"
        android:textSize="14sp"
        android:textAllCaps="false"/>

    <TextView
        android:id="@+id/result_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:text=""
        android:textColor="#277"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/count_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:text=""
        android:textColor="#277"
        android:textSize="12sp" />

gistCommit

### MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

int healthLevel;
String message;

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

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

}

public void displayMessage(String text) {
    TextView message = (TextView) findViewById(R.id.display_message);
    message.setText(text);
}

public void checkResult(View view) {
    displayFinalResultText();
    displayCount(healthLevel);
}

public void yes(View view) {
    healthLevel++;
    message = "You answered yes, current health level is " + healthLevel;
    displayHealthLevelCount(healthLevel);
    displayMessage(message);
}

public void sometimes(View view) {
    healthLevel = healthLevel + 0;
    message = "You answered sometimes, current health level is " + healthLevel;
    displayHealthLevelCount(healthLevel);
    displayMessage(message);
}

public  void no(View view) {
    healthLevel--;
    message = "You answered no, current health level is " + healthLevel;
    displayHealthLevelCount(healthLevel);
    displayMessage(message);
}

private void displayHealthLevelCount(int score) {
    TextView currentScore = (TextView) findViewById(R.id.current_score);
    currentScore.setText("Your current score is " + score);
}

private void displayFinalResultText() {
    TextView finalResultText = (TextView) findViewById(R.id.result_message);
    finalResultText.setText("Final Result:");
}

private void displayCount(int scored) {
    TextView count = (TextView) findViewById(R.id.count_message);
    count.setText("You scored " + scored );
}

}

Screenshot_20210916-011031_Health_Check 1

Screenshot_20210916-010939_Health_Check 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment