Solved

how useful is main.xml ?

Posted on 2012-03-22
8
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37755420
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
ID: 37755431
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
ID: 37755782
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 37755801
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
 
LVL 8

Expert Comment

by:Santhana
ID: 37756590
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
ID: 37781141
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:Santhana
ID: 37781172
Hi garikapatisuresh,

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

Expert Comment

by:garikapatisuresh
ID: 37782011
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 63
web project error add remove 1 55
Exchange ActiveSync 12 54
What is the use of Forwarding Class in java 1 37
A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

726 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