Android java intents and activities - java

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?

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

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

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

Related

Changing activity crashes

My app crashes when I try to navigate to another activity. Why does that happen?
I'm able to start the other activity when I launch it at first so there's no problem in the CheckUsernameActivity.
public class CheckNumberActivity extends AppCompatActivity {
EditText phoneNumberEditText;
Button countryCodeButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_number);
Button button = (Button) findViewById(R.id.okButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
countryCodeButton = (Button) findViewById(R.id.countryCodeButton);
phoneNumberEditText = (EditText) findViewById(R.id.phoneNumberEditText);
Log.v("areaCode", countryCodeButton.getText().toString());
Log.v("phoneNumber", phoneNumberEditText.getText().toString());
Intent k = new Intent(CheckNumberActivity.this, CheckUsernameActivity.class);
startActivity(k);
}
});
}
}
Try This way. I think it will help you.
public class CheckNumberActivity extends AppCompatActivity {
EditText phoneNumberEditText;
Button countryCodeButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_number);
Button button = (Button) findViewById(R.id.okButton);
countryCodeButton = (Button) findViewById(R.id.countryCodeButton);
phoneNumberEditText = (EditText) findViewById(R.id.phoneNumberEditText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v("areaCode", countryCodeButton.getText().toString());
Log.v("phoneNumber", phoneNumberEditText.getText().toString());
Intent k = new Intent(CheckNumberActivity.this, CheckUsernameActivity.class);
startActivity(k);
}
});
}
}
I didn't have my new activity defined in my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dimsumdev.runk" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".activity.CheckNumberActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.CheckUsernameActivity" >
<!--Default Intent Filter-->
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".activity.HomeActivity" >
</activity>
</application>
</manifest>

Unable to start an activity on button click

I have a button on main activity which onclick is supposed to start a activity
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v){
if(v.getId()==R.id.lbutton)
{
Intent i = new Intent(MainActivity.this,Display.class);
startActivity(i);
}
}
}
lbutton is the id of button
Display.java
public class Display extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondsc);
}
}
secondsc.xml is a layout file which contains content for the new activity
Manifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Display">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
remove intent-filter to DisplayActivity in your manifest file:
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
And register your clickListener button
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.lbutton).setOnClickLIstener(new OnClickLIstener (){
void onClick (View v) {
onButtonClick(v);
}
});
}
You need to add an event listener to the button. You can either implement the View.OnClickListener interface and implement the onClick method (good if you have multiple buttons that need listeners), or use an anonymous class:
btn.setOnClickListener(new View.OnClickListener() {
onClick (View v) {
}
}
Are you sure that the OnButtonClick is being executed? I recommend doing it this way:
Button lButton = (Button) findViewById(R.id.lButton);
lButton.setOnClickListener(new OnClickListener() {
onClick(View view) {
Intent i = new Intent(MainActivity.this,Display.class);
startActivity(i);
}
}
-Daniel

multiple intent filter to multiple activity

i have two activity associated with two buttons
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Subactivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:taskAffinity="com.example.start_cs.sub">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".sub"
>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" android:taskAffinity="com.example.start_cs.main"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".main"
>
</activity>
</application>
MainActivity code
package com.example.start_cs.myapp;
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.main_text_view);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, main.class);
startActivity(intent);
}
});
}
layout code
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/main"
android:background="#drawable/main"
android:layout_centerHorizontal="true"
android:id="#+id/main_text_view"
android:layout_marginTop="17dp"
/>
in this situation
Subactivity button opened sub
but MainActivity button did not open main
but if i put MainActivity above Subactivity
Subactivity button did not open sub
but MainActivity button opened main
ok, based on your responses to my comments, I guess all I can say is that you need to follow a tutorial on how to create an app for android. There are excellent tutorials available all over the internet.
You have two activities, so you should have two layout files. You only posted one.
You should also have two source files, one for each activity. You only posted one.
Every android element in a layout that you would like to reference in your code needs an ID. your code references R.id.main_text_view, but your layout file has no such id in it. I'm surprised your code even compiles.
However, to answer your specific question, what you need is the following:
1) The <name> tags in your manifest file must match the name of your java class source files for each of your activities. So your activity class files appear to be called "MainActivity" and "Subactivity" according to your manifest file. However, see my comment on your onClickListener code below.
Also, your manifest indicates that both of your activies are "LAUNCHER" activities. You only need that tag for activities which you want to be able to launch from the Android application launcher (i.e. the list of all the apps installed on your phone). Seems like you would only want this on your main activity, but you could specify more than one if you like.
2) Your activities are duals of each other (i.e. they sound like they do exactly the same thing - each has a button that launches the other) so the code will be very similar. Your code for MainActivity should look something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.main_text_view); //<-- This tries to find a button in this activity (using the activity's layout file that was used in the call to setContentView() in onCreate(). However, the id you specify doesn't exist in your layout file. This should either not compile or return null.
//This is fine.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, main.class); //<--"main.class" doesn't match either of the activity names declared in your manifest. It should match one of the names declared in the <name> tag of one of your <activity> tags.
startActivity(intent);
}
});
}
Your layout file needs to contain an id for the button that wish to find using findViewById(). Modify your layout file as follows (and create one for each activity - although you could, technically, reference the same layout for each activity. But for now, it is conceptually easier to have separate files).
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/main"
android:background="#drawable/main"
android:layout_centerHorizontal="true"
android:id="#+id/main_text_view" <!-- Here is the line that identifies the button for your app. The format is "#+id/some_name", and is reference as "R.id.some_name" in your code. -->
android:layout_marginTop="17dp"
/>
Now you must do the same in your sub-activity code, but your onClickListener will call the Main Activity instead of your subactivity. So the onClickListener code looks like this for your MainActivity (which launches your subactivity):
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subactivity.class);
startActivity(intent);
}
});
and like this in your subactivity (which launches your Main Activity)
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
}
});

activitythread.performlaunchactivity(activitythread$activityclientrecord intent) source not found

then when i press f8 a i get : ZygoteInit$methodandargscaller.run() source not found
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.copyup"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.copyup.MainActivity"
android:label="copy up" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Game"
android:label="Copy Up">
</activity>
<activity
android:name=".Rules"
android:label="Copy Up!">
</activity>
<activity
android:name=".Scores"
android:label="Copy Up!">
</activity>
<activity
android:name=".LearnCircle"
android:label="Copy Up!">
</activity>
<activity
android:name=".LearnHoriz"
android:label="Copy Up!">
</activity>
<activity
android:name=".LearnVert"
android:label="Copy Up!">
</activity>
<activity
android:name=".LearnMenu"
android:label="Copy Up!">
</activity>
</application>
</manifest>
and my code:
package com.example.copyup;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button start, rules, hs, learn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linktoxml();
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, Game.class);
MainActivity.this.startActivity(myIntent);
}
});
/*
rules.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, Rules.class);
MainActivity.this.startActivity(myIntent);
}
});
hs.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, Scores.class);
MainActivity.this.startActivity(myIntent);
}
});
learn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, LearnMenu.class);
MainActivity.this.startActivity(myIntent);
}
});*/
//show shape, take reading after 4 seconds, compare with other, if true next, if false, end game
//dont make it complex with time reduce yet!!
}
private void linktoxml() {
start = (Button) findViewById(R.id.bcstart);
rules = (Button) findViewById(R.id.brules);
hs = (Button) findViewById(R.id.bhs);
learn = (Button) findViewById(R.id.blearn);
}
}
I have tried cleaning the project, re-writing the manifest and everything i can possible think of. It works if i comment out the links to the buttons in the code and leave them in the manifest but the moment i uncommnet them i get these errors, please help!!!
The problem is that the activity throw exception.
eclipse looks for the source code in android SDK and can't find it.
Just debug your code and find out where the code throw exception.
solved it, feeling pretty stupid now, needed bstart not bcstart when the button is defined.

Multiple Layout Android Application

I have an Android program using the IntelliJ 12 Community Edition IDE. I need to navigate between the different layouts (.xml) I made with the use of buttons. But, whenever I run it using the emulator, it only opens the main.xml screen, whenever I click the buttons, it says that: "Unfortunately, ITax has stopped working."
This is my code for the MyActivity.java class:
package com.example.ITax;
import android.app.Activity;
import android.content.Intent;
android.os.Bundle;
android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.btn_info);
Button button2 = (Button)findViewById(R.id.btn_tutorial);
Button button3 = (Button)findViewById(R.id.btn_calc);
Button button4 = (Button)findViewById(R.id.btn_back_from_calcu);
Button button5 = (Button)findViewById(R.id.btn_next_from_calcu);
Button button6 = (Button)findViewById(R.id.btn_back_from_monthlyorannual);
Button button7 = (Button)findViewById(R.id.btn_next_from_monthlyorannual);
Button button8 = (Button)findViewById(R.id.btn_back_from_civilstatus);
Button button9 = (Button)findViewById(R.id.btn_next_from_civilstatus);
Button button10 = (Button)findViewById(R.id.btn_back_from_inputamount_monthly);
Button button11 = (Button)findViewById(R.id.btn_compute_from_monthly);
Button button12 = (Button)findViewById(R.id.btn_back_from_outputamount_monthly);
Button button13 = (Button)findViewById(R.id.btn_home_from_outputamount_monthly);
}
public void Open_BasicInfo()
{
Intent i = new Intent(this, OpenBasicInfo.class);
startActivity(i);
}
public void Open_Tutorial()
{
Intent i = new Intent(this, OpenTutorial.class);
startActivity(i);
}
public void Open_Calculator()
{
Intent i = new Intent(this, OpenCalculator.class);
startActivity(i);
}
public void Open_MonthlyOrAnnually()
{
Intent i = new Intent(this, OpenMonthlyOrAnnually.class);
startActivity(i);
}
public void Open_CivilStatus()
{
Intent i = new Intent(this, OpenCivilStatus.class);
startActivity(i);
}
public void Open_InputAmountsFromMonthly()
{
Intent i = new Intent(this, OpenInputAmountsFromMonthly.class);
startActivity(i);
}
public void Open_OutputAmountsFromMonthly()
{
Intent i = new Intent(this, OutputAmountsFromMonthly.class);
startActivity(i);
}
public void Open_Main()
{
Intent i = new Intent(this, MyActivity.class);
startActivity(i);
}
}
I already created a class for each layout and declared in the Android manifest like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ITax"
android:versionCode="1"
android:versionName="ITax 1.0">
<uses-sdk android:minSdkVersion="1"/>
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name=".MyActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".OpenCalculator">
</activity>
<activity android:name=".OpenCivilStatus">
</activity>
<activity android:name=".OpenInputAmountsFromMonthly">
</activity>
<activity android:name=".OutputAmountsFromMonthly">
</activity>
<activity android:name=".OpenTutorial">
</activity>
<activity android:name=".OpenBasicInfo">
</activity>
<activity android:name=".OpenMonthlyOrAnnually">
</activity>
<activity android:name=".OpenMain">
</activity>
</application>
</manifest>
I already put the code android:onClick = "name of method" in all of the buttons.
Help! what seems to be the wrong I did here? :(
My application in the emulator stops working whenever I already clicked the buttons. >.<
In order to comply with the xml android:onclick="MethodName". Your activity must implement method that looks like this...
public void MethodName(View v){}
All of your onclick events are missing passing the View parameter.
Personally I don't like using the xml, because if you make a spelling mistake in your code. Your activity will crash because it can't inflate the xml. This imo leaves you vulnerable to silly mistakes. Here a link in more details about all the ways to do a button onclick event In the end it's up to you.

Categories