Meng-Jiun Chiou
Meng-Jiun Chiou

Reputation: 13

Creating new blank activity using Android Studio

I'm a new hand learning Android using Android Studio. While I follow the official tutorial Getting Started provided by Google, I encountered a problem: When I trying to create a new blank activity, the default code in DisplayMessageActivity() is different from the sample. To be specific, there's just function onCreate(), but no OptionsItemSelected() and class PlaceholderFragment there.

I've tried to copy the code list below from the Getting Started,

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent

Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.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);
}

however I still can't get the same activity (layout) of second activity: DisplayMessageActivity. There's just text but no top bar (consist of App's name, return button, etc.).

M.-J. Chiou

Upvotes: 1

Views: 2036

Answers (1)

Surro
Surro

Reputation: 308

You have to create a menu_display_activity.xml into the folder /res/menu like this one:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".DisplayMessageActivity">
</menu>

And add following to your DisplayMessageActivity.java :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_display_activity, menu);
    return super.onCreateOptionsMenu(menu);
}

EDIT:

And don´t forget to add following code in your AndroidManifest.xml in your "application" - Tag:

    <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity"/>

Upvotes: 3

Related Questions