Sunday, January 25, 2015

Create Android View in Java Class

This time, I'm gonna discuss about how to build android app (Java).
Usually I'm share something rare or advance, but in this post, I will start from basic..maybe...

Basically after we create a Android application project. There are two thing need to create,
  • MainActivity.java
  • activity_main.xml
Below is the example,





In the MainActivity.java, will have this line,



That line use to call activity_main.xml via ID and use it as view. But there have other way to set and create the view. Now the lesson begin.

We can create a class that extends Layout or View, and then call it in activity. Below is the example code,





and to call the layout class is like this,




Below the sample running,




By the way, you also can call and add the activity_main.xml into MainLayout.java.

Let me show the example,



to call the layout same like before but need to add one more parameter, LayoutInflater.




and below the sample running, there are two "hello world!" display.
One with upper case 'W' from textView and one more with lower case 'w'  from activity_main.xml.



Here the full code for MainActivity,java and activity_main.xml,

MainActivity.java




activity_main.xml




So, here the end of this post...
If you have problem or something to ask something related to java...
you can email or comment on this post...
I will try to find the solution and share it with you..












you can test it by your self..
i hope you like this souce code...
and please give some comment....
                                                                                                                                                      Created By : Z-man, 2015

No comments:

Post a Comment