Android layout puzzle development

I am new to Android.

I want to develop an application in which I can change the screens based on the selected button. The application can end with 20+ screens with buttons or an input form. From 1 screen I had to change the screen to another screen. I was thinking about FrameLayout, where I can change children.

I do not get a way to start. How I created Activity. Does my every screen have to exceed a class so that I can add it to the layout? How to make the first screen visible at startup.

All this seems to be a simple and silly question, but in fact I cannot get a starting point for the same. Any help is appreciated to help me achieve my goal.

UPDATED:

@Ghost, another question arose from your decision in my mind. For screens where I need to show only buttons in a specific format, I added a GridView and ButtonAdapter using this site .

If I add clickListener only in the ButtonAdapter, then how can I pass the setIntent parameters (FooFooActivity.this ....) ????? I have a Conext in the ButtonAdapter - I can cast it to FooFooActivity and work with it. I can give ifs in the ButtonAdapter in onclick () to go to the correct screen. But setIntent cna works the way I say, or something like that. If it can work, then for many screens my only GridView and one ButtonAdapter class can do all the work.

NEW UPDATES ON PROBLEMS:

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

  • In my FooFooActivity, I cannot set onclikcListeners for buttons added via ButtonAdapter. I tried with gridview.getChild (i), but this is simply not set.
  • I also made another linearlayout xml (buttonspage.xml) with 6 buttons and a DataActivity that uses it. This works fine, and when the button is clicked, FooFooActivity is also displayed.
  • To use the same buttonspage.xml layout in several actions, I set the contents of FooFooActivity as pagepage and set its buttons to listen to. For 1 button, I set the exit from the application and for another button to show DataActivity.

  • So I got 2 activities, FooFoo, which shows DataActivity / Exit and DataActivity, which returns in FooFoo. This is the loop that begins and ends with FoofooActivity. If I first click "Exit", he will leave. But if I click "Exit" after displaying the DataActivity, then it just wonโ€™t leave and will only show DataActivity. Can't I use the same layout in multiple actions? Or can I be wrong somewhere (I so doubt)? The same button layout that I have to use on 10-12 pages, with the exception of different text on the button and events. So I thought about Write Once Use Multiple Times . It is also necessary to dynamically change the button styles of all these buttons.

thanks

+6
source share
1 answer

As for your first screen , it depends on the activity and is executed directly when the project starts. For example, if you have a project called FooFoo , then when compiling / creating your android project, the android itself calls the first class as FooFooActivity (i.e. if you allowed the android to create activity from it). Of course, you can rename it then and there yourself. So, as soon as you compile your user interface in the default main.xml file and return to your java FooFooActivity file, there is a specific line inside the onCreate method. This line is setContentView(R.layout.main); This line sets the view. the contents inside this main.xml file. When you run the program and run the emulator, the emulator presents a representation of your main.xml if your project does not contain compilation errors or main.xml .

Now, having come to your 20+ layouts : you can use them in your layout folder. Assign at least one button on each screen, which will take you to some screen, say โ€œXYZโ€, and suppose the class name is โ€œABCโ€. Suppose also that your FooFooActivity class leads you to this ABC class. In the next steps you will learn how to make a navigation part.

I. Inside the onCreate method of your onCreate class, you already have setContentView . Now, after this 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); } }); 

In By transmitting an intention, you can direct yourself / your user to the desired screen / activity.

II. Keep in mind that your ABC class means the same onCreate method and place your XYZ layout inside setContentView as follows: setContentView(R.layout.XYZ);

III. In your manifest file after

  <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 are done with these steps, run your program.

Please note that this is just showing 2 screens for your user. You need to repeat it 20 more times. I do not know / do not see another way.

As for your layouts, I would advise you to stick with Relative layout or Linear layout . Generally, most โ€œnormalโ€ layouts can be achieved with any of these or combinations thereof. If you want more, you can always find help on the Android Developers Site or Stack Overflow . In any case, there are many other things that I could not explain here, but I intend in due time. You can always refer to great books such as Hello Android , Android Programming, and Learning Android . Even CommonsWare - Mark Murphy has its own set of books, which are quite popular. You can always start with any of them.

Hope this helps. All the best!

EDIT YOUR UPDATE:

Of course, it will return the last action, because I assume that you have the following code snippet with an exit button:

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

So basically what you do ends up with that particular ABC / Main Activity. This means that android will lead you to the previous screen / activity. All this. If you stick to the same layout and plan to use it for different activities, everything will just be confusing. Only 2 screens / actions, this is pretty confusing for you. Imagine what can happen with 20+. Well, the design is ultimately left to you. That's all I can say.

+12
source

Source: https://habr.com/ru/post/905181/


All Articles