Skip to main content

Referring to layout elements from the java activity

If you need to create a dynamic app, you need to make sure you are able to easily refer to your layout elements from your activities. There are two easy things in application building; coding java and making the layout. The hardest concept is connecting the two such that they match and work harmoniously.
Every layout element has a class. If it’s a widget, its class is located in the widget interface. If it’s a viewgroup, its class will be located in the view interface. When you reach that point, you realize that you can’t cram coding. There are so many layout interfaces and classes in those interfaces that it becomes senseless to cram them.
Many times when I was learning to code android apps, I read so many books, but I didn’t find a working principle for calling out the layout elements by id. It gave me some hard time learning until I read secure coding for android, a book published yearly to advice developers on the best secure ways to code their programs. You can download android application secure coding guide book with one click.
Each time I tried to call layout elements by id, I would get an element cannot be resolved error during compile time. This is what I realized:

Every element class has to be imported

This is obvious. Take an example of a code to call upon the TextView element below.

TextView bb=. (TextView) findViewById(R.id.heady);
bb.setText(“….”);

Even if you connected your activity to your layout by using the layout path, that code will shoot an error. Let’s now see the one that will work. It’s the same as the one above, but it imports the TextView class in order to tell the program that you are going to use it. That’s one special thing with java. The language needs to be told what it needs to use such that memory is saved by not assuming that the code will use each and every class.

Import android.widget.TextView;
That will make the code run. If you haven't learnt java yet or are struggling with it, check out my article on how to learn java in weeks.

Understanding the android interfaces and classes

When you are developing, you need to research all the imports you’ll make by mostly referring to the android developer guide online. Let me remind you, you can’t know all those interfaces, packages and classes let alone the functions associated with them and the parameters and their types that such functions take in.

Writing your xml code 

Your layouts may try to be good, but it’s so difficult to find a book that will guide you in your android xml understanding. More difficult is it to find a good book to teach you full android development because things change swiftly and books are not very flexible to change. That’s why most of your studies should be got at developer guides. 

Writing clean code
Never fail to check out the secure coding for android. That pdf book is published yearly by a Japanese association of developers. It’ll help you with coding safe apps and protecting sensitive user information from external, internal and social engineered attacks. Stay up to date by reading it. The first time, you’ll have to read about 450 Pages, but as you keep checking, you’ll only need to read the latest additions for that year. You can also read only the parts that are relevant to your current project then using the book as a reference.
Thanks for reading. If you have any questions about faulty code, let me know in the comments.

AUTHOR
                     Emmy Jayson

Comments