0
I’m taking an online course and the menu part is not working in my Android Studio. First, I deleted the Contents who came, left only what is Activity in the Layout. About the code: The App starts in a list, I click on the "+" button and go to a form, in the form should appear in the menu the "Done" icon and so back to the list. It may be because I am using Activity and not content that is not showing the menu?
I’ll send you the code Androidmanifest.xml, Formularioactivity.java, Listaalunosactivity.java, activity_formulario.xml, activity_list_students.xml and menu_formulario.xml and can you tell me what I can change? In the emulator, nothing from the menu simply appears.
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>
Formularioactivity.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);
}
}
Listaalunosactivity.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>
lista_alunos_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_formulario.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"/>
It worked, thank you very much.
– Ygor Fraga