I'm doing an online course and the menu section is not working in my Android Studio. First, I delete the Contents that came, I just left what is Activity in Layout . About the code: The App starts in a list, I click the "+" button and I go to a form, the form should appear in the menu the "Done" icon and thus return to the list. Could it be because I'm using activity and not content that the menu is not appearing?
I'll send the AndroidManifest.xml , ActivityActivity.java , StudentActivity.java , activity_formular.xml > activity_list_exists and menu_formula.xml and can you tell me what I can change? In the emulator simply nothing appears on the menu.
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ygorfraga.agenda">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ListaAlunosActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FormularioActivity"
android:label="@string/title_activity_formulario"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
ActivityActivity.java:
public class FormularioActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_formulario);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_formulario, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_formulario_ok:
Toast.makeText(FormularioActivity.this, "Aluno Salvo!", Toast.LENGTH_SHORT).show();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
StudentsActivity.java:
public class ListaAlunosActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
String[] alunos = {"Daniel", "Ronaldo", "Jefferson", "Felipe"};
ListView listaAlunos = (ListView) findViewById(R.id.lista_alunos);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alunos);
listaAlunos.setAdapter(adapter);
Button novoAluno = (Button) findViewById(R.id.novo_aluno);
novoAluno.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentVaiProFormulario = new Intent(ListaAlunosActivity.this, FormularioActivity.class);
startActivity(intentVaiProFormulario);
}
});
}
}
activity_formulario.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Nome"
android:id="@+id/formulario_nome"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Endereço"
android:id="@+id/formulario_endereco"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Telefone"
android:id="@+id/formulario_telefone"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Site"
android:id="@+id/formulario_site"/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:max="10"
android:numStars="5"
android:id="@+id/formulario_nota"/>
</LinearLayout>
student_list_activity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_lista_alunos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lista_alunos" />
<Button
android:id="@+id/novo_aluno"
android:layout_width="56dp"
android:layout_height="56dp"
android:text="+"
android:textColor="#fff"
android:textSize="40sp"
android:elevation="6dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/fundo"
android:stateListAnimator="@null"/>
menu_formular.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_formulario_ok"
android:title="Ok"
android:icon="@drawable/ic_confirmar"
app:showAsAction="always"/>