I am learning to build android applications and I need some specific help. I can't seem to get my head around which bits of template code I am required to change, and which bits are static.
In the LAYOUT folder I have my ACTIVITY_MAIN.XML which reads
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/main_buttons_photos" />
</LinearLayout>
Next, I have my second activity ACTIVITY_SEND_PHOTOS.XML which is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".SendPhotos" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/title_activity_send_photos"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
I then have my MainActivity.java (is this the .class?) this reads package com.example.assent.bc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}
and then my SendPhotos.java file which is;
package com.example.assent.bc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class SendPhotos extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_photos);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_send_photos, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
I would like the button in my main activity to link through to my sendphotos activity, simply opening up that activity, nothing fancy, not sending any data or anything.
I know that somewhere I need my
Intent i = new Intent(FromActivity.this, ToActivity.class);
startActivity(i);
but I have no idea what to replace ToActivity.class with or what else I need where.
You can move to desired activity on button click. just add
android:onClick="sendMessage"
this line.xml:
In your main activity just add this method:
And the most important thing: don't forget to define your activity in
manifest.xml
The answer for the complete noob from a complete noob:
MainActivity
is the name of the first activity.MainActivity2
is the name of the second activity.button1
is the I.D of the button in xml forMainActivity
Activity.Replace the below line code:
Add the below lines in your manifest file:
This always works, either one should be just fine:
IN JAVA CODE
}
From Activity : where you are currently ?
To Activity : where you want to go ?
Intent i = new Intent( MainActivity.this, SendPhotos.class); startActivity(i);
Both Activity must be included in manifest file otherwise it will not found the class file and throw Force close.
Edit your Mainactivity.java
now Write code for click event.
Hope it will work for you.