1

I'm new to Android programming and am attempting to create a new Main Activity for my app (taken from the Android tutorial website). My original Main Activity is called "MainActivity". The new activity I want to be my Main Activity is called "Homepage" and it should contain a button that produces "MainActivity" upon clicking. I'm unsure what or where I'm supposed to include information in the manifest regarding the new page "homepage", homepage.xml, and the button. Specific code would be appreciated.

HomePage:

package com.myphoneapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class HomePage extends Activity {

    private Button ScheduleBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage);

         ScheduleBtn = (Button) findViewById(R.id.home_btn);

        ScheduleBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub


                Intent myIntent = new Intent(HomePage.this, MainActivity.class);
                HomePage.this.startActivity(myIntent);


            }
        });
    }   


}

homepage.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="Welcome to ClearLight" 

    android:id="@+id/home_btn"

    />

</LinearLayout>

MainActivity:

package com.myphoneapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


    public class MainActivity extends Activity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
        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
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

    }

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myphoneapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.myphoneapp.MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.myphoneapp.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.myphoneapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myphoneapp.MainActivity" />
        </activity>
        <activity
            android:name="com.myphoneapp.HomePage"
            android:label="@string/homepage" android name="MainActivity"
            android:parentActivityName="com.example.myphoneapp.MainActivity" >

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myphoneapp.MainActivity" />
        </activity>
    </application>

</manifest>
Nick
  • 51
  • 1
  • 8

3 Answers3

1

To make the HomePage your first activity, edit your Manifest file such that it has the intent filter for action.MAIN. And you dont have to define anything about layouts in the Manifest file. Only the Activity declaration (which you already have)

So your new manifest file would look like

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.myphoneapp.MainActivity"
            android:label="@string/title_activity_main" >

        </activity>
        <activity
            android:name="com.myphoneapp.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.myphoneapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myphoneapp.MainActivity" />
        </activity>
        <activity
            android:name="com.myphoneapp.HomePage"
            android:label="@string/homepage" 
            android:parentActivityName="com.example.myphoneapp.MainActivity" >

            <!-- Move the intent filter to HomePage -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myphoneapp.MainActivity" />
        </activity>
    </application>

</manifest>

And for the button to start mainActivity, you have already done that in HomePage.java

ScheduleBtn = (Button) findViewById(R.id.home_btn);
ScheduleBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(HomePage.this, MainActivity.class);
        HomePage.this.startActivity(myIntent);
    }
});

This code (taken from your HomePage.java opens MainActivity from an intent

Ahmed Aeon Axan
  • 2,139
  • 1
  • 17
  • 30
  • Hi thanks for your help, I have used the code you have provided me with however I am still receiving errors for this line: `android:label="@string/homepage" android:name="MainActivity"` – Nick Mar 11 '13 at 14:59
  • If you could post the exact errors in your question that would be great – Ahmed Aeon Axan Mar 11 '13 at 15:01
  • Yes. you should remove that extra android:name=".MainActivity" from there. It was a mistake in the code. see my revised code. – Ahmed Aeon Axan Mar 11 '13 at 15:08
  • Having entered your altered code `android:label="@string/homepage"` now has an error: No resource found that matches the given name (at 'label' with value '@string/homepage'). – Nick Mar 11 '13 at 15:10
  • make sure you have a string called homepage defined in your strings.xml file – Ahmed Aeon Axan Mar 11 '13 at 15:13
0

Change your manifest file like this

  <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myphoneapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.myphoneapp.MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".HomePage"

    </activity>
</application>

user1835052
  • 455
  • 1
  • 5
  • 11
0

in the manifest file your Homepage activity has to be your main activity so your manifest should look like this:

 <activity
        android:name="com.myphoneapp.MainActivity"
        android:label="@string/homepage" android name="MainActivity"
        android:parentActivityName="com.example.myphoneapp.MainActivity" >

        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myphoneapp.MainActivity" />
    </activity>
<activity
        android:name="com.myphoneapp.HomePage"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Adil
  • 69
  • 1
  • 6