-
-
Save udacityandroid/0d6a67d340cdf28bbf6e to your computer and use it in GitHub Desktop.
<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); | |
} | |
} |
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.
@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.
good excercice
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?
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" />
Done! Nice! Thank you Udacity and GitHub!
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.
my code run sucessfully but when i clicked on the button then nothing happened... No log showed..
Doooone. :D
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.
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);
}
}
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
i was here
guys it doesn not poo up anything
Android studio cannot resolve symbol "@dimen/..........."

Some said I need to a have dimen.xml file.
please where is it?