How to connect GUI screens in Android ? - 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.

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;
}

Android Eclipse View/Intent inquiry

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

Loose coupling and starting activities

I want to keep my app as loosely coupled as possible, and most of is done with IoC
however, at some point, i need to launch different activities,and the class implementing this activity, could be any,meaning i don't want to define a specific class that starts the activity, but one needs to be set in the intent.
where is the best place to write the code to launch my activities ? is it in the same activity that starts the other activity? or have some outside logic about it?
I have an activity A
from which i need to start activity B
where do i put the logic of
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, requestingB);
It sounds like you're trying to have an activity that can be launched by some other application and you don't want the activity to necessarily know about what is launching it.
Try using an intent-filter within your activity. Then, when something needs to launch it, all it has to do is fire off an intent with the action defined in the intent-filter.
As always, Vogella has a good tutorial here: http://www.vogella.com/tutorials/AndroidIntent/article.html
As the OP mentioned in comments that he wants to start another activity on the click of a button, below is the sample code:
Button myBut = (Button) findViewById(R.id.but1);
myBut.setOnClickListener(new onClickListener()
{
#override
public void onClick(View view)
{
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
}
});
Hope this helps

Android, Calling Activity within itself

I am trying to make an app which have 2 main controls (along with other info fields)
1.> Next button
2.> Done button
I want to call the same activity when next button is pressed and display some other activity when Done button is pressed
Done button is working fine. But when I press Next button the app stops working. The error that I get is :
Unfortunately, myapp has stopped working
This is the same error which I usually get when I don't define activity in manifest file.
Can anyone please help me with this problem.
And finally Is is legitimate to call same activity within itself ?
Thanks
I think this should work
Intent i= new Intent(ActivityA.this,ActivityA.class);
You can use Intent flags to call the activity again. In the button click
setContentView(R.layout.main);
Intent intent= new Intent(this,SameClass.class);
startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
Try this
Intent intent= getIntent();
finish();
startActivity(intent);
Thank you
Why do you need to call the Activity within itself?
You can do following things:
1. You can reset data on next button click.
2. You can hide view or make visible on next button click.
Clear your requirements and show your code to check why the error Unfortunately, myapp has stopped working is coming.
Use TabGroupactivity
In the next button onclick write
public void next(View v) {
Intent next= new Intent(getParent(), next.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("next", next);
}
Intent i= new Intent(ActivityA.this,ActivityA.class);
startActivity(i);
or
startActivity(new Intent(ActivityA.this,ActivityA.class));

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.

Categories

Resources