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);
}
}
@Syiekei
Copy link

Syiekei commented Jun 18, 2018

Awesome coding

@Odikanwa
Copy link

wonderful... It took me forever to get this. not a bad exercise though

@AtereVictor
Copy link

It was quite helpful

@Michaeltochukwu
Copy link

This is great! I will surely go back to this course and study again, until I internalize all the teachings, so lovely

@jerrychong25
Copy link

Completed!

@Manhans
Copy link

Manhans commented Aug 23, 2018

I don't understanding...

@medax07
Copy link

medax07 commented Aug 26, 2018

done

@shuvamlal
Copy link

got stuck with @Dimen

@treedbox
Copy link

My code:
activity_main.xml ________________

<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="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>

MainActivity.java ________________

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 setLog(String className, String message) {
        Log.v(className, message);
    }

    public void getMenuMessage(String className, int id) {
        TextView menuItem = findViewById(id);
        String menuItemText = menuItem.getText().toString();
        setLog(className, menuItemText);
    }

    public void printToLogs(View view) {
        String className = "MainActivity";

        // Find first menu item TextView and print the text to the logs
        getMenuMessage(className, R.id.menu_item_1);

        // Find second menu item TextView and print the text to the logs
        getMenuMessage(className, R.id.menu_item_2);

        // Find third menu item TextView and print the text to the logs
        getMenuMessage(className, R.id.menu_item_3);
    }
}

@emanhamad
Copy link

all done

@bapiego
Copy link

bapiego commented Oct 23, 2018

Android studio cannot resolve symbol "@dimen/..........."
printing to log error
Some said I need to a have dimen.xml file.
please where is it?

@medju06
Copy link

medju06 commented Dec 3, 2018

Android studio cannot resolve symbol "@dimen/..........."
printing to log error
Some said I need to a have dimen.xml file.
please where is it?

rebuild project

@driss65
Copy link

driss65 commented Jan 1, 2019

Hello,
Whene i click on the buttom, the log is good in android studio, but the messages doesnt appear on the phone.
Any idea?
Thank you.

@play14
Copy link

play14 commented Jan 22, 2019

@correndonomundo Are you testing it on a Huawei device? If yes, try this.
For my Huawei device the following helped me to solve the same problem:

Dial:

##2846579##
and a hidden menu is shown. Go to "Background Setting" -> "Log setting" and enable the log levels.

Thanks for the post. This save me some time.

@seltamouni
Copy link

good excercice

@hammambys
Copy link

Android studio cannot resolve symbol "@dimen/..........."
printing to log error
Some said I need to a have dimen.xml file.
please where is it?

Just change it to "16dp" and move on it's not a big deal

@developersamuelakram
Copy link

I see no point of doing this exercise. And I got pissed when I was just reading it and I could not understand the reason of doing this exercise If this was so important she should have made a video about it because this is something new

String menuItem1 = textViewItem1.getText().toString();

what is going on above and also

Log.v("MainActivity", menuItem3);

concatenation is taking place? or what . can someone please summarize this whole exercise to me?

@sanketgitmod
Copy link

image

@mouda123
Copy link

help me plz
zzzzz

@zakaria3698
Copy link

hiy man take this

MainActivity.
package com.example.android.menu;
import android.nfc.Tag;
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 setLog(String className, String message) {
    Log.v(className,message) ;

}

public void getMenuMessage(String className, int id) {
    TextView menuItem = findViewById(id);
    String menuItemText = menuItem.getText().toString();
    setLog(className, menuItemText);
}

public void printToLogs(View view) {
    String className = "MainActivity";

    // Find first menu item TextView and print the text to the logs
    getMenuMessage(className, R.id.menu_item_1);

    // Find second menu item TextView and print the text to the logs
    getMenuMessage(className, R.id.menu_item_2);

    // Find third menu item TextView and print the text to the logs
    getMenuMessage(className, R.id.menu_item_3);
}

}

activity_main.xml

<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" />

@zakaria3698
Copy link

@NaelAlShaban
Copy link

Done! Nice! Thank you Udacity and GitHub!

@Islam3li
Copy link

delete the error in red in XML file :

 `android:paddingBottom="@dimen/activity_vertical_margin"`
 `android:paddingLeft="@dimen/activity_horizontal_margin"`
 `android:paddingRight="@dimen/activity_horizontal_margin"`
 `android:paddingTop="@dimen/activity_vertical_margin"`

delete that line in Java file :

import android.support.v7.app.AppCompatActivity;

then final code will be :

public void printToLogs(View view) {

    // Find first menu item TextView and print the text to the logs
    TextView textView1 = findViewById(R.id.menu_item_1);
    String menuItem1 = textView1.getText().toString();
    Log.v("MainActivity",menuItem1);

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

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

and that is done thanks for helpers and try to figrue it out by ur self in the first.

@Abhilashtyagi007
Copy link

my code run sucessfully but when i clicked on the button then nothing happened... No log showed..

@yaz71
Copy link

yaz71 commented Sep 20, 2020

Doooone. :D

@MedoRedo
Copy link

I think it is also possible to cast the textViewItem#.getText() from CharSequence to String. Am I wrong? My code also works like this: String menuItem3 = (String) textViewItem3.getText();

Nevertheless, a CharSequence is not a superClass of an String so I don't know why it works...

CharSequence is not a class. It is something called an interface. And it is implemented by the String class. You can search for interfaces for more Info.

@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