Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created June 27, 2015 23:17
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save udacityandroid/0d6a67d340cdf28bbf6e to your computer and use it in GitHub Desktop.
Save udacityandroid/0d6a67d340cdf28bbf6e to your computer and use it in GitHub Desktop.
Android for Beginners : Menu Solution Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/menu_item_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mango sorbet"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="@+id/menu_item_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Blueberry pie"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="18sp" />
<TextView
android:id="@+id/menu_item_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Chocolate lava cake"
android:textAppearance="?android:textAppearanceMedium"
android:textSize="18sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="printToLogs"
android:text="Print menu to logs" />
</LinearLayout>
package com.example.android.menu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void printToLogs(View view) {
// Find first menu item TextView and print the text to the logs
TextView textViewItem1 = (TextView) findViewById(R.id.menu_item_1);
String menuItem1 = textViewItem1.getText().toString();
Log.v("MainActivity", menuItem1);
// Find second menu item TextView and print the text to the logs
TextView textViewItem2 = (TextView) findViewById(R.id.menu_item_2);
String menuItem2 = textViewItem2.getText().toString();
Log.v("MainActivity", menuItem2);
// Find third menu item TextView and print the text to the logs
TextView textViewItem3 = (TextView) findViewById(R.id.menu_item_3);
String menuItem3 = textViewItem3.getText().toString();
Log.v("MainActivity", menuItem3);
}
}
@napiorka-design
Copy link

The final answer given at the top didn't work with my Android version.I guess it is because the code comes from 2015?
So I made some minor adjustment and now it works perfectly and it looks like this:

For the XML file it is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

tools:context=".MainActivity">

<TextView
    android:id="@+id/menu_item_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Mango sorbet"
    android:textAppearance="?android:textAppearanceMedium" />

<TextView
    android:id="@+id/menu_item_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Anna Sun"
    android:textAppearance="?android:textAppearanceMedium"
    android:textSize="18sp" />

<TextView
    android:id="@+id/menu_item_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Chocolate lava cake"
    android:textAppearance="?android:textAppearanceMedium"
    android:textSize="18sp" />

For the Java file:
package com.example.android.menu;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void printToLogs(View view) {
    // Find first menu item TextView and print the text to the logs
    TextView textViewItem1 = (TextView) findViewById(R.id.menu_item_1);
    String menuItem1 = textViewItem1.getText().toString();
    Log.v("MainActivity", menuItem1);

    // Find second menu item TextView and print the text to the logs
    TextView textViewItem2 = (TextView) findViewById(R.id.menu_item_2);
    String menuItem2 = textViewItem2.getText().toString();
    Log.v("MainActivity", menuItem2);

    // Find third menu item TextView and print the text to the logs
    TextView textViewItem3 = (TextView) findViewById(R.id.menu_item_3);
    String menuItem3 = textViewItem3.getText().toString();
    Log.v("MainActivity", menuItem3);
}

}

@Kenmasters-jee
Copy link

Kenmasters-jee commented Apr 7, 2021

java code
package com.example.android.newproject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void printToLogs(View view) {
    // Find first menu item TextView and print the text to the logs
    TextView textViewItem1 = (TextView) findViewById(R.id.lee);
    String menuItem1 = textViewItem1.getText().toString();
    Log.v("MainActivity", menuItem1);

    // Find second menu item TextView and print the text to the logs
    TextView textViewItem2 = (TextView) findViewById(R.id.Jax);
    String menuItem2 = textViewItem2.getText().toString();
    Log.v("MainActivity", menuItem2);

    // Find third menu item TextView and print the text to the logs
    TextView textViewItem3 = (TextView) findViewById(R.id.yi);
    String menuItem3 = textViewItem3.getText().toString();
    Log.v("MainActivity", menuItem3);
}

}
xml

<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
tools:context=".MainActivity">

    <TextView
        android:id="@+id/lee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:text="LeeSin"
        android:textColor="@color/black"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.061"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.024" />

    <TextView
        android:id="@+id/Jax"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/cardview_light_background"
        android:text="Jax"
        android:textColor="@color/black"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.048"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.319" />

    <TextView
        android:id="@+id/yi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/cardview_light_background"
        android:text="master yi"
        android:textColor="@color/black"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.077"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.17" />

    <Button
        android:id="@+id/button"
        android:layout_width="136dp"
        android:layout_height="66dp"
        android:text="Print MTL"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.941"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.451" />

</androidx.constraintlayout.widget.ConstraintLayout>
it give nothing

@Sonualam-bot
Copy link

i was here

@Adams1996-hash
Copy link

guys it doesn not poo up anything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment