Search code examples
androidandroid-layoutuser-interfacescreenchildren

Layout puzzle development in Android


I am a newbie in Android.

I want to develop an application where I can change the screens based on button selected. Application might endup with 20+ screens with buttons or entry form. From 1 screen I got to change the screen to some other screen. I thought of FrameLayout where I can change the children.

I am not getting a way to start up. Like I created an Activity. My each screen should exceed which class so I can add it to the Layout ? How do I make my first screen visible on start up.

These all seem to be simple and silly questions, but really I can't get a starting point for the same. Any help is appreciative to help me achieve my goal.

UPDATED :

@Ghost, from your solution 1 more question arised in my mind. For screens where I only have to show buttons in specific format, I added a GridView and a ButtonAdapter with the help of this site.

If I add clickListener in ButtonAdapter only, then how can I pass parameters to setIntent(FooFooActivity.this....) ????? I have the Conext in ButtonAdapter - I can typecast it to FooFooActivity and can that work on. I can give ifs in ButtonAdapter in onclick() to make t go to proper screen. But the setIntent cna work the way I am saying or something like that. If it can work, then for many screens my single GridView and single ButtonAdapter class can do all the work.

NEW UPDATIONS ON Trials :

@Ghost, I tried and found that the above setIntent(FooFooActivity.this....) in ButtonAdapter doesnot work - as in ButtonAdapter it wont find scope for FooFooActivity.this.

  • In my FooFooActivity, I can't set onclikcListeners for buttons added via ButtonAdapter. I tried with gridview.getChild(i), but just doesn't set in any way.
  • I also made another linearlayout xml (buttonspage.xml) with 6 buttons and a DataActivity that uses it. That works perfectly and on button click also shows FooFooActivity.
  • To use the same buttonspage.xml layout in multiple activities, I set the content of FooFooActivity as buttonspage and set its button click listeners. For 1 button I set to exit the application and for other button to show DataActivity.

  • So with this I got 2 activity, FooFoo that shows DataActivity/Exit & DataActivity that returns to FooFoo. Its a cycle that starts & ends up with FoofooActivity. If I click "Exit" at first, it quits. But If I click "Exit" after showing DataActivity, then it just doesn't quit and shows DataActivity only. Is it that I can't use same layout in multiple activity ?? Or may I be going wrong somewhere (I doubt so) ? Same buttonpage layout I got to use in 10-12 pages except with different text on button and events. So was thinking for Write Once Use Multiple Times. Also got to change button styles dynamically of all these pages buttons.

Thanks


Solution

  • As far as your first screen is concerned, it depends on the activity, and it runs directly when you run the project. For example, if you have your project named as FooFoo, then while compiling/building your android project, android itself names the first class as FooFooActivity (i.e., if you've allowed android to create an activity out of it). Of course, you can rename it then and there itself. So, once you compile your UI in the default main.xml file and come back to your java file FooFooActivity, there's a particular line inside the onCreate method. That line is setContentView(R.layout.main);This line sets the view of the contents present inside that main.xml file. When you run the program, and when emulator starts, the emulator presents the view of your main.xml if your project doesn't contain any compilation or runtime errors.

    Now, coming to your 20+ layouts: You can have them all in your layout folder. Assign atleast one buttonin each screen that takes you to some screen say "XYZ" and let's assume that the class name is "ABC". Let's also assume that your FooFooActivity class takes you to this ABC class. Following steps will take you through how to do the navigation part.

    I. Inside the onCreate method of your FooFooActivity class, you already have the setContentView. Now, after that line, add the following lines:

     Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() 
                {
                    public void onClick(View v) 
                    {
                        Intent intent = new Intent(FooFooActivity.this, ABC.class);
                        startActivity(intent);  
                    }
                });
    

    By Passing an Intent you can guide yourself/your user to the desired screen/activity.

    II. Please keep in mind that in your ABC class, imply the same onCreate method and place your XYZ layout inside the setContentView like this: setContentView(R.layout.XYZ);

    III. In your manifest file, after your

     <activity
                android:label="@string/app_name"
                android:name=".FooFooActivity" >
    
    
                <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    

    add the following:

    <activity 
                android:name=".ABC"
                android:label="@string/app_name"
                >
            </activity>
    

    Once you're done with these steps, run your program.

    Please note that this is just for showing 2 screens to your user. You need to repeat it for 20+ times. I don't know/see any other way.

    As far as your layouts are concerned, I'd suggest you stick with Relative Layout or a Linear Layout. Generally most of the "normal" layouts can be achieved by either of these or their combination. If you want more, you can always find help on Android Developers' Site or Stack Overflow. Anyway, there are lot of other things that I haven't been able to explain here, but I intend to in due time. You can always refer to great books such as Hello Android, Programming Android, and Learning Android. Even CommonsWare - Mark Murphy has his set of books that are pretty popular. You can always start off with any of them.

    Hope this helps. All the best!

    EDIT FOLLOWING YOUR UPDATION :

    Of course it will return the last activity because I presume you've the following piece of code with your exit button:

    Button exit = (Button)findViewById(R.id.button2);
            exit.setOnClickListener(new OnClickListener() 
            {
    
                public void onClick(View v) 
                {
                    finish();
    
                }
            });
    

    So, basically what you're doing is ending that particular ABC/Main Activity. That means, android will take you to the previous screen/activity. That's all. If you keep the same layout and plan it to use it for different activities, everything will simply get confusing. With just 2 screens/activities, it's confusing enough for you as of now. Imagine what might happen with 20+. Well, the design is ultimately left to you. That's all I can say.