Android Eclipse View/Intent inquiry - android

So I'm taking a mobile programming class that's covering all 3 mobile OS's (android, iOS, WinMobile 8) and we haven't learned Java yet at all (it's mostly a game programming program, they are experimenting with mobile development now). So my question is basically I am trying to understand code that we are given as an example, so I'm going to post code below, and I'm only sure of what the code means up until I call a clickListener, I'm just trying to figure out what the code is doing so I can understand it. (We don't have any resource at our school for mobile development yet, this is the first class they are teaching on it). Thank you in advance and I appreciate any help given.
What I think this is doing:
-creating a link from Java to XML with "Button" and linking it to the id "button1"
-listening for a click
-when button is clicked:
-create a new intent (from my research 'intents' are glue between activities, though i'm still not sure what exactly this means) <-- this is where I am lost.
Button btn = (Button)this.findViewById(R.id.button1);
btn.setOnClickListener(myListener);
}
// Create an anonymous implementation of OnClickListener
private OnClickListener myListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
Intent myIntent = new Intent(v.getContext(), SecondPage.class);
String text = ((EditText)findViewById(R.id.editText1)).getText().toString();
myIntent.putExtra("Text", text);
startActivityForResult(myIntent, 0); } };

Here is a commented form of your code:
// Create an anonymous implementation of OnClickListener
private OnClickListener myListener = new OnClickListener() {
// do something when the button is clicked
public void onClick(View v) {
// An intent tells the android system that you intend to start a new activity.
// This can be done explicitly by giving the class object of the activity to be started
// or it can be done implicitly by providing a string identifying the type
// of action you wish to perform (like read a bar code). In this case you are
// explicitly stating that you wish to start the SecondPage intent.
Intent myIntent = new Intent(v.getContext(), SecondPage.class);
// get some text from an editText control in the current view
String text = ((EditText)findViewById(R.id.editText1)).getText().toString();
// Add that text as an extra bit of data to the Intent. This will allow the
// Activity that is started to read the extra bit of data.
myIntent.putExtra("Text", text);
// Start an activity with the given intent and tell the android system that you
// are expecting some sort of result from the launched activity (like a barcode or an image)
startActivityForResult(myIntent, 0); } };
For information on the Intent, you can look at the javadoc, which states:
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
see: http://developer.android.com/reference/android/content/Intent.html

Related

Can I use the same button on multiple Android activities and return to original activity?

I'm new to Android and making a very, very rough sketch of how my app will display information to users. I am making the app but not providing all the text it displays.
What I want to do is have multiple activities with a text box and underneath it have a button using the oncreate method (I believe the correct method) to have the exact same button on each activity linking back to main activity. What I want to do is make it so I don't have to recreate the intent or the button code on every page, just make a single line that does like "display button ID=home" on each activity. Just want to make it as simple as possible because I'll be making a lot of activities with text display.
When I figure it out I'm just going to drop the buttons and use a navigation drawer to access the info but need a draft for the team to start organizing the Information it displays.
I hope explaining what my final product will be can help show what I want to do. Thanks for any help in advance!
Create button programmatically.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button button1 = createButton(this);
layout.addView(button1);
public static Button createButton(Activity a) {
Button b = new Button(a);
b.setLayoutParams(layoutParams);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(a.class.this, MainActivity.class);
startActivity(intent);
}
})
return b;
}

Are activities created by intents of the same class, the same activity?

If I create a new Intent of the same class every time I click a button, is the created activity the same?
Every time I click a button, I want to have a dialog show with a slider inside it and after I change it I want the state to be saved so that the next time I open up the dialog the state of the slider is the same.
My code for the button is this:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Slider_Logic.class);
v.getContext().startActivity(intent);
}
});
By "same," I assume you mean the same object instance. The answer is no. In general, when you start a new activity, it creates a new instance of that activity and pushes it onto the stack in front of the existing activity.
I say "in general" because the activity's launch mode can effect this behavior. For example, if you set launchMode to singleTop, it will create a new instance of the activity if one doesn't already exist in the target task. Please see the docs for more information.

How do i create Intent and to where to put the codes?

I am trying to follow the lesson here and now im stuck on "Building an Intent". I am quite confused for how to make this Intent and where to paste it. Can someone show me the step by step process on this tutorial? I am getting massive headaches now. Please I want to learn to do this.
Build an Intent
An Intent is an object that provides runtime binding between separate
components (such as two activities). The Intent represents an app’s
"intent to do something." You can use intents for a wide variety of
tasks, but most often they’re used to start another activity.
Inside the sendMessage() method, create an Intent to start an activity
called DisplayMessageActivity:
Intent intent = new Intent(this, DisplayMessageActivity.class); The
constructor used here takes two parameters:
A Context as its first parameter (this is used because the Activity
class is a subclass of Context) The Class of the app component to
which the system should deliver the Intent (in this case, the activity
that should be started)
As the tutorial says, you need to add the line of code that creates an new instance of the Intent class. You will use this instance later to tell the OS to launch another activity or a service. In this particular example, the Intent you are building will direct the OS to launch the DisplayMessageActivity.
To do this step properly, you need to modify the sendMessage method that you have added in the previous step of the tutorial. The final method should look something like this:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
After creating the Intent, the code will take the content of the editText control in the current activity, assign it to the message variable, and then add it as an additional parameter to the intent, so that the target DisplayMessageActivity activity can do something with it.
Don't worry about the DisplayMessageActivity yet. It will be added in a later step.
How do i create Intent and to where to put the codes?
You want to open the activity using intent then you can write your code in this method.
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}

Android dev - how to create buttons on the home screen that lead to actions

I just compiled and ran the hello world app and it worked. And I read through a bunch of stuff on the android documentation about the diff components of android and how it all works together. Now I want to make a few buttons which link to various actions.
For example, I want to make a button that goes to a new screen. Is there a tutorial for this sort of a thing? Or maybe someone could explain to me how to do that?
Thanks!
This is really simple. To make a button that goes to a new screen you should put a new button on your XML layout, and assign it an id. After you've done this, in your code, you need to do something like the following:
Button mButton = (Button) findViewById(R.id.mybutton);
mButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
Hope this helps you.
Also, be sure that the activity that you're trying to start A) Exists, and B) is in your manifest.
The Form stuff tutorial is really useful. I believe that's the next thing you want to learn after HelloWorld.
To start a new screen call startActivity() or startActivityForResult(), depending if you intend to get data back from the new activity. You also want to learn something called Intent, which you can add information to it and pass between screens(or activity). Using intents are most often used for stating a new activity (Android dev Guide for Acticities).
Hope that's helpful.

How to connect GUI screens in Android ?

How to connect GUI screens in Android? I have created several screens and I want to connect these using buttons.
It depends what else you want to do by clicking a button. If you only want to click a button and go to next activity or you want to send some data to next activity. Or maybe you want to dial a number or view contacts etc.
Here's a nice tutorial about using startActivity() and startActivityForResult() methods - android startActivity and startActivityForResult
Also basic Intents Tutorial will be useful.
just take a event handler .. in that use INTENT
Here s the syntax , startActivity(Intent(activity1.this, activity2.class);
ex:
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(activity.this, activity2.class);
startActivity(i);
}
});
Here !st activity1 i..e.. ur current class(1st page) name
Intent is the best mechanism to connect your component(activties, services) run time. here you can find about intent class.

Categories

Resources