Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active October 31, 2022 07:15
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 26 You must be signed in to fork a gist
  • Save udacityandroid/a00cf27969aea4ba7848 to your computer and use it in GitHub Desktop.
Save udacityandroid/a00cf27969aea4ba7848 to your computer and use it in GitHub Desktop.
Android Development for Beginners : Practice Set 2 Shopping List 2 Example
int raspberryPrice = 5;
display1("1 box: $" + raspberryPrice);
raspberryPrice = 10;
display2("2 boxes: $" + (raspberryPrice));
display3("3 boxes: $" + (raspberryPrice * 3));
@YassineOuardini
Copy link

Java CODE
package com.example.com;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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

    int raspberryPrice = 5;
    display1("1 box: $" + raspberryPrice);
    display2("2 boxes: $" + (raspberryPrice * 2));
    display3("3 boxes: $" + (raspberryPrice * 3));
}

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

public void display2(String text) {
    TextView t = (TextView) findViewById(R.id.display2_text_view);
    t.setText(text + "");
}


public void display3(String text) {
    TextView t = (TextView) findViewById(R.id.display3_text_view);
    t.setText(text + "");
}

}

XML

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/display_text_view"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/display2_text_view"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
   />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/display3_text_view"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
   />

@Oshomo
Copy link

Oshomo commented May 29, 2020

If you struggle in latest version of Android Studio check below codes
JAVA CODE:

package com.example.android.practiceset2;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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

    int raspberryPrice = 5;
    display1("1 box: $" + raspberryPrice);
    display2("2 boxes: $" + (raspberryPrice * 2));
    display3("3 boxes: $" + (raspberryPrice * 3));
}

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

public void display2(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view_2);
    t.setText(text + "");
}


public void display3(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view_3);
    t.setText(text + "");
}

}

XML CODE:

<TextView
    android:id="@+id/display_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="45sp" />

<TextView
    android:id="@+id/display_text_view_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="45sp" />

<TextView
    android:id="@+id/display_text_view_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="45sp" />

@surajsahani
Copy link

Correct Answer
Click On No than
1 box: $5
2 boxes: $10
3 boxes: $30

@Ashish538
Copy link

katherine is best

@MarvUkazu
Copy link

the goal wasn't accomplished and it displayed

1 box: $5
2 boxes: $10
3 boxes: $30

I wrote just the price without adding the number of boxes

1 box: $5
2 boxes: $10
3 boxes: $30

This is the answer an not :

$5
$10
$30

or

5
10
30

@AjayThenuan-CodingQuiz
Copy link

Great it Works Totally guys

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