How to use addContentView?

Put your problem here if it does not fit any of the other categories.

How to use addContentView?

Postby NeoLiu » Tue Jan 15, 2008 3:55 am

Hi guys

i want Add an additional content view to the screen. Added after any existing ones in the screen -- existing views are NOT removed.

for example :

EditText c = new EditText(this);
this.addContentView(c, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

but my layout is not load from layout.xml, it was created myself

myView = new MyView(this);
setContentView(myView);

in this view add the contents any time i want or delete it
NeoLiu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Jan 07, 2008 3:27 am

Top

Postby kaobiore » Tue Jan 15, 2008 10:05 pm

Hi!

Looks like that I had quite the same problem like you ... it's simply not working to replace the whole content with "this.setContentView(view)" (uuuhh ... or at least I had some problems with it).

I'm going to show you what I've done ... I'm not creating the second view (for result-displaying in my case) in runtime but I'm switching between two views by showing and hinding them.

my "main.xml":

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    android:background="@drawable/background"
  13.  
  14.    >
  15.  
  16.        
  17.  
  18.         <LinearLayout
  19.  
  20.         id="@+id/mainlayout"
  21.  
  22.        android:orientation="vertical"
  23.  
  24.             android:layout_width="fill_parent"
  25.  
  26.             android:layout_height="wrap_content"
  27.  
  28.             >
  29.  
  30.             <!-- More Content here -->
  31.  
  32.         </LinearLayout>
  33.  
  34.  
  35.  
  36.         <LinearLayout
  37.  
  38.             id="@+id/resultlayout"
  39.  
  40.             android:orientation="vertical"
  41.  
  42.             android:layout_width="fill_parent"
  43.  
  44.             android:layout_height="wrap_content"
  45.  
  46.             >
  47.  
  48.             <!-- More Content here -->
  49.  
  50.         </LinearLayout>
  51.  
  52. </LinearLayout>
  53.  
  54.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Ok, I have defined my two views/layouts in main.xml ... on startup "mainlayout" will be displayed and should be replaced by "resultlayout". In fact I'm just hiding "mainlayout" and displaying "resultlayout".

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     private void generateMainView() {
  3.  
  4.         LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainlayout);
  5.  
  6.         LinearLayout resultLayout = (LinearLayout)findViewById(R.id.resultlayout);
  7.  
  8.  
  9.  
  10.         // Enable Main & Disable Result
  11.  
  12.         resultLayout.setVisibility(View.GONE);
  13.  
  14.         mainLayout.setVisibility(View.VISIBLE);
  15.  
  16.     }
  17.  
  18.    
  19.  
  20.     private void generateResultView() {
  21.  
  22.         LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainlayout);
  23.  
  24.         LinearLayout resultLayout = (LinearLayout)findViewById(R.id.resultlayout);
  25.  
  26.  
  27.  
  28.         // Enable Main & Disable Result
  29.  
  30.         resultLayout.setVisibility(View.VISIBLE);
  31.  
  32.         mainLayout.setVisibility(View.GONE);
  33.  
  34.     }
  35.  
  36.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


This works fine for me but I guess it wouldn't probably fit for you.

I've not tested it but you could try the following ...

1. Get the mainLayout like I did.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. mainLayout = (LinearLayout)findViewById(R.id.mainlayout);
  3.  
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


2. Try to add you "additional" view to the "mainLayout".

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. mainLayout.addView(child);
  3.  
  4.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Could probably work :D

Cheers
-kao-
kaobiore
Freshman
Freshman
 
Posts: 8
Joined: Sat Dec 29, 2007 11:21 am
Location: Vienna / Austria

Re: How to use addContentView?

Postby NeoLiu » Thu Jan 17, 2008 3:19 am

Hi kaobiore

Your code isnot fit for me , but that is good idea, thanks
NeoLiu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Jan 07, 2008 3:27 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 9 guests