-
-
Save LaszloLajosT/8786ce477066458d9c5929a83c53514d to your computer and use it in GitHub Desktop.
<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); | |
} | |
} |
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
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"
Heya, thanks a million. Will try it out, if I don't get it right will write again.
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.
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">
If I try to put in the Linear Layout the whole app goes blank and I can't upload the scroll View either.