Last active
April 17, 2020 17:43
-
-
Save LaszloLajosT/8786ce477066458d9c5929a83c53514d to your computer and use it in GitHub Desktop.
This is an Android Basics Nanodegree Program Practice Set. Lesson 4. Making an App interactive. I read many comments how confusing to set up a new project before even start the practice part. I got it. A few updates came out since and the first step for beginners can be hard. So, here is my "create a new project" first step.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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:orientation="vertical" | |
tools:context="android.example.practiceset2.MainActivity"> | |
<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" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package android.example.practiceset2; | |
import android.os.Bundle; | |
import android.widget.TextView; | |
import androidx.appcompat.app.AppCompatActivity; | |
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 raspberryPrice = 5; | |
display1("1 box: $" + raspberryPrice); | |
raspberryPrice = 10; | |
display2("2 boxes: $" + (raspberryPrice)); | |
display3("3 boxes: $" + (raspberryPrice * 3)); | |
} | |
/** | |
* 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. | |
*/ | |
public void display(String text) { | |
TextView t = (TextView) findViewById(R.id.display_text_view); | |
t.setText(text); | |
} | |
public void display(int text) { | |
TextView t = (TextView) findViewById(R.id.display_text_view); | |
t.setText(text + ""); | |
} | |
public void display1(String text) { | |
display(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); | |
} | |
} |
That's fine :)
I hope it works and you will have more questions.
A little note
for everyone who is coming from the discussion:
Please, keep an eye:
- on package name(first line),
- the imports name,
- TextView id's field in the XML file.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Heya, thanks a million. Will try it out, if I don't get it right will write again.