Solved

how useful is main.xml ?

Posted on 2012-03-22
8
410 Views
Last Modified: 2012-04-17
I have done silverlight programming before and it uses a xml file for a UI as well and you could also just use code instead.

q1)IN Android/eclipse  is there any problems wiht just using code for UI elements?
I would prefer to just have the basic code like below and add UI elements in main Java file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

...

q2) how do I code values in string.xml in the main Java code ?

    <string name="celsius">18</string>  I dont want to set the value here

q3) How do I set background color  for the app?
0
Comment
Question by:jagguy
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Isn't taht all (with the exception of background) part of the examples - section
Upgrade the UI to an XML Layout
of this page:
http://developer.android.com/resources/tutorials/hello-world.html

So, if you just follow their recommendations, what happens?
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
and here they say how to change background:

http://stackoverflow.com/questions/2748830/how-to-change-background-color-in-android-app

You need to use the android:background property , eg

android:background="@color/white"

Also you need to you need to add a value for white in the strings.xml

<color name="white">#FFFFFF</color>

and there is another option there also
0
 

Author Comment

by:jagguy
Comment Utility
ok, i just need to place this code in java main. How do I get rid of this file? I want to set these properties with java code

<?xml version="1.0" encoding="utf-8"?>
<resources>

     <color name="color">#0000FF</color>
      <string name="edit1">2.2</string>
       <string name="text1">appears here 2</string>
    <string name="app_name">Tut1_22</string>
    <string name="celsius">18</string>
      <string name="calc">convert</string>
  <string name="fahrenheit">20</string>

</resources>
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
this is an exmaple how to make dialog without xml,
this is not exactly like they made with main - but gives some idea how to do it:
http://android-codes-examples.blogspot.com/2011/03/how-to-display-custom-dialog-and.html

and then there are API's more or les like with say Swing - so you need to know some deatils about elements, how theyplay with layouts' etc.

Say this would be the TextView API

http://developer.android.com/reference/android/widget/TextView.html

but to create it you alos need to know abouta layouts, etc
just like when you learn how to do it with Swing
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
Hi  jagguy,

q1)  There is no problem of using jave  code to create UI elements...Actually declaring UI through java will be faster( Because declare UI through xml will take time to parse the xml )

Following reasons has been mentioned in the android documentation

The Android framework gives us the flexibility to use either or both of these methods for declaring and managing your application's UI.
The advantage to declaring your UI in XML is that

1) Better separate the presentation of your application from the code that controls its behavior
2) Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile
3) Declaring the layout in XML makes it easier to visualize the structure of your UI, so it's easier to debug problems


http://developer.android.com/guide/topics/ui/declaring-layout.html

q2)  How do I code values in string.xml in the main Java code ?
 <string name="celsius">18</string>

In  java code just create a variable String  celsius = "18" . But if you declare this it in xml we can access from the different activities....

q3) How do I set background color  for the app?

If you want  to set a background to the particular activity ,just get the root view reference.

Ex :
TextView   rootView  = new TextView(mContext);
setContentView(rootView);

rootView .setBackgroundColor(color);  // To set the background color of the activity
rootView .setBackgroundDrawable(drawable) // To set the drawable as background

or
we can use themes to change the appearence of the activity

1) Through java code we can dynamically change/set the Theme
         ((CurrentActivity)this).setTheme(R.style.demoStyle);

2) In Manifest file we can set the theme to particualr activity.
<activity ...
android:theme="@style/demoStyle"
</activity>
0
 
LVL 1

Accepted Solution

by:
garikapatisuresh earned 500 total points
Comment Utility
Hi jagguy,

The below are my comments about your questions.

q1)IN Android/eclipse  is there any problems wiht just using code for UI elements?
I would prefer to just have the basic code like below and add UI elements in main Java file.


Ans :

The Android framework gives you the flexibility to use either or both of these methods for declaring and managing your application's UI. For example, you could declare your application's default layouts in XML, including the screen elements that will appear in them and their properties. You could then add code in your application that would modify the state of the screen objects, including those declared in XML, at run time.

The ADT Plugin for Eclipse offers a layout preview of your XML — with the XML file opened, select the Layout tab.
You should also try the Hierarchy Viewer tool, for debugging layouts — it reveals layout property values, draws wireframes with padding/margin indicators, and full rendered views while you debug on the emulator or device.
The layoutopt tool lets you quickly analyze your layouts and hierarchies for inefficiencies or other problems.

The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile. For example, you can create XML layouts for different screen orientations, different device screen sizes, and different languages. Additionally, declaring the layout in XML makes it easier to visualize the structure of your UI, so it's easier to debug problems.

Hence based on your Customization requirement you can go for programmatic approach. But if you want that your code should be common across multiple screen density devices and multiple configurations etc, it's better to go for declarative approach.

q2) how do I code values in string.xml in the main Java code ?

    <string name="celsius">18</string>  I dont want to set the value here

Ans :

If you want a string resource should be maintain but value if you want to pass from the code then, it should be like below.

in your strings.xml declare the below:
<string name="celsius">"%1$d"</string>

and in your code refer the above resource in the below way.
int lCount = 18;
String.format(getResources().getString(R.string.celsius),lCount);
;

q3) How do I set background color  for the app?

Ans :

In order to apply colors or styles to your entire application, you can follow the below way.

- First create a file called styles.xml in your res\values folder.
- Define some style and color objects in styles.xml like below.

<color name="custom_theme_color">#b0b0ff</color>
<style name="MyCustomTheme" parent="android:Theme.Light">
        <item name="android:windowBackground">@color/custom_theme_color</item>
        <item name="android:colorBackground">@color/custom_theme_color</item>
    </style>
- In your AndroidManifest.xml in your application declaration assign the above style as a theme for your application like below.
 <application android:name="ApiDemosApplication"
            android:label="@string/activity_sample_code"
            android:icon="@drawable/app_sample_code"
            android:theme="@style/MyCustomTheme"
   >
---------------
-------------
----------------
</application>
0
 
LVL 8

Expert Comment

by:santhanasamy
Comment Utility
Hi garikapatisuresh,

    You are reinventing the wheel....
0
 
LVL 1

Expert Comment

by:garikapatisuresh
Comment Utility
Hi Mr. samy,

I'm not invented the android concept, I just tried to provide some information regarding where to use declarative approach and where to use the programmatic approach as per the question asked...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Everyone is using a smartphone these days. Contacts, messages, applications, documents, bookmarks, pictures, videos and other stuff which you created and are present only on your device come under the umbrella called “Personal Data”.…
A short article about a problem I had getting the GPS LocationListener working.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now