pjmil
pjmil

Reputation: 2097

Android java intents and activities

Bear with me, this is my first Android project.

I've been making a simple Java class, Event, and made the CRUD functions for it. Now I'm doing the layout and can't figure out how I launch these activities after a button is clicked. This is the code I'm working with.

activity_main.xml;

<Button
        android:id="@+id/newEventButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="New Event" />

MainActivity.java;

public void addEvent(MenuItem item) {
        Intent i = new Intent(this, AddEventActivity.class);
        startActivity(i);
    }

AndroidManifest.xml;

    <activity
        android:name="com.example.eventmanager.AddEventActivity"
        android:label="@string/title_activity_add_event" 
        android:parentActivityName="com.example.eventmanager.MainActivity" 
        android:noHistory="true">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.eventmanager.MainActivity" />
    </activity>

What do I need to include into these segments that will link the newEventButton to launch AddEventActivity.class?

Upvotes: 0

Views: 146

Answers (3)

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

<activity
    android:name="com.example.eventmanager.MainActivity"
    android:label="@string/title_activity_add_event" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
<activity
    android:name="com.example.eventmanager.AddEventActivity"
    android:label="@string/title_activity_add_event" 
        <intent-filter>
            <action android:name="android.intent.action.ADDEVENTACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</activity>

Here u can work like this also

EDIT Dont you set your onclick listener if no copy this in oncreate

Button b = (Button) findViewById(R.id.newEventButton);
b.setOnClickListener(new onClickListener()
{
@Override
 public void onClick(View v)
{
Intent i = new Intent("android.intent.action.ADDEVENTACTIVITY");
startActivity(i); 
 }
}); 
}

Upvotes: 0

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

In your main activity in onCreate after setContentView add the following code

// get newEventButton    
Button addEvent = (Button) findViewById(R.id.newEventButton);
// Listen for button click and start AddEventActivity 
addEvent.setOnClickListener(new onClickListener()
{
    @Override
     public void onClick(View v)
     {
         startActivity(new Intent(this, AddEventActivity.class);
     }
 });

Upvotes: 1

Karakuri
Karakuri

Reputation: 38585

You need to add an OnClickListener to the Button in your Activity's Java code. Normally you do this in onCreate().

public class MainActivity extends Activity implements OnClickListener {
    public void onCreate(Bundle saved) {
        super.onCreate(saved);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.newEventButton);
        button.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.newEventButton:
            Intent i = new Intent(this, AddEventActivity.class);
            startActivity(i);
            break;
        }
    }
}

Upvotes: 1

Related Questions