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.
Related
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!
I'm having a problem where I just don't know how to save input user data and then recall it. Let's say I want to store someone's name, so I believe I would use an Edit Text box, but that's all I can really figure out. I then later want to display that information in another activity. If you could link me to something or guide me in the right direction it would meant the world to me.
Well if you want persistent storage even after the user closed the app, SQLite or sharedpreferences is for you. Otherwise you can juse use Bundles to pass around the small bit of data in intents when starting a new activity.
SQLite guide,Sharedpreferences guide. Hope these helped.
Use an EditText to allow the user to input their name. Get the value from the EditText component via the EditText's getText() method. After that you have lot of different ways you can go. Save it for later or just pass it on via an intent to the next activity.
Good luck.
The term "Storing" should have a scope. Say, you are gonna store the data for a specific code block, or at activity level, or at application level, or you want persistent(storing data in Preferences or Database, for example, storing the game score, etc) or non-persistent(temporary storage: declaring the variables in the activity) storage.
In your case, to get the Text from EditText and send it to next activity:
Declare a global variables,
EditText editText;
String data;
// write this inside onCreate, after the `setContentView()` method
editText = (EditText) findViewById(R.id....);
data = editText.getText().toString();
// say you want to pass the data to next activity on button click, so write this inside //onClick()
Intent intent = new Intent(currentActivityName.this, NextActivityName.class);
intent.putExtra("data", data);
startActivity(intent);
Up till now what we have done is, get text from EditText, and prepared for sending it to next it to next activity. Now, on button click, the data will be sent to next activity.
But on next activity, you need to catch the data, that was sent from previous activity, to use it.
So, in the next activity, write the following code inside onCreate()
Intent intent = getIntent();
String dataReceived = intent.getStringExtra("data");
Log.i("data received", dataReceived);
After doing this check if you got the log, with tag name "data received" and data you sent.
I've to develop an application that allows user to browse picture and write some information
about him, all these information will stored in a class with Person name,
After he presses NextButton the activiy should moves him to another activity with these information in the second activity he'll type another information about him,
well in the 3rd activity I should receive all these info. from ( 1st and 2nd ) activities then, showing it in the 3rd one ,,
my Questions are:
1- How can move more than one info. I write a code that moves string and other code to move picture, but I couldn't combine them with each other!
2- How can I insert the information that will be typed in the second activity to the same object of Person Class ??
Hope my Questions and my scenario is clear!!
thanks alot
Shomokh =)
-----------------------Updating----------------------------------------
// this is for String info
String FullPersonInfo = person1.toString();
Bundle basket = new Bundle();
basket.putString("key", FullPersonInfo);
Intent intent = new Intent(Form_1.this,Form_2.class);
intent.putExtras(basket);
startActivity(intent);
// I'm confusing how can I add image when i try this code it doesn't work
intent.putExtra("URI", selectedImageUri.toString() );
You can pass in extra information in the intent you are passing in to the next activity and then read the intent using intent.getExtra in the 2nd and 3rd activity. I hope that gives you better idea.
As omkar.ghaisas aluded to above you can pass extras to an Intent. However, you can only pass primitives OR any object which implements the Parcelable interface. Passing string is easy enough but the image data is trickier.
I think you have a couple of options:
Write the image path to disk and then pass the file location as a
string using the Extras in the Intent, plus any other strings you
need, e.g. other metadata
Grab the image as a byte[] array, create a class which implements
Parceleable and shuffle the byte[] around.
I think #1 is probably easier.
you can Create Parcelable object of your class that content all information whcih u want to move from one activity to another.use Bundle.putParcelable and extract all class object in another Activity.
for passing Parcelable custom or complex object between activites see:
Passing a list of objects between Activities
http://prasanta-paul.blogspot.in/2010/06/android-parcelable-example.html
Android: Passing object from one activity to another
and as your application requirement but do't pass whole image in bundle just pass a reference path and text to another activity and retrieve image from path
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#
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 ;)