6

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

Tvd
  • 4,463
  • 18
  • 79
  • 125

1 Answers1

12

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.

Ghost
  • 3,966
  • 1
  • 25
  • 36
  • @Ghost, Thanks for the explaination. So basically what you are saying is I should have Activity class for each Layout screens I want (20+). And for each button in the Activity I got to start as setIntent(). I hope I got this much correct. – Tvd Jan 05 '12 at 12:42
  • @Ghost, The only start-up issue I have - my main.xml remains as it is. I want to start up with "mainpage" layout i.e. MainActivity from my original FooFooActivity. The reason for doing this is, I will have to set/show MainActivity i.e. mainpage also. If I add that contents in main.xml then how do I show that screen from ABC class ? – Tvd Jan 05 '12 at 12:46
  • 1
    Yes, that's correct. But you don't set intents on every button in your layout. Example: If you've a cancel button that typically lets the user exit that particular activity, then passing an intent doesn't make sense. You pass an intent on a button only when you need that button to guide your users to different screen/activity.. And yeah, please accept the answer if it has/will helped/help you.. :) – Ghost Jan 05 '12 at 12:49
  • That's simple, my friend. All you've to do is just write the code in FooFooActivity instead of your MainActivity. In other words, make FooFooActivity as your MainActivity. – Ghost Jan 05 '12 at 12:52
  • @Ghost, in my many screens I only have N number of buttons. Thne on each click, I navigate to other set of buttons and then to particular form. If from ABC class, on a button click I want to see/go to my FoofooActivity, then how do I get that ? I had set the intent to ABC, now I want to remove the intent of ABC and show the initial one. For that what to set. – Tvd Jan 05 '12 at 13:05
  • @Ghost, have updated my question based on your solution provided. One point regarding Button click arised in my mind. Please try to answer that too. – Tvd Jan 05 '12 at 13:06
  • Oh goodness, Tvd! :) I'll surely try answering your questions and try helping you, but tomorrow (06 Jan., 2012) for sure. 10:30AM Indian Standard Time (IST). Convert that to your time and if you're/you can be online by then, then I can surely help you to the best of my knowledge. As of now, I've to leave. Adios. – Ghost Jan 05 '12 at 13:13
  • @Ghost, Thanks Ghost. That tells that I should use differnet layout xml's for each. Thanks once again. BTW, my other question is up. – Tvd Jan 06 '12 at 11:10
  • This also tells me that I got to call this.finish() on the Activity which I wish to close and auto the previous activity will be shown up. I can't keep both of them open and make it work like "CardLayout" of Java. – Tvd Jan 06 '12 at 11:38
  • You can't open 2 activities at once, AFAIK. If you intend to do so, you might have to play with threads, but still, I don't think it's possible. You're right about your derivations though. BTW, I've answered your other question to the best of my knowledge. – Ghost Jan 06 '12 at 11:44