Android App, checkbox search engine - java

I am creating an app in Android Studio and I am fairly new to java programming. The main idea is that in the main activity of my project I will have a list of different options, with check-boxes next to them.
A user has to click on check-boxes which interest him and then press the button on the bottom "Search". After that a new activity will show and for example if the user picked "Option A" and "Option D" then in the new activity two images will show which I will assign to these two activities.
I am can't figure out a way to actually show these images as a result of the checkboxes selected by the user.
Can someone give me a tip on how it may be possible to do?

You have to know Inents,Activity,Images,CheckBoxes. Please search on the web, After that it is also hard I will implement for you.But right know I will suggest you next one.
1)Learn Activity and How to pass through multiple activity data.
2)How to use Checkbox and How to set Image to ImageView .

You should start learning Java ASAP (also Android basics). I'd recommend learning it, before developing even simplest app.
You can store options in a list. First declare list:
List<String> optionList = new ArrayList<>();
Then, as user checks checkboxes you can add options:
optionList.add("Option A");
// Or whatever the option is
When you start new activity, you can put&send some data:
void openNewActivity() {
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("options", optionList);
startActivity(intent);
}
From within NewActivity's OnCreate method, you can get data:
List<String> options = getIntent().getSerializableExtra("options");
Voila!
Good luck!

Related

Can I create a new Activity on the first click of a button and after the first click open the previously created activity?

I've been developing a Grade/GPA Calculator Android application during my free time and I'm wondering if there's a way to do the following:
In my MainActivity I'm letting the user add as many Semesters as they want and the idea is that when a user clicks on, for example, Fall Semester 2020 the Activity will change to the Activity that will have the Classes the user is taking during that specific semester. My question would be, is there a way I can do that? Because I thought of creating a SemesterActivity, but the problem with that would be that it will always redirect to the same Activity so it will always have the same content so it doesn't matter if the user clicks on Summer Semester 2020 or Fall Semester 2020 it would open the same activity with the same classes that the user had added.
I've been doing some research online about creating an activity on the click of a button, but I've had no luck and I'm not sure if that's really what I should do. If anyone can point me in the right direction I would appreciate it. Thanks!
Edit: Maybe this video might help with what I want to do. As you can see, I click on a specific Semester and it redirects to an empty Activity that has a button that lets the user add new courses and that's what I want to do, but I want to do one for each semester
You would need to use Extra to pass data to SemesterActivity to tell it what semester to render data accordingly. To do that in MainActivity:
Intent myIntent = new Intent(this, SemesterActivity.class);
myIntent.putExtra("semester","Summer2020");
startActivity(myIntent);
in SemesterActivity, you retrieve the Extra and determine the semester:
Intent myIntent = getIntent();
String semester= myIntent.getStringExtra("semester");

Java to XML Android

New to Android Development and was wondering if there was some way of taking the users input to create an activity? For example say the user is going through the process of setting up a profile of themself. One of the questions is "how many pets do you have?". The user inputs "4" and then clicks the 'Next' button (which opens the next activity).
How would I take the users input of "4" to create four editText objects in the next activity so that the user can now input the name's of his/her's pets?
I'm an ok-ish programmer (have never touched XML before though) so you don't need to go into much detail I just don't know how I would access this variable to create the four editText objects. From what I've found out you can't add strings to the resources at run-time nor can even edit/append files in resources.
I was thinking of writing an XML file from java and making the activity (written in XML) read the XML file if that's even possible? Can XML files read XML files?
this has nothing to do with xml
the user's input will be stored in a different data type like a string. If you need to load a new activity, one thing you can do is pass information in an android Bundle type.
you start new activities with intent data types. the intent data type has a putExtra method, where you can put variables in.
after you startActivity(yourIntent) the new activity can call Bundle and that has a method called getExtra associated with getIntent()
these should guide you at least
Intents.
String 1 = et.getText().toString();
String 2 = et.getText().toString();
String 3 = et.getText().toString();
String 4 = et.getText().toString();
Intent i = new Intent(this, newclass.class).
i.putExtras("key1", 1);
// and so on.
// Then you get the data by.
bundle extras = i.getExtras();
string temps1 = extras.getString("key1", null);
More info here: http://developer.android.com/reference/android/content/Intent.html
What you're asking about is not really the creation of activities, but rather the creation of layouts. As you're already aware, Android allows you to define a layout using xml and Activities have a simple method to call to set their content view based on that xml. Layouts are not required to be defined in xml though; they can be programatically created.
When you start an activity you create an Intent for it, and you are able to add "extras" to this intent; by doing that you can pass parameters into your activity and it can use those as hints for how to build the layout.
Do some research on programatic layouts (http://mylifewithandroid.blogspot.com/2007/12/xml-and-programmatic-layout.html might help) and you will probably figure out what you need to know.
You can pass data (in your case user input) with the help of intent.
You can send data by using putExtra. Here is good tutorial for you.
For dynamic layout you can inflate your current layout and add controls to your current view. For dynamic layout this is good tutorial to refer.
I hope this two two tutorial will help you.

How to open an activity twice but with different content?

I'm developing a chat app, but I have a problem.
I've a list with the contacts and when I select one contact I'm starting a new activity
Intent i = new Intent(this, MessageScreen.class);
startActivity(i);
but, when I choose another contact to talk I will use the same activity.
but it always open with the last contact screen and the variables still with the old values.
I would like to make something similar to google talk, where you can start to talk with another contact, and all the messages use the same screen, and you can change between the chats fast with no need to reconstruct the screen, reload the messages, etc..
Anyone have any idea of how to implement this?
Sliding between activities isn't a common feature, it sounds like there is one second activity that has a ViewPager that is populated with multiple chats. When starting this activity, they're probably adding the Reorder to front flag to the intent and have overridden onNewIntent to add a new view to the pager.
Try something like*:
i.PutExtra ("key", value);
before starting the activity (e.g. store the user name) and then read your value from the activity and adjust (e.g. UI) it based on the value
note: syntax could differ a bit since I do my Android stuff from C#

For programming an android app: How do I make it display a one time setup screen

While searching this question I have seen this. One time Android setup screen?
It asks my question, but I can never get it to work. What I want to happen is when I first startup the app, it gives me the setup screen. Then when I press save, I want it to quit the app. When I click the app again, I want it to preform a task, rather than show up with a screen. So thats really 2 questions, how to make it show a setup screen one time, and then how to make it do an action(by clicking on the app) without a screen showing up at all.
Right now, I use SharedPreferences editor to input my settings.
Pretty simple, in the onCreate of your activity:
savedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
if(savedPreferences.getBoolean(PREF_SHOW_ABOUT_ON_APP_START, true)){
Intent intent = new Intent(this, SetupActivity.class);
startActivity(intent);
savedPreferences.edit().putBoolean(PREF_SHOW_ABOUT_ON_APP_START, false).commit(); // YOu could do this line within the SetupActivity to ensure they have actually done what you wanted
finish();
} else {
// Go somewere else
}
You don't have to activity switch, but you get the picture
For the first time only set up screen, you simply have to use SharedPreferences. Check if a value, alreadyLaunched exists in the shared preferences. If not, show the set up screen. If yes show your app.
For the second part of your question, it would be considered very bad practice to have an icon app that does nothing at all when you click it. How would the user know that it worked or did what it was supposed to do ?
For the "I want a task to execute but I don't want the user to see it happening", I believe you should look at services. When your user closes the app clicking the save button on the set up screen, start a service ( http://developer.android.com/reference/android/app/Service.html )
When the user clicks again on your app, simply show a message and make the app call the service via an intent.
Probably you are forgetting to call
myEditor.commit()
after you are done with setting your preferences.

Re-load Android activity data

I am writing an Android app, part of which will be a survey involving multiple pages of checkbox question and answers. I have created an activity to display the question and options (from the DB) and what I want to do now is when i press the "Next" button it should just reload the current activity with the next question set from the database.
(the activity starts with survey.getNextQuestion() - so its just a case of refreshing the activity so it updates)
Im sure this is a simple thing to do -any ideas?
Thanks
Typically you would start a new instance of the activity for the new question. That way if the user hits back, it has the logical behavior of taking them back a page.
It is possible to update the same activity, but it is more complicated.
To open a new activity:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("nextQuestion", x);
startActivity(intent);
Then in your onCreate method, you can pull the next question from the database.
Im sure this is a simple thing to do -any ideas?
It is pretty straight forward, yes. Mayra's answer is perhaps the better one, however here is an approach that will achieve the functionality you specified.
You can use findViewByID(int) to identify the View objects in your layout that need to be updated, and assign the result to an attribute in your onCreate to allow you to access it later.
E.g.
aView = (View) findViewById(R.id.aview);
Your survey.getNextQuestion() can obviously be used to get the next question.
The question can then be placed into the UI by manipulating the Views you obtained in onCreate.
E.g.
aView.setText("your question/answer");
Depending on the number of answers you may need to programatically create/remove checkbox Views from your layout
E.g.
ViewGroup yourViewGroup = findViewById(R.id.yourviewgroup);
View yourView = new View();
//Configure yourView how you want
yourViewGroup.addView(yourView);
youViewGroup.removeView(yourView);
All of this functionality can be contained in a function that is called by the onCreate method and when the next button is pressed.
Don't forget to store the result of the previous question before refreshing ;)

Categories