-
-
Save udacityandroid/332432b53a13e5c55c1c 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="com.example.android.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> |
it's 16/01/2020 and this the code that's work for me i have android version 3.5.3 for Windows 64-bit
`package com.example.practiceset2;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import com.example.practiceset2.R;
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
}
/**
* 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 it doesn't work for you then try to change the package name in first line (First line of above code) with your actual package name which you can also find in first line of your android studio's MainActivity.java class
i need a help to understand " why the 1st text don't show and the name of the app doesn't show also"
This is my java code :
package com.example.practiceset2;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display1("This is Box 1.");
display2("And this is Box 2.");
display3("And look! Box 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);
}
}
i need a help to understand " why the 1st text don't show and the name of the app doesn't show also"
This is my java code :
Hi @AMAbdElhameed,
Your java file is ok, I tested and shows all the TextViews.
I tested the XML file what Udacity shared here, that's also ok.
Your problem is in the file name.
In java file your code is that:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display1("This is Box 1.");
display2("And this is Box 2.");
display3("And look! Box 3!");
}
So, it uses "activity_main" XML file. Your XML file name is: "content_main.xml"
All you need to do is:
- to rename your XML file to "activity_main"
or - to rename your "activity_main" input in java file to "content_main".
Try using this one it worked for me
package com.example.practiceset2;
import android.os.Bundle;
//import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
//import java.text.NumberFormat;
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
}
/**
* 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);
}
}
Guys don't worry, just try a solution.
In activity_main.xml file
Replace this: tools:context="com.example.android.practiceset2.MainActivity">
To this: tools:context="com.example.practiceset2.MainActivity">
and in MainActivity.java file
Replace this: package com.example.android.practiceset2;
To this: package com.example.practiceset2;
one suggestion from my side :- DO NOT COPY-PASTE THE CODE AS TOLD BY THE INSTRUCTOR.
rather build up the code as given here in the gist.
actually i had issue of app crashes and what not.
i made a new project and rebuilt the code on my own and it worked
In activity_main.xml file
Replace this: tools:context="com.example.android.practiceset2.MainActivity">
To this: tools:context="com.example.practiceset2.MainActivity">
and in MainActivity.java file
This is the Final Working code for me.
I'm using Windows 10 (64 bit), and android studio 4.2 (beta 4).
_I tried the solution by @fedor90 and then the following code:_
### **for "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:orientation="vertical"
tools:context="com.example.practiseset2.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>
**### for "MainActivity.java "**
package com.example.practiseset2;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import com.example.practiseset2.R;
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
}
/**
* 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);
}
}
``
for those who are facing a issue with unable to resolve display_text_view issue just check your text view id because in my id "+" was not present
Thank you!
It is leaving some kind of errors.