Android bundle



hello, i’m edu from edu4java and this is android tutorial number 5 the title of this tutorial is: navigating with intents, we are going to navigate from our form which we created in the last tutorial to another activity using an intent the other activity will be called result


Android bundle, the first thing we are going to do is take this button “add” “agregar” in spanish and we are going to fill the property onclick the onclick in a button says which button is going to catch the click


ok, onaddclick… is the name of the method which is going to catch the click ok, “cancelar”, for the cancel button iâ´m going to put the oncancelclick method this textboxes were autocompletetextviews, i need an identifier in order to get the content from the java code then, i am going to put an identifier title… book title for the title book author for the author and book isbn for the last text box in that way iâ´m going to catch the content from the java code now we are going to create the second activity the activity which is going to receive the intent from our form activity


ok, here i am going to add a result layout that i have already prepared and i need two new strings constants, one: result, “resultado” in spanish and “resultok” which is: “inserciã³n exitosa del libro”; the register of the book is ok now, let’s check the result xml. result xml has here the string result as a name of the text view that we put as the title… ok, you can see in the layout i have a another text view which uses an identifier because we are going to use this identifier from java code to put the data


that came from the other activity the data we are going to send through the intent if you remember, intent, was like a message from one activity to another activity ok, we need to add the activity to the manifest if you remember the manifest was the principal file here i put an activity, the name .result, this means that we have to create a class result in the default package and i put a label for this activity which is a result; string result constant


here, i bring the new result class which i have also prepared that is the one we were talking about, is going to use result xml it is already added in the android manifest xml ok, i think we have everything ready let’s go to form and we are going to add the methods to handle the onclick events if you remember we create two methods here onaddclick and oncancelclick… if you remember the only thing you need is a method with a view as a parameter


ok, let’s see what we are going to do if someone clicks on cancel the first thing we are going to do is to create a new intent in the intent we are going to set the components with a new component that we create using a reference to this and result class which is the class for the new activity then, we are going to use putextra to add data to the intent in this case we are going to add a string called cancel as a data with a label “cancel” to end this we are going to call “start activity”


and we are going to send an intent; start activity… start activity is a method that we get from the activity class the result class is the one which is going to receive this intent and in oncreate, as you can see, the typical call to super (the father) and setcontentview with the layout result xml which we had already put, it is the same as any activity and then we are going to get the textview component which we call result text in the id, if you remember, in result.xml here, we defined a text view with that id


with this findviewbyid we can get the component that we are going to use to show the result in the action activity ok, getintent gives me the intent that made the call getextras gives me the information in the intent this information came in a bundle object with the getstring method we ask the bundle if he has a cancel data inside if this is not equal null that means that it has then, we are going to set the text in the result text as “operaciã³n cancelada” that means “cancelled operation”


let’s go to form to see the onaddclick method, this is going to be the method we are going to get when somebody clicks on addbutton here we get a reference for the three components where we are going to put the title, author and isbn in the form we create a new intent, we set the class in the intent reference to… the result class, this is another way to do the same, to setcomponent, but i want to show you both ways…


ok, we put the data in the intent with putextra, booktitle; we get the booktitle component and we get the text and we put this in the extra we do the same with author and with isbn then, the intent has all the data, we call startactivity and send the intent now, i am going to start the emulator because if you remember the first time takes longer and while we wait for the emulator


we are going to see what happened with the result when the result activity received the addclick the addclick is not going to have… the intent is not going to have in the bundle cancel then we are going to end up here here, we get from the bundle, the booktitle, the bookauthor, the bookisbn, that we are going to use to fill the result text as you can see here, ok, we are going to use a string stored in the string xml,


if you remember, resultok and getstring, this is the way we get a string resource from the java code we send as a parameter the constant which identifies the string results and then you can see here we are going to get “inserciã³n exitosa del libro” and we are going to concatenate with the booktitle, the bookauthor, the bookisbn, ok, letâ´s run the application we have to put a title here, any title, “un titulo” an author; “juan”


an isbn; like “222-333-444” then “agregar” which means add and we get this in the other activity; “inserciã³n exitosa de un libro…juan…” ok, between “libro” and “un” there is no space we have the space here but it doesn’t work ok, we can fix this in… result we can put just here a space and this is all,ok, this “operaciã³n cancelada” is not ok here, we have to put this as a resource,


the only thing we have to do is get this string, change this string by a constant and put this string in a string xml as a new entry ok, here, we put this as cancelop and we go here and just put getstring and the id r.string.cancelop… i have to save it in order to get the compilation string.cancelop, ok… let’s cancel and we get “resultado” and “operaciã³n cancelada”,


Android bundle

as you can see here we get the oncancelclick


and in the result we get the getstring cancel different from null and we get the cancel ok, that’s all for this tutorial, see you in the next one!!


Android bundle Rating: 4.5 Diposkan Oleh: PaduWaras