Android - Switching between Activities and Screens with same xml file - java

I'm building an app for educational purposes, with a lot of questions and challenges to the user to solve. But, it would be a HUGE amount of screens if I create a lot of questions and one xml file and activity for each one, as I want to have different kinds of questions, where the user should be able to write the code, select a block of code or select the correct answer about theory and stuff. Is there a way to use the same model of screen(xml file) for one specific kind of question? Like, using one model to all the screens and questions where the user should select the correct answer, and another model to another kind of question..
p.s: Yes, i'm kind newbie in Android
Thanks! :)

I would recommend using FragmentStatePagerAdapter
You only needs one layout and every time a fragment looses focus, it is destroyed automatically.
It is the same as adapters. You just create the visible views and 2 more. In this case you will have the visible question and one on the left and one on the right.
Let me know if you need any help.

You can define all kinds of questions, such as checkboxes, radioButtons, Edittexts and etc. in a xml file and, if necessary, visible or hide them.

Use Fragments to display questions in your activity.
In short, Fragments are mini-Activities with their own layouts, so each different type of question should have a corresponding Fragment. That should be exactly what you need.
For example, you can create a fragment_radiobox and a fragment_codesnippet in your project. In first one you will include a TextView for your question and a few checkboxes for an answer, and in the second one you'd have an EditText field to write some code. You can call any of those two fragments in your main activity depending on which question you want to display.

Related

How to update content of the activity without creating new Activity?

I'm trying to make an app and I have made a blueprint for a specific activity, but I don't know how to implement it. The layout contains few buttons at the top of the activity, and each button features some information, which is displayed inside the view. The view which needs to be updated is present under the buttons. I don't want the activity to be changed, instead it should update the contents of the View, which is different for each category/button.
By doing some research I have realised that the "Tab Layout" can be used to achieve my requirements, but I don't want the tabs and I need some stylish buttons as a replacement.
I know I'm not the best at describing, so I have looked upon Dribble and found one design which is 100% similar to blueprint.
I want to achieve this using XML and Java using Android Studio! Every suggestion will be a great support foy my app.
Thanks a lot.
As far as I know, you could achieve that by using fragments (which is the same concept you would have used on TabLayout). I don't really know how much knowleadge you have on Android, but if you know what a Fragment is, it should be easy for you to recreate the idea.
You have 3 buttons for 3 different fragments, so you must design every fragment by separate and change it depending the button you click.

Converting my current project to fragments. Lost with my project

I am absolutely lost with my project and I need someone to point me to the right direction because right now im just looking at my code and I cant understand where to start.
MY app is all about answering questions. A player has to answer if the question is right or wrong. If he answers the right amount of questions he unlocks a new level. So until today I just thought I will need to create a new activity on every level, create questions in that activity and let it run. But just today I realized that having 100 activities of different levels is just stupid.
So I need to make my app work with fragments. The only thing i need is to make it that on each different level my app would select specific questions from the question list and display them with their values.
Right now i create questions in level activity. For example: Level1Activity
//Creating questions. (Question, boolean, answer).
final Question first = new Question("Do i understand this code?", true, "Only Jesus knows");
final Question second = new Question("Why dont i understand this code?", false, "Im not Jesus");
final Question third = new Question("Why I am not Jesus?", true, "2fat.");
//Creating Lists for questions and boolean values.
final ArrayList<Question> questions = new ArrayList<Question>();
final ArrayList<Boolean> type = new ArrayList<Boolean>();
//Adding questions to the question list
questions.add(first);
questions.add(second);
questions.add(third);
// Adding boleans to the boolean list
type.add(first.correctAnswer);
type.add(second.correctAnswer);
type.add(third.correctAnswer);
So what do i need to do so that I would not need to create a new activity with new questions on each level. Ho to make one activity and display them with fragments for all levels?
Probably you do not need fragments either. You can have multiple layouts (used via setContentView(int id)), or even better, have one single layout with several instances of TextView on it, and set the text in these text views programmatically.
By the way, you can startActivity() and invoke finish() on the current activity (I am not saying you should do namely this, but you can).
You must start using the same fragment in all levels, and load dynamically the questions. Using one activity for each level is a really bad idea.
First, try to create a fragment, inflate it and use it, and model ONE level. Perhaps the (parent/holder) activity can control the current level, and can pass the questions to the fragment accordingly, and save attributes such the score or the game state.
Note that from the MVC (Model-View-Controller) viewpoint an Activity is a Controller and it is wrong to keep Model data in the Controller. The View hierarchy (the View) and the Activity (the Controller) are re-created when the screen turns. Better use a separate class, probably, a singleton, to keep the data that must survive screen turns.

Designing two column Android application - switching views?

I am building a two column application for Android and I'm wondering how to do the navigation. The left column is the navigation bar. The right one is the content view. Both of them are LinearLayouts.
I have a different activity for all the options. How do I implement my menu into these? It is the same for all the activities (except the current one is highlighted), so copying the same code multiple times seems waste and makes it harder to change it later because I would have to change all the files.
I also have to change the title for every activity.
The typical answer would be Fragments. Here's a great tutorial on that topic.
However, depending on the triviality of your requirements, you could also consider using a horizontal LinearLayout containing your two original LinearLayouts.
in my opinion you should use fragments for your columns.
http://developer.android.com/guide/components/fragments.html
you Should use Fragment control for this. you can call content Fragments on right side (Content View) area with the click on leftSide(Content item/Index) .
I feel you should follow this link.
http://developer.android.com/training/multiscreen/screensizes.html
Am not sure but hopes u asked for the same.
Thanks..

How to Determine XML layout for Fragments in Tabs?

Okay, So I just started Android development (I am average at VB.Net, so I understand basic code even if its not in VB). I've made a couple play around apps which used Text-To-Speech, Async Tasks and Reading/Writing files. However, I now wish to make a tabbed Android app.
I started the project in Eclipse Juno and filled in all the details. I then selected (For navigation) the Tabs/Swipe layout. I now have the default code for that layout type (Link to Tabs on developer.android.com - http://developer.android.com/design/building-blocks/tabs.html).
I understand mostly what the default code is doing. The only problem I am having, is determining the individual layout of my Tabs. I wish to have 2 Tabs, 1 in which the user selects an option, and the other, in which an image is shown depending on the selection in Tab 1.
So the question is: How do I create a .xml file in Layout to determine what is shown on the Fragment?
If you want to do this in XML the answer is simple, it can't be done just with XML, you must create a class that's implementing a ActionBar.TabListener.
Than you can override the onTabSelected method in which you can exchange the content.
A proper solution would be:
Use a LinearLayout as root container, and implement two Fragments for each of your Tabs (there you can design an individual XML-layout). Now you can add one fragment initially to the root-container and implement the exchange of the layouts inside the onTabSelected method and you are done.

View different images depending on what item in my list view I click

I'm fairly new to Android programming and I've got this project I need to finish and I'm currently stuck.
I've got a standard listview in a Menu class with an array containing around 20 different elements. So what I want to do is load images in an imageview depending on which item in the listview I click, and since I'm a beginner the only idea I had was to make a new activity for each imageview which seems like a pretty bad way to do it since I'd need about 20-30 new activities..
To sum things up what I want is:
Code for making ONE activity that will display a different image depending on which item in the listview I click, probably pretty basic coding I want as simple solution as possible.
If possible I'm also looking for a solution that includes an SQLite database that stores the URL of an image and then display it in a single activity, also depending on which item I press in my current listview.
(I hope you understand my needs, and if you need I can also post my current code for the Menu class if it helps you help me) Or you can just show me a different way to make this work, I appreciate every answer! Thanks in advance!
NOTE
And please keep in mind, I'm a noob at Java and Android so keep it rather simple or at least explain what you do.
When you click on a list item, display the image in another view in the same layout, unless you want the image to take up the entire screen real estate. If you want it in the entire screen, go to a new Activity by sending the activity an Intent.
Activities are the "controller" of your application. They interact with the visible UI and the input from the user. You don't need a separate activity for each image, just an activity that's associated with a "place" in the UI (an ImageView) where you'll display the image.
I'd start by adding the images as resources under res/drawable before going on to databases.
You are going to have to do most of this yourself. There really isn't any substitute for taking the time to learn Java and Android. There are several tutorials and Android University classes under the Resources tab in the Developers Guide; I suggest you do all of them.

Categories