I'm new in Android programming. I'm trying to create a login form app with options menu.
When the app starts it shows only welcome.xml. But I also want the menu on the top.
How and when I should call onCreateOptionsMenu()?
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.fruci.davide.firstapplication">
<application android:allowBackup="true" android:label="@string/app_name"
android:icon="@mipmap/ic_launcher" android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainClass"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainClass.java
public class MainClass extends Activity {
@Override
public void onCreate(Bundle saveOnInstanceState){
super.onCreate(saveOnInstanceState);
setContentView(R.layout.welcome);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
welcome.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:id="@+id/username" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="@+id/password" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:onClick="login"
android:text="Login"/>
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:onClick="cancel"
android:text="Cancella"/>
</TableRow>
</TableLayout>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/MENU_1"
android:title="Nuova nota"/>
<item
android:id="@+id/MENU_2"
android:title="Elenco note"/>
</menu>
styles.xml (@style/AppTheme)
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
This is what is shown when the application starts: