• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

how useful is main.xml ?

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
jagguy
Asked:
jagguy
  • 3
  • 2
  • 2
  • +1
1 Solution
 
for_yanCommented:
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
 
for_yanCommented:
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
 
jagguyAuthor Commented:
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!

 
for_yanCommented:
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
 
SanthanaTechnical LeadCommented:
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
 
garikapatisureshCommented:
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
 
SanthanaTechnical LeadCommented:
Hi garikapatisuresh,

    You are reinventing the wheel....
0
 
garikapatisureshCommented:
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

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!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now