Layout puzzle development in Android

混江龙づ霸主 提交于 2019-12-01 17:23:26

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!