Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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>
@dominic-berlin

This comment has been minimized.

Copy link

dominic-berlin commented Aug 9, 2016

Line 10 didn't work for me:
tools:context=".MainActivity">

I changed it the following:
tools:context="com.example.android.practiceset2.MainActivity">

@kaito007

This comment has been minimized.

Copy link

kaito007 commented Aug 17, 2016

It worked for me.

@DGF86

This comment has been minimized.

Copy link

DGF86 commented Aug 30, 2016

Funcionou para mim.

@manmeet-22

This comment has been minimized.

Copy link

manmeet-22 commented Sep 21, 2016

In this Line 10 does not contain the package name-
`

tools:context=".MainActivity">`

Instead add this-
tools:context="com.androidexample.practiceset2.MainActivity">

@TulioIsaak

This comment has been minimized.

Copy link

TulioIsaak commented Oct 15, 2016

Line 10 didn't work for me:
tools:context=".MainActivity">

I changed line 3 from AndroidManifest.xml file:
package="app.com.example.android.practiceset2">
to:
package="com.example.android.practiceset2">

than worked.

@ajayanku

This comment has been minimized.

Copy link

ajayanku commented Nov 3, 2016

screenshot 43
its not working pls tell me?

@itechmode

This comment has been minimized.

Copy link

itechmode commented Dec 26, 2016

Get the same problem here.
I changed the line10. It was originally:
tools:context=".MainActivity"

I changed For:
I changed it the following:
tools:context="com.example.android.practiceset2.MainActivity"

The problem after that it was MainActivity get red. Any clues aboute this error?

@rdh47

This comment has been minimized.

Copy link

rdh47 commented Jan 9, 2017

I had a similar issue, but it was because I did not create the project with the recommended domain. I corrected the domain at beginning of the main activity.java to the one I used, and it corrected the error.

@AnestisZisidis

This comment has been minimized.

Copy link

AnestisZisidis commented Jan 19, 2017

If it still doesn't work ,after the changes on tools:context= .
Just Rebuild your project (Build-->Clean Project,Build-->Rebuild Project).

@AbhishekCCT

This comment has been minimized.

Copy link

AbhishekCCT commented Jan 20, 2017

w.r.t. rdh47, there must be a spelling difference(of your project name) between the one in java code (first line) and the one in xml code (10th line). This was the case with me.

@Walizai

This comment has been minimized.

Copy link

Walizai commented Jan 30, 2017

Hello dear all,

I have the same problem with line number 10, it show .MyActivity with red color, I tried the solution above now show the praticeset2 with red color but it can run without any problem in Andriod Studio but in mobile says you application failed to start.

Any help!!!

@poltudora

This comment has been minimized.

Copy link

poltudora commented Feb 1, 2017

Same problems over here ? Have you figured out what is the issue ?

@gielmuda

This comment has been minimized.

Copy link

gielmuda commented Feb 4, 2017

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".

@Prakhar32

This comment has been minimized.

Copy link

Prakhar32 commented Feb 6, 2017

I am encountering following errors
Error:(8, 28) No resource found that matches the given name (at 'paddingBottom' with value '@dimen/activity_vertical_margin').
Error:(9, 26) No resource found that matches the given name (at 'paddingLeft' with value '@dimen/activity_horizontal_margin').
Error:(10, 27) No resource found that matches the given name (at 'paddingRight' with value '@dimen/activity_horizontal_margin').
Error:(11, 25) No resource found that matches the given name (at 'paddingTop' with value '@dimen/activity_vertical_margin').

What do I do?

@Fedor98

This comment has been minimized.

Copy link

Fedor98 commented Feb 10, 2017

@gielmude: It doesn't matter, it's just the name of a project you'll probably never publish to the public. So never mind if you misspelled.

@Prakhar32: in the Android view of the project, go to app -> res -> values. Right click on values, then "New" (right at the top of the pop-up menu), "Value resource file". In the "file name" space, type "dimens.xml" (without the quotation marks " ") and then simply "Ok".
Click on the newly created "dimens.xml" file. Remove all code in it and paste the following:
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="activity_vertical_margin">8dp</dimen> <dimen name="activity_horizontal_margin">8dp</dimen> </resources>

This should work! :)

@Prakhar32

This comment has been minimized.

Copy link

Prakhar32 commented Feb 11, 2017

Thanks Fedor98.
It worked like a charm.

@jcunanan05

This comment has been minimized.

Copy link

jcunanan05 commented Mar 4, 2017

@Fedor98 thanks

@RidowanAhmed

This comment has been minimized.

Copy link

RidowanAhmed commented Mar 4, 2017

Why getting those red lines
image

@byronwarwick

This comment has been minimized.

Copy link

byronwarwick commented Mar 6, 2017

Thanks for that solution @Fedor98

@sumitPhD

This comment has been minimized.

Copy link

sumitPhD commented Mar 10, 2017

"package" in MainActivity.java and "tools:context" in activity_main.xml should be same and proper.
Just like below
git1
git2

@O-leChuck

This comment has been minimized.

Copy link

O-leChuck commented Mar 16, 2017

@Fedor98: Thanks a lot! Worked for me, too. Could maybe someone give me a short explanation of what this does or why the problem occured? I'd like to understand what was wrong :)

@lynden73

This comment has been minimized.

Copy link

lynden73 commented Mar 18, 2017

@Fedor98: Thanks, that worked perfectly.

@DoulatRana

This comment has been minimized.

Copy link

DoulatRana commented Mar 18, 2017

@RidowanAhmed

Following are the instructions of @Fedor98. This instructions resolved the problem that you're facing.

Go to Android view of the project (look to left side of the android studio), go to app -> res -> values. Right click on values, then "New" (right at the top of the pop-up menu), "Value resource file". In the "file name" space, type "dimens.xml" (without the quotation marks " ") and then simply "Ok".
Click on the newly created "dimens.xml" file. Remove all code in it and paste the following:

8dp 8dp

This should work! :)

@Mergen23

This comment has been minimized.

Copy link

Mergen23 commented Mar 23, 2017

important !!!!!

this 3 line must be same :

  • copy the package name in AndroidManifest.xml
    *paste in line 12 of activity_main.xml (example: tools:context="com.example.administrator.practiceset2.MainActivity")
    *paste in first line of MainActivity.java (example package com.example.administrator.practiceset2; )

it will solve the red MainActivity.

@kickupthefire

This comment has been minimized.

Copy link

kickupthefire commented Apr 8, 2017

@Fedor98 thanks for helping to resolve dimen, you're di man!

@neliamx4

This comment has been minimized.

Copy link

neliamx4 commented Apr 9, 2017

Thank you all guys! I would have big problems without your advice.

@nagyklaudiu

This comment has been minimized.

Copy link

nagyklaudiu commented Apr 11, 2017

after creating the dimens.xml file everything will be ok. Thank you Fedor98

@Wierdgeezer

This comment has been minimized.

Copy link

Wierdgeezer commented Apr 23, 2017

@Fedor98 - Thanks for the dimens.xml file tip. That saved me a lot of tears!

@O-leChuck - I could be wrong but it looks like the @Dimen elements are custom ones, not part of android studio's standard definitions. What @Fedor98's bit of code did was to create two variables named activity_vertical_margin and activity_horizontal_margin in a new resource called @Dimen and assign them both the value 8dp.

@watfordjc

This comment has been minimized.

Copy link

watfordjc commented Apr 30, 2017

@Wierdgeezer - According to a Stack Overflow answer it is because we are starting from a ConstraintLayout Empty activity. Before Android Studio 2.3 the default activity created dimens.xml.

Up to this point I've just been deleting those lines, or replacing android.support.constraint.ConstraintLayout with LinearLayout/RelativeLayout, so thanks @Fedor98.

@inaborges

This comment has been minimized.

Copy link

inaborges commented May 11, 2017

Thanks @Fedor98 ! :)

@rohangho

This comment has been minimized.

Copy link

rohangho commented May 12, 2017

I think the line number 10 is a bit faulty.

@hoangtubevietnam

This comment has been minimized.

Copy link

hoangtubevietnam commented May 14, 2017

thank for your advice @Fedor98

@tbanj

This comment has been minimized.

Copy link

tbanj commented May 14, 2017

@Fedor98

thanks for the comment on dimen.xml file,
after inserting the required xml code the error syntax which it was indicated in my activity_main.xml at the line 6 to 9 vanished.. it worked

@harneeblayze

This comment has been minimized.

Copy link

harneeblayze commented May 19, 2017

@Fedor98 you are saving lives... thanks a bunch!!!!!

@mystery1999

This comment has been minimized.

Copy link

mystery1999 commented May 20, 2017

@Fedor98...I had an error-no resource found at object @dimen/activity_vertical_margin
I've been able to resolve it but can I get an explanation on whyit occurred?

@Iszybe

This comment has been minimized.

Copy link

Iszybe commented Jun 24, 2017

Thanks @Fedor98

@Vyshakh-Babu

This comment has been minimized.

Copy link

Vyshakh-Babu commented Jun 25, 2017

@Fedor98 Thanks bro

@Amn-Akash

This comment has been minimized.

Copy link

Amn-Akash commented Jun 30, 2017

Thanks @Fedor90

@strategist-bal

This comment has been minimized.

Copy link

strategist-bal commented Jul 7, 2017

Thanks @Fedor98

@kenny57

This comment has been minimized.

Copy link

kenny57 commented Jul 9, 2017

Thanks @Fedor98

@ershadak

This comment has been minimized.

Copy link

ershadak commented Jul 10, 2017

line 6 to 10 cannot resolve the values starting with @dimen/activity

@sandeepvenkata

This comment has been minimized.

Copy link

sandeepvenkata commented Jul 16, 2017

i'm geeting errors
just

@crisfrench

This comment has been minimized.

Copy link

crisfrench commented Jul 23, 2017

Since the project doesn't have a dimen.xml file I commented out all the code [@dimen/...] and added [android:padding="8dp"]

As for tools:context , I used [tools:context=".MainActivity">]

More info on using dimension - https://developer.android.com/guide/topics/resources/more-resources.html#Dimension

@Guille55

This comment has been minimized.

Copy link

Guille55 commented Aug 22, 2017

I had the same "@Dimen....." errors
Thanks to @Fedor98 for the solution!

@MokshMahajan

This comment has been minimized.

Copy link

MokshMahajan commented Sep 24, 2017

Help me resolving this error
help

@ELHafizhHidayat

This comment has been minimized.

Copy link

ELHafizhHidayat commented Oct 2, 2017

thanks a lot @Fedor98, really helpfull.
@MokshMahajan just scroll up, and see @Fedor98 post

@ishaanwalter

This comment has been minimized.

Copy link

ishaanwalter commented Oct 15, 2017

the package is missing, try adding the following code to the top of your Mainactivity.java
package com.example.android.justjava;
hope this would help you.

@FranzoJosefo

This comment has been minimized.

Copy link

FranzoJosefo commented Oct 16, 2017

screen shot 2017-10-15 at 11 26 55 pm

screen shot 2017-10-15 at 11 27 10 pm

Hi guys, any thoughts on why line 23 is failing on my main activity? for some reason it cannot resolve display_text_view but as you can see in the activity_main.xml file that id exists. Any thoughts?

Thanks in advance.

@kkchain

This comment has been minimized.

Copy link

kkchain commented Oct 28, 2017

Thanks @Fedor98, for sharing how to resolve the @Dimen problem at the xml

@rosberta

This comment has been minimized.

Copy link

rosberta commented Nov 9, 2017

Thank you @Fedor98!

@lixrg

This comment has been minimized.

Copy link

lixrg commented Nov 14, 2017

I got the @Dimen problem as well so I followed the instructions of @Fedor98 but now I'm still stuck. What did you guys write into the new dimens.xml to resolve the problem?? Would highly appreciate some help!

@cyberdeity

This comment has been minimized.

Copy link

cyberdeity commented Nov 16, 2017

@Fedor98 Thanks a lot, problem solved :)

@tgejason

This comment has been minimized.

Copy link

tgejason commented Nov 21, 2017

I got the @Dimen problem as well so I followed the instructions of @Fedor98 but now I'm still stuck. What did you guys write into the new dimens.xml to resolve the problem?? Would highly appreciate some help!

@lixrg

this is what I found and what should be in 'dimens.xml'
< ?xml version="1.0" encoding="utf-8"? > <resources> <dimen name="activity_vertical_margin">8dp</dimen> <dimen name="activity_horizontal_margin">8dp</dimen> </resources>

@seni1

This comment has been minimized.

Copy link

seni1 commented Dec 2, 2017

@tgejason this is what I used thanx to @Fedor98 and it worked for me:

<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="activity_vertical_margin">8dp</dimen> <dimen name="activity_horizontal_margin">8dp</dimen> </resources>

... look out for spaces and other tiny misspellings... extra dot in the wrong place, messes the whole code... it's that kind of business m8!

@udacityandroid

This comment has been minimized.

Copy link
Owner Author

udacityandroid commented Dec 4, 2017

Thank you for all the feedback! And big shoutout to all the students taking the time to help others out.

Updates in activity_main.xml:

(1) In the LinearLayout, the tools:context has been updated to
tools:context="com.example.android.practiceset2.MainActivity"
which includes the full package name of the activity the layout is associated with

(2) In the LinearLayout, the following padding dimensions have been removed. Including them would require adding a separate dimens resource file (that was not initially included). For the purpose of this exercise, this was extraneous.

    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
@kr-vaz

This comment has been minimized.

Copy link

kr-vaz commented Jan 12, 2018

@FranzoJosefo perform a gradle sync that should solve the problem.

@jofego12

This comment has been minimized.

Copy link

jofego12 commented Jan 13, 2018

Excuese me @udacityandroid I still having problems with "tools" and they're just solved if a put "MainActivity". If not, all remains like in this image.
practiceset2 xml

Also, I still having problems on MainActivity.java package it's called package com.example.android.practiceset2; and it's some problem with it statements (I'm attaching a second image for this issue).
practiceset2 java

Could you help me please? I tried all that your commented on this forum, unsueccesfuly.

@drszabodavid

This comment has been minimized.

Copy link

drszabodavid commented Jan 14, 2018

It works for me, thanks!

@Maxwe1

This comment has been minimized.

Copy link

Maxwe1 commented Jan 31, 2018

All works for me, thanks a lot.

@Nogaeman

This comment has been minimized.

Copy link

Nogaeman commented Feb 2, 2018

Thanks
untitled

@ohi4fi

This comment has been minimized.

Copy link

ohi4fi commented Feb 3, 2018

works fine.

@tomnta

This comment has been minimized.

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

This comment has been minimized.

Copy link

jubilee2 commented Feb 8, 2018

Working well
image

@abdahma01

This comment has been minimized.

Copy link

abdahma01 commented Feb 13, 2018

Done

@jorge262

This comment has been minimized.

Copy link

jorge262 commented Feb 17, 2018

@tomnta thank you1

@badrddinb

This comment has been minimized.

Copy link

badrddinb commented Feb 21, 2018

DONE
image

@optmsshiv

This comment has been minimized.

Copy link

optmsshiv commented Apr 10, 2018

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

lanrelinux commented Jun 3, 2018

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

This comment has been minimized.

Copy link

emanhamad commented Oct 1, 2018

it works well

@shankar-shiv

This comment has been minimized.

Copy link

shankar-shiv commented Dec 23, 2018

it works fine!!!

@Saitama-Sensie

This comment has been minimized.

Copy link

Saitama-Sensie commented Feb 11, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.