[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Layout help required for Android Eclipse

Posted on 2011-03-02
5
Medium Priority
?
541 Views
Last Modified: 2012-06-21
I am new to Android development and have been trying to implement a particular layout using Eclipse without any joy.

What I would like to implement are two toolbars at the top and bottom of the screen and a main panel in the middle.

The bottom toolbar has four buttons with images which should be spaced evenly although the text length of each button will vary.

I thought about implementing by using a table layout with three rows.  In the third row, I have a LinearLayout and my four buttons.

Firstly is this the best way to get the layout that I want.
Secondly I can't seem to set the buttons to be of equal size and fill the screen i.e. each button takes up a quarter of the width.
0
Comment
Question by:fujihuynh
[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
5 Comments
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35026474

Here is the code that will show you how to achieve what you are after. Its all done through LinearLayout.

You have to play around with it a while and learn exactly how things work.

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        setContentView(mainLayout, lp);

        LinearLayout top = new LinearLayout(this);

        Button button = new Button(this);
        button.setText("long test 1");
        top.addView(button);

        button = new Button(this);
        button.setText("test 2");
        top.addView(button);

        LinearLayout bottom = new LinearLayout(this);

        button = new Button(this);
        button.setText("long test 1");
        lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.weight = 1;
        bottom.addView(button, lp);

        button = new Button(this);
        button.setText("test 2");
        lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.weight = 1;
        bottom.addView(button, lp);

        LinearLayout center = new LinearLayout(this);

        button = new Button(this);
        button.setText("center");

        lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        center.addView(button, lp);


        lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mainLayout.addView(top, lp);

        lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.weight = 1f;
        mainLayout.addView(center, lp);

        lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mainLayout.addView(bottom, lp);
    }
}

Open in new window

0
 

Author Comment

by:fujihuynh
ID: 35026849
Thanks but do you have an example of the XML layout resource rather than designing the layout programatically
0
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 2000 total points
ID: 35027060

Well, the above is not an example - it's exactly what you asked for.

I designed it for you to see how its done.  From here on it is just straight forward to translate the code to xml layout - it only requires some work.

I never use xml layout, so It would also take me some time to translate it.

It really shouldnt be that difficult. Simpy follow the rules for xml layout  and translate, say this:

----
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
----

into this:

-----
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical" >   
</LinearLayout>

----
0
 

Author Comment

by:fujihuynh
ID: 35031954
Can I ask whether there are advantages to creating the layout programmatically rather than using the XML files for layout as you say you never use xml layout.
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35034053

Google advertises xml layout saying it is the best way to separate gui from code. I simply dont see any benefit in that approach since I then have to reference the objects defined in xml by id and type casting them to appropriate class.

I just dont think there is any real separation of layout code and business logic if you simply put the layout in xml - in the end you have to reference it and it is basicly the same as puting the layout code into a separate method. But that is just my opinion, you should use what you are most comfortable with.

Other than that I am not aware of any other advantages.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
These days software publishers make it possible to move all the possible applications on smartphones, tablets, smartwatches, and more. Although behind every logic and decision is a gross doubt: how do they make it possible? In this blog post, we sha…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
Suggested Courses

656 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