Linking two Views together?


Last Updated:

  1. SC2011

    SC2011 New Member This Topic's Starter

    Joined:
    Jul 25, 2010
    Messages:
    2
    Likes Received:
    0
    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...
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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 (Text):
    1.  
    2. public class MyActivity extends Activity implements OnClickListener
    3. {
    4.     @Override
    5.     public void onCreate(Bundle savedInstanceState)
    6.     {
    7.         super.onCreate(savedInstanceState);
    8.     }
    9. }
    10.  
    That is the framework of your first activity.

    Next, you will want to define a class variable.

    Code (Text):
    1. Button button;
    You will then initialize this variable and set its OnClickListener within the onCreate() method like so:
    Code (Text):
    1.  
    2.     public void onCreate(Bundle savedInstanceState)
    3.     {
    4.         super.onCreate(savedInstanceState);
    5.         button = (Button)findViewById(R.id.start);
    6.         button.setOnClickListener(this);
    7.     }
    8.  
    Now here is the meat of the code. You will implement the method onClick() and start a new activity via an Intent.

    Code (Text):
    1.  
    2.     public void onClick(View v)
    3.     {
    4.         startActivity(new Intent(MyActivity.this, ActivityToBeCalled.java));
    5.        
    6.     }
    7.  
    I hope this helps!
     
    SC2011 likes this.
  3. SC2011

    SC2011 New Member This Topic's Starter

    Joined:
    Jul 25, 2010
    Messages:
    2
    Likes Received:
    0
    Thanks for that clarification! It doesn't help my research efforts went to researching connecting different views, haha. Thanks for the help!
     

Share This Page

Loading...