-
-
Save udacityandroid/042a390d4414f32a5558 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.view.View; | |
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 | |
// Find second menu item TextView and print the text to the logs | |
// Find third menu item TextView and print the text to the logs | |
} | |
} |
This was my final code for lesson 23. I decided to modify it so that I had one method calling the final 3 lines 3 times.
Not sure if this is better code but I figured cutting out 6 lines of duplicated code may be tidier ?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) { //Call itemList method for each menu item itemList(findViewById(R.id.menu_item_1)); itemList(findViewById(R.id.menu_item_2)); itemList(findViewById(R.id.menu_item_3)); } public void itemList(View itemID) { // convert View item to TextView>String by passed param and print the text to the logs TextView menuText = (TextView) itemID; String MenuItem = menuText.getText().toString(); Log.i("MainActivity.Java", MenuItem); }
Good try
Mine code for printing text from views over to logcat!
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 menu1 = (TextView) findViewById(R.id.menu_item_1);
Log.i("Menu App Log:",menu1.getText().toString());
// Find second menu item TextView and print the text to the logs
TextView menu2 = (TextView) findViewById(R.id.menu_item_2);
Log.i("Menu App Log:",menu2.getText().toString());
// Find third menu item TextView and print the text to the logs
TextView menu3 = (TextView) findViewById(R.id.menu_item_3);
Log.i("Menu App Log:",menu3.getText().toString());
}
}
im not understanding this section i think the part one is alittle bit easier
is just me or any one here didnt understant this parts
@mohamed-ismail-ops If you use androidX instead of android, you need changeimport android.support.v7.app.AppCompatActivity;
to import androidx.appcompat.app.AppCompatActivity;
Anyone have an issue with this? Works for me...
`
public void printToLogs(View view) {
// Find first menu item TextView and print the text to the logs
TextView menuItemOne = findViewById(R.id.menu_item_1);
Log.i("First Menu Item", (String) menuItemOne.getText());
// Find second menu item TextView and print the text to the logs
TextView menuItemTwo = findViewById(R.id.menu_item_2);
Log.i("Second Menu Item", (String) menuItemTwo.getText());
// Find third menu item TextView and print the text to the logs
TextView menuItemThree = findViewById(R.id.menu_item_3);
Log.i("Third Menu Item", (String) menuItemThree.getText());
}`
public void printToLogs(View view) {
// Find first menu item TextView and print the text to the logs
TextView menu1 = (TextView) findViewById(R.id.menu_item_1);
Log.i("Menu App Log:",menu1.getText().toString());
// Find second menu item TextView and print the text to the logs
TextView menu2 = (TextView) findViewById(R.id.menu_item_2);
Log.i("Menu App Log:",menu2.getText().toString());
// Find third menu item TextView and print the text to the logs
TextView menu3 = (TextView) findViewById(R.id.menu_item_3);
Log.i("Menu App Log:",menu3.getText().toString());
}
thanks githup and udacity
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 {
TextView menu_item_1,menu_item_2,menu_item_3;
String text1,text2, text3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu_item_1 = findViewById(R.id.menu_item_1);
text1 = (String) menu_item_1.getText();
menu_item_2 = findViewById(R.id.menu_item_2);
text2 = (String) menu_item_2.getText();
menu_item_3 = findViewById(R.id.menu_item_3);
text3 = (String) menu_item_3.getText();
}
public void printToLogs(View view) {
Log.v("MainActivity.java", text1);
Log.v("MainActivity.java", text2);
Log.v("MainActivity.java", text3);
}
}
Solution:
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);
}
@shubhams612 ,i m getting the same problem ,have u got it solved ?
I guess dimen.xml file if not there in resource/values/.
You can replace the error text with "16dp" temporarily
or you can create new dimen.xml file in resource/values/
public void printToLogs(View View){
TextView menu1 = (TextView) findViewById(R.id.menu_item_1);
String ahmed = (String) menu1.getText();
Log.i("MainActivity", ahmed);
TextView menu2 = (TextView) findViewById(R.id.menu_item_2);
ahmed = (String) menu2.getText();
Log.i("MainActivity", ahmed);
TextView menu3 = (TextView) findViewById(R.id.menu_item_3);
ahmed = (String) menu3.getText();
Log.i("MainActivity", ahmed);
}
package com.example.menu;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button button;
TextView menu_item_1, menu_item_2, menu_item_3;
@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.menu_Btn);
menu_item_1 = findViewById(R.id.menu_item_1);
button.setOnClickListener(v -> {
printToLogs(menu_item_1);
});
}
public void printToLogs(View view) {
// Find first menu item TextView and print the text to the logs
Log.i("EnterpriseActivity.java", "Captin's Log, Stardate 43125.8." + "Mango sorbet");
// Find second menu item TextView and print the text to the logs
Log.i("EnterpriseActivity.java", "Captin's Log, Stardate 43125.8." + "Blueberry Pie");
// Find third menu item TextView and print the text to the logs
Log.i("EnterpriseActivity.java", "Captin's Log, Stardate 43125.8." + "Choolate lava cack");
}
}
You can modify the text to be printed though. Just like what i did. Sometimes copying and pasting may not suit your current Android Studio Environment. All you need to do is try to understand.
The purpose of this exercise to print the messages to the LogCat window and not to the screen of the emulator.

look at the image below