Skip to content

Instantly share code, notes, and snippets.

@LaszloLajosT
Last active April 17, 2020 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LaszloLajosT/8786ce477066458d9c5929a83c53514d to your computer and use it in GitHub Desktop.
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.
<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>
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);
}
}
@Annielaing
Copy link

Hey,

Thanks for the update it really helped a lot. I have a question regarding the Linear layout and constraint layout.

If I have this code then my app works:

<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="Quantity"
    android:textAllCaps="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.037"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.291" />

If I try to put in the Linear Layout the whole app goes blank and I can't upload the scroll View either.

@LaszloLajosT
Copy link
Author

LaszloLajosT commented Nov 21, 2019

If I try to put in the Linear Layout the whole app goes blank and I can't upload the scroll View either.

You are welcome!
Good to hear that somebody actually checked it and found it useful.
If you have difficulties during the course, you can message me, maybe we can find a solution together.

Go back to business:
I didn't use ConstraintLayout for my projects yet, but I can tell that your TextView contains a lot of attributes for RelativeLayouts and not for LinearLayout.

app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.037"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"

This code above works for RelativeLayout!
There are some attributes what you can only use for LinearLayout and some code for only RelativeLayout.
I think you can find these code inside 2)User Interface -> Lesson 3: Building Layouts: Part 2 -> 7. RelativeLayout

@LaszloLajosT
Copy link
Author

If I try to put in the Linear Layout the whole app goes blank and I can't upload the scroll View either.

Keep only this code for your TextView and it will work fine with LinearLayout. Even you can use ScrollView I believe.

android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="Quantity"
    android:textAllCaps="true"

@Annielaing
Copy link

Heya, thanks a million. Will try it out, if I don't get it right will write again.

@LaszloLajosT
Copy link
Author

That's fine :)
I hope it works and you will have more questions.

@LaszloLajosT
Copy link
Author

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