Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active July 8, 2022 07:14
Show Gist options
  • Star 57 You must be signed in to star a gist
  • Fork 31 You must be signed in to fork a gist
  • Save udacityandroid/332432b53a13e5c55c1c to your computer and use it in GitHub Desktop.
Save udacityandroid/332432b53a13e5c55c1c to your computer and use it in GitHub Desktop.
Android Development for Beginners : Starter Gist for 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.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>
@drszabodavid
Copy link

It works for me, thanks!

@Maxwe1
Copy link

Maxwe1 commented Jan 31, 2018

All works for me, thanks a lot.

@Nogaeman
Copy link

Nogaeman commented Feb 2, 2018

Thanks
untitled

@ohi4fi
Copy link

ohi4fi commented Feb 3, 2018

works fine.

@tomnta
Copy link

tomnta commented Feb 5, 2018

If anyone is getting an error in your tools:context="com.example.tom.practiceset2.MainActivity where part or all of the text is showing up in red, please try this:

  1. In your Project Explorer (left "explorer-like" pane of Android Studio), navigate to: app > manifests > AndroidManifest.xml and double click that file to edit it:
    as3
  2. On line 3, copy the value of the package attribute: package="com.example.tom.practiceset2"
  3. On line 7 in your activity_main.xml, paste that value into: tools:context="PASTE VALUE HERE"
  4. On line 1 in your MainActivity.java, paste that value again into: package PASTE VALUE HERE
  5. On the menu, click BUILD > CLEAN PROJECT
  6. On the menu, click BUILD > REBUILD PROJECT
  7. Run the app

The reason for this error is because the company domain was left at its default value, which was likely different than the values in these gists. All we did in these steps was to make sure all the values for the company domain are in sync. The screen for this practice exercise should look like this:
app
Hope this makes sense and it helps!

@jubilee2
Copy link

jubilee2 commented Feb 8, 2018

Working well
image

@abdahma01
Copy link

Done

@jorge262
Copy link

@tomnta thank you1

@badrddinb
Copy link

DONE
image

@optmsshiv
Copy link

When I use:- tools:context="com.example.android.MainActivity" instead of tools:context="com.example.android.practiceset2.MainActivity" , my code run
practise
without any error.

@arhnita
Copy link

arhnita commented May 30, 2018

tools:context="com.example.android.practiceset2.MainActivity">
The error message i'm getting is "Typo in word "practiceset"
What do i do please

@lanrelinux
Copy link

for some weird reason the android studio misspelt the android.example.com as "andriod" recreate you project and correct it then it will work. I am using a Mac. I hope this helps someone.

@emanhamad
Copy link

it works well

@shiva-karthick
Copy link

it works fine!!!

@Saitama-Sensie
Copy link

for those having problems with package name, on the updated android studio the word android is left out of package name. try deleting android from package name

@Aadhitya01
Copy link

Worked for me.

@estudioporta19
Copy link

estudioporta19 commented Aug 5, 2019

I close and open other projects. When I openned again ".MainActivity" wasn't red in "activity_main.xml" nor "AndroidManifest.xml". Although some other terms were in red in the MainActivity.java

Substituted:

import android.support.v7.app.AppCompatActivity;

for

import androidx.appcompat.app.AppCompatActivity;

and it runned in the phone.

Before the only thing that was in red was ".MainActivity", there were no errors when I run, but the app crashed on the phone.

@DutchSammmie
Copy link

Do you gyus have the project name as Practice Set 2, or Practise Set 2?
The thing is it should be practice with two "C"s, not with a "c" and then "s".

this must be it! Thanks...

@LaszloLajosT
Copy link

Hi guys,

I know it can be hard sometimes but look at that way. Once you understand how to fix these kind of errors, you will be faster next time. I mean, certainly by the time there are new features/updates and if you learn the technique, you will fix any future errors more likely.

My errors came after copy paste and certainly even my created project package name wasn't the same, so... Look at my package name and what I changed in XML and Java file. Good luck.

Fix1
Fix java

@happy1201
Copy link

It is leaving some kind of errors.

@FTW-Khushal
Copy link

FTW-Khushal commented Jan 16, 2020

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

@AMAbdElhameed
Copy link

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 :
Capture1
Capture4

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);
}

}

@LaszloLajosT
Copy link

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:

  1. to rename your XML file to "activity_main"
    or
  2. to rename your "activity_main" input in java file to "content_main".

@keithchad
Copy link

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);
}

}

@SagaMB
Copy link

SagaMB commented Sep 20, 2020

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;

@zimmy9537
Copy link

zimmy9537 commented Dec 6, 2020

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

@eshaananand
Copy link

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

@nafisahmad
Copy link

nafisahmad commented Feb 9, 2021

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);
        }
}
``

@mdparvez8
Copy link

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

@Samdzaka
Copy link

Samdzaka commented Jul 8, 2022

Thank you!

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