• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Linking two Views together?

SC2011

Lurker
Hey everyone,

First, I'm completely new to this whole developing thing, so far, i'm a fan.

Second, My issue.

I'm trying to link two Views together. By view (I think it's called a view), I mean I'm at a home screen, push a button, and i'm at another screen with buttons, then depending on which one is pushed, another view, and so on. The closet thing I found to it was in the API demo Redirect. I've completely dissected the demo and done the Hello World's but can't figure out how to link the two screens.

I can post codes of the demo or my project if need be, but I think this issue has something to do with my Button line. I'm using:

Button button = (Button)findViewById(R.id.start);
button.setOnClickListener(onClickListener);

I think what's in red, is somehow causing my issue.

Any words of wisdom are welcomed...
 
Hey everyone,

First, I'm completely new to this whole developing thing, so far, i'm a fan.

Second, My issue.

I'm trying to link two Views together. By view (I think it's called a view), I mean I'm at a home screen, push a button, and i'm at another screen with buttons, then depending on which one is pushed, another view, and so on. The closet thing I found to it was in the API demo Redirect. I've completely dissected the demo and done the Hello World's but can't figure out how to link the two screens.

I can post codes of the demo or my project if need be, but I think this issue has something to do with my Button line. I'm using:

Button button = (Button)findViewById(R.id.start);
button.setOnClickListener(onClickListener);

I think what's in red, is somehow causing my issue.

Any words of wisdom are welcomed...

Heya,

First, just a little education on the subject matter, a "view" is any element that is displayed on the screen (such as a button, or a text box, etc...). The "screens" you are referring to and want to link are called, "activities."

Now, with that said, let's get on with the code!

This is actually a very simple process and you better get the hang of it now, because it becomes prominent in just about every android app you will make!

Going along with your previous post, first you must make your activity implement the OnClickListener. To do this, you would have something like:

Code:
public class MyActivity extends Activity implements OnClickListener
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}
}

That is the framework of your first activity.

Next, you will want to define a class variable.

Code:
Button button;

You will then initialize this variable and set its OnClickListener within the onCreate() method like so:
Code:
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		button = (Button)findViewById(R.id.start);
		button.setOnClickListener(this);
	}

Now here is the meat of the code. You will implement the method onClick() and start a new activity via an Intent.

Code:
	public void onClick(View v) 
	{
		startActivity(new Intent(MyActivity.this, ActivityToBeCalled.java));
		
	}

I hope this helps!
 
Thanks for that clarification! It doesn't help my research efforts went to researching connecting different views, haha. Thanks for the help!
 
Back
Top Bottom