Android Community
Results 1 to 7 of 7

Thread: Linear Layout problem

  1. #1
    Join Date
    Dec 2008
    Posts
    67

    Default Linear Layout problem


    I'm trying to recreate the GUI from the Notepad tutorial in code instead of XML(I'm not a fan of XML) and I can't seem to get it working, when I debug, I get a vague error message. Can anyone tell me what's wrong with this code? I'm used to java's layout managers and this seems like it should be easy...

    Code:
      LinearLayout.LayoutParams ltp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0);
            LinearLayout titleContainer = new LinearLayout(this);
            LinearLayout bodyContainer = new LinearLayout(this);
            LinearLayout mainContainer = new LinearLayout(this);
            TextView titleText = new TextView(this);
            EditText titleEdit = new EditText(this);
            TextView bodyText = new TextView(this);
            EditText bodyEdit = new EditText(this);
            
            titleText.setText("Title");
            titleEdit.setText("Enter Title Here:");
            bodyText.setText("Body");
            bodyEdit.setText("Enter Body Here:");
     
            titleContainer.setOrientation(LinearLayout.VERTICAL);
            bodyContainer.setOrientation(LinearLayout.HORIZONTAL);
            
            titleContainer.addView(titleText, ltp);
            titleContainer.addView(titleEdit, ltp);
            bodyContainer.addView(bodyText, ltp);
            bodyContainer.addView(bodyEdit, ltp);
            
            mainContainer.addView(titleContainer, ltp);
            mainContainer.addView(bodyContainer, ltp);
            
            setContentView(mainContainer);

  2. #2
    Join Date
    Oct 2008
    Posts
    1,493

    Default Re: Linear Layout problem

    This belongs in the development forum. Moving it there now.

    Why aren't you using the layout xml?

  3. #3
    Join Date
    Dec 2008
    Posts
    67

    Default Re: Linear Layout problem

    I'm used to the code for of Layouts and I personally like them...

  4. #4
    Join Date
    Nov 2008
    Posts
    144

    Default Re: Linear Layout problem

    I'm not a fan of XML layouts either. The Google tutorial (and many others) state that you can choose whether to use code based layout or XML based.

    But every single book, tutorial or example then goes on to exclusively use XML only layouts, so I would dispute that....

    Give the XML layouts a try. There's much more examples and they're actually not that complex once you get your head round them... And the eclipse plugin gives a fairly true representation of what they look like in the preview window.

    I tried the code in Eclipse and it worked fine. Can you post the error you are talking about? I have attached 2 screenshots of your code working. I have embedded your code in my own FTP client's TabbedLayout, so just ignore the tabs. But as you can see, the layout is working without error and without modification...
    Attached Images Attached Images

  5. #5
    Join Date
    Dec 2008
    Posts
    67

    Default Re: Linear Layout problem

    Sorry, I should have posted in the original message what the error was. They are enclosed below.

    I can't make much heads or tails of this... I seems like for all intents and purposes it should work.

    I don't like XML just because the coding seems easier and nicer to me, but I agree with you that while they say you can do it both ways, XML is the most preferred way of doing it... I guess it's time for this old dog to learn new tricks
    Attached Images Attached Images

  6. #6
    Join Date
    Nov 2008
    Posts
    144

    Default Re: Linear Layout problem

    Ouch. Those error messages are nasty. Try reinstalling SDK and Eclipse plugin...

  7. #7
    Join Date
    Dec 2008
    Posts
    67

    Default Re: Linear Layout problem


    Nope. I got it. I had an old reference from something that I neglected to take out. What do those error message usually indicate though, because sometimes In other code I will get it and not know what it's indicating...

Similar Threads

  1. Replies: 20
    Last Post: 10-05-2008, 09:49 PM
  2. Wallpaper Layout Done!!!
    By uga.buga in forum Developers Guild
    Replies: 24
    Last Post: 10-05-2008, 04:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •