Solved

how useful is main.xml ?

Posted on 2012-03-22
8
413 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
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

809 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