[solved]Activity with different layouts

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

[solved]Activity with different layouts

Postby Pandoro » Fri Dec 04, 2009 3:21 pm

Hello,

I am sure this has been asked in many ways and also been solved, but since I am not sure how to look for a solution exactly, I don't seem to find my answer.

I have an activity for which I want to dynamically change layout. Say I first have a login. Then after the login I get to the next screen with a different layout. In this screen I actually want to have 6 tabs, but since I haven't found a way to add the tabs in 2 rows, I decided to use 6 buttons. If I click on a button I want to change the layout accordingly.

So far the only "solution" I have seen is by starting a new activity. But the way I see it, if I have a variable in my first activity and I start a new activity, this variable is gone.

So how do I realize this? Is there a good solution for this, or do I really have to do it with different activities?

A hint to a older post or tutorial for this problem would be really helpful!

Thanks already,
pandoro

EDIT:

Okay, after spending some more time on my problem I tried creating a LinearLayout in my main.xml and then on a Button click load a View into the LinearLayout with addView(view). The views I inflated from other xml files. This works fine for simple XML files. However if I insert a button and actually want to make it clickable I reach the limit of this approach. No matter where I define my onClickListener it will not work out.

Here is my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Lineartest extends Activity {
  2.  
  3.         /** Called when the activity is first created. */
  4.  
  5.     @Override
  6.  
  7.     public void onCreate(Bundle savedInstanceState) {
  8.  
  9.         super.onCreate(savedInstanceState);
  10.  
  11.         setContentView(R.layout.main);
  12.  
  13.        
  14.  
  15.         final EditText text = (EditText) findViewById(R.id.TextView01);
  16.  
  17.         final View tab1 = View.inflate(this, R.layout.tab1, null);
  18.  
  19.         final View tab2 = View.inflate(this, R.layout.tab2, null);
  20.  
  21.         final Button button = (Button) findViewById(R.id.inTabButton);
  22.  
  23.         final Button tab1Button = (Button) findViewById(R.id.Button01);
  24.  
  25.         final Button tab2Button = (Button) findViewById(R.id.Button02);
  26.  
  27.         final LinearLayout test = (LinearLayout) findViewById(R.id.LinearLayout02);
  28.  
  29.        
  30.  
  31.  
  32.  
  33.         tab1Button.setOnClickListener(new OnClickListener() {
  34.  
  35.             public void onClick(View v) {
  36.  
  37.                 test.removeAllViews();
  38.  
  39.                 test.addView(tab1);
  40.  
  41.             }
  42.  
  43.         });
  44.  
  45.        
  46.  
  47.         tab2Button.setOnClickListener(new OnClickListener() {
  48.  
  49.             public void onClick(View v) {
  50.  
  51.                 test.removeAllViews();
  52.  
  53.                 test.addView(tab2);
  54.  
  55.             }
  56.  
  57.         });
  58.  
  59.        
  60.  
  61.         button.setOnClickListener(new OnClickListener() {
  62.  
  63.             public void onClick(View v) {
  64.  
  65.                 text.setBackgroundResource(R.color.red);
  66.  
  67.                
  68.  
  69.             }
  70.  
  71.         });
  72.  
  73.     }
  74.  
  75. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


An the xml file I try to load into my linearlayout
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.             >  
  13.  
  14.         <TextView      
  15.  
  16.                 android:layout_width="fill_parent"  
  17.  
  18.                 android:layout_height="wrap_content"  
  19.  
  20.                 android:text="tab1"  
  21.  
  22.             />  
  23.  
  24.         <Button
  25.  
  26.                 android:layout_width="wrap_content"
  27.  
  28.                 android:layout_height="wrap_content"
  29.  
  30.                 android:text="test    "
  31.  
  32.                 android:id="@+id/inTabButton"
  33.  
  34.                 />
  35.  
  36.        
  37.  
  38.         <TextView
  39.  
  40.                 android:text="@+id/TextView01"
  41.  
  42.                 android:id="@+id/TextView01"
  43.  
  44.                 android:layout_width="wrap_content"
  45.  
  46.                 android:layout_height="wrap_content"
  47.  
  48.                 />
  49.  
  50. </LinearLayout>  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4



My question is still kinda the same, would it be possible to get it to work like this? Or do I really have to start a new activity for every of my "tabs"?
Last edited by Pandoro on Sat Dec 05, 2009 4:13 pm, edited 1 time in total.
Pandoro
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Nov 04, 2009 9:41 pm
Location: Netherlands

Top

Postby appforce » Fri Dec 04, 2009 8:56 pm

Hi,

This method can work, to get references to the dynamically added View-s you have to initialize them every time the contents of the LinearLayout is changed:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      tab1Button.setOnClickListener(new OnClickListener() {
  2.             public void onClick(View v) {
  3.                test.removeAllViews();
  4.                test.addView(tab1);
  5.                button = (Button) test.findViewById(R.id.inTabButton);
  6.                button.setOnClickListener(new OnClickListener() {
  7.                     public void onClick(View v) {
  8.                        text.setBackgroundResource(R.color.red);
  9.                     }
  10.                });
  11.             }
  12.         });
  13.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby Pandoro » Sat Dec 05, 2009 2:54 pm

Oooh wow thnx man! I guess my mistake was to declare the variables within the onCreate() method. Because of this I could not do anything with the buttons inside of the onClickListener.

This seems to work out pretty good now, but my question is now does this scale well? Could I used 8 tabs instead of 2 with way more buttons and other elements within the view? Or should I used a different approach to accomplish my task?

I'm not really looking for the dirty fix, but rather for the good and elegent way ;) An answer would be very much appreciated.

Thank you very much so far already!
Pandoro
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Nov 04, 2009 9:41 pm
Location: Netherlands

Postby appforce » Sat Dec 05, 2009 3:35 pm

Hi,

This way you'll make one very big activity, which is not the best thing to do. One way to tidy things up is to make an abstract Activity class to be extended by activities for each tab. It's layout will have the buttons for switching tabs and a FrameLayout below them for the tab-specific content. You can override it's setContentView method to put the specific layouts inside the FrameLayout. That's a proven way to have custom tabs.

Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby Pandoro » Sat Dec 05, 2009 3:56 pm

Hi,

Well my "problem" is that I will be connecting to a server and transfering data to my device. If i start a new activity for each tab, I would need to keep passing on the data between my tabs if I get it right and I'd need to reconnect in every tab.

So would it be better to have 1 activity with all the data available or should I rather have several activities, which need to communicate with each other all the time?
For me this was the main reason to try and get it, into a single activity.
Pandoro
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Nov 04, 2009 9:41 pm
Location: Netherlands

Postby appforce » Sat Dec 05, 2009 4:11 pm

Hi,

I really can't give you advice on that - it's up to you. You can take a look at some convenient ways to exchange objects between activities:

viewtopic.php?p=30272&highlight=#30272

Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Top

Postby Pandoro » Sat Dec 05, 2009 4:13 pm

Okay :) Thank you very much. You've been a great help!

I'll change the topic to solved for now.
Pandoro
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Nov 04, 2009 9:41 pm
Location: Netherlands

Postby matany » Sat Dec 19, 2009 9:20 pm

Appforce,

Can you please help me here please...

I have 4 tab/mapview issue.

Here is my problem..


I have been strugling with this issue for the past 10 days...I am developing an app that has 4 tabs. One of the tabs (tab3) in TabActivity, I would like to show a mapview I created in seperate program/class

So I have...

1) TabActivity with 4 tabs in its own class and layout. (runs fine by it self) 2) MapActivity in its own class and layout. (runs fine by it self)

I tried to use intents to call the mapview in tab3 and run my program, I get error I also defined both classes in mainfest.xml

Please let me know what the best way to make this happen or merge the two files.

Thanks

Y
matany
Experienced Developer
Experienced Developer
 
Posts: 66
Joined: Tue Dec 08, 2009 9:29 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests