LinearLayout problem

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

LinearLayout problem

Postby timweicker » Fri Jun 12, 2009 9:48 am

Hey,


if I have a LinearLayout in my main.xml that looks like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.         android:id="@+id/linearlayout1"
  3.         android:orientation="vertical" android:padding="4dip"
  4.         android:gravity="center_horizontal" android:layout_width="fill_parent"
  5.         android:background="#99999999"
  6.         android:layout_height="fill_parent">
  7. </LinearLayout>
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


And in my .java-file, I'd like to add an image to this LinearLayout with the ID "linearlayout1" - how do I refer to exactly that LinearLayout?

I tried R.id.linearlayout1.addView(view);
But that doesn't work because R.id.linearlayout1 is an int, and I need a LinearLayout to refer to.

How can that be made? Any ideas?


Regards, Tim


EDIT: mLinearLayout = (LinearLayout)findViewById(R.id.linearlayout1); doesn't seem to work, either...
timweicker
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Jun 10, 2009 7:55 pm

Top

Postby VIDEN » Fri Jun 12, 2009 10:00 am

use this:

findViewById(R.id.linearlayout1)

it will work!

Did you do a: setContentView(R.layout.main); ..or whatever your layout.xml is called....
in the beginning?

greets
Last edited by VIDEN on Fri Jun 12, 2009 10:03 am, edited 1 time in total.
VIDEN
Developer
Developer
 
Posts: 32
Joined: Tue May 12, 2009 9:39 am
Location: Germany

Postby WarrenFaith » Fri Jun 12, 2009 10:03 am

This is simple :)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ((LinearLayout) findViewById(R.id.linearlayout1)).addView(myNewView);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


//edit: wasnt fast enough :)
//edit2: post your java code please... and the error in your LogCat...
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby timweicker » Fri Jun 12, 2009 10:20 am

My images.java-file:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package test.images;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.view.ViewGroup.LayoutParams;
  10.  
  11. import android.widget.Gallery;
  12.  
  13. import android.widget.ImageView;
  14.  
  15. import android.widget.LinearLayout;
  16.  
  17.  
  18.  
  19. public class images extends Activity {
  20.  
  21.         protected void onCreate(Bundle savedInstanceState) {
  22.  
  23.                 super.onCreate(savedInstanceState);
  24.  
  25.  
  26.  
  27.                 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
  28.  
  29.  
  30.  
  31.                 ImageView image1 = new ImageView(this);
  32.  
  33.                 image1.setImageResource(R.drawable.icon1);
  34.  
  35.                 image1.setAdjustViewBounds(true);
  36.  
  37.                 image1.setLayoutParams(new Gallery.LayoutParams(
  38.  
  39.                                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  40.  
  41.                 linearLayout.addView(image1);
  42.  
  43.  
  44.  
  45.                 setContentView(R.layout.main);
  46.  
  47.         }
  48.  
  49. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


My main.xml-file:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    android:background="#ffffff"
  12.  
  13.    android:id="@+id/linearlayout" >
  14.  
  15. </LinearLayout>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


In my LogCat, it warns me with "uncaught handler" (?)
timweicker
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Jun 10, 2009 7:55 pm

Postby VIDEN » Fri Jun 12, 2009 10:39 am

Put the:

setContentView(R.layout.main); to the beginning of your method before you do the findViewById() call
...or else the findViewById() Method won't know where to look for the ID! ...it should work than

greets
VIDEN
Developer
Developer
 
Posts: 32
Joined: Tue May 12, 2009 9:39 am
Location: Germany

Postby tbi » Fri Jun 12, 2009 10:49 am

[EDIT]Damn, too slow.[/EDIT]

Greets,
/TBI
A la fin, il y aura seulement de la beauté.
tbi
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri May 29, 2009 8:22 am
Location: Marburg, Germany

Top

Postby timweicker » Fri Jun 12, 2009 11:25 am

Thank you very much!!!

This solved my problem :)
timweicker
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Jun 10, 2009 7:55 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests