-
-
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 | |
} | |
} |
@thapscoding
You can just delete @dimen/activity_...
all of them and use simple values in dp like "10dp", "16dp" or whatever.
OR
go to app-res-values. check if you have a "dimens.xml" file there. if you don't create one. the steps are easy, just name it exactly "dimen", then add this line of code and modify if you want to:
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>
@thapscoding
sorry, I meant "dimen.xml" file
public class CreateEvent extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
}
public void printToLogs(View view){
TextView text1=findViewById(R.id.menu_item_1);
Log.i("Item 1",text1.getText().toString());
text1=findViewById(R.id.menu_item_2);
Log.i("Item 2",text1.getText().toString());
text1=findViewById(R.id.menu_item_3);
Log.i("Item 3",text1.getText().toString());
}
}
my app has loads of errors when it is running through the codes with @Dimen? It shows me four red lines, and the app won't start.
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
Thanks @sooheelee! That solved compilation errors.
09-29 04:27:24.706 2436-2436/com.javatpoint.example.menu I/MainActivity: Mango sorbet
Blueberry pie
Chocolate lava cake
Why my log cat shows like the above. Adding the date, time & package to just the 1st line and not the rest of the menu items?
My code is as follows...
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 text1 = (TextView) findViewById(R.id.menu_item_1);
Log.i("MainActivity", text1.getText().toString());
// Find second menu item TextView and print the text to the logs
TextView text2 = (TextView) findViewById(R.id.menu_item_2);
Log.i("MainActivity", text2.getText().toString());
// Find third menu item TextView and print the text to the logs
TextView text3 = (TextView) findViewById(R.id.menu_item_3);
Log.i("MainActivity", text3.getText().toString());
}
}
My solution
I gave the LinearLayout View an Id(linearLayout), I then looped through all the children in the View and if the child is not a Button View (Button extends TextView) then get the text from the child and print it to the log.
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
for (int i = 0; i < linearLayout.getChildCount(); i++) {
View child = (View) linearLayout.getChildAt(i);
if (!(child instanceof Button)) {
Log.i("Menu Activity", (((TextView) child).getText().toString()));
}
}
Paste this code in printToLogs Method-
TextView textView1 = (TextView) findViewById(R.id.menu_item_1);// Find first menu item TextView and print the text to the logs
Log.v("MainActivity",textView1.getText().toString());
TextView textView2 = (TextView) findViewById(R.id.menu_item_2);// Find second menu item TextView and print the text to the logs
Log.v("MainActivity",textView2.getText().toString());
TextView textView3 = (TextView) findViewById(R.id.menu_item_3);// Find third menu item TextView and print the text to the logs
Log.v("MainActivity",textView3.getText().toString());
@thapscoding
You can just delete@dimen/activity_...
all of them and use simple values in dp like "10dp", "16dp" or whatever.
ORgo to app-res-values. check if you have a "dimens.xml" file there. if you don't create one. the steps are easy, just name it exactly "dimen", then add this line of code and modify if you want to:
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>
Thanks for this! Helped me fix the errors that appeared when following the udacity instuctions!
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 msg1 = (String)textView1.getText();
Log.v(msg1, "Message 1" +msg1);
// Find second menu item TextView and print the text to the logs
TextView textView2 = findViewById(R.id.menu_item_2);
String msg2 = (String)textView1.getText();
Log.v(msg2, "Message 2 " +msg2);
// Find third menu item TextView and print the text to the logs
TextView textView3 = findViewById(R.id.menu_item_3);
String msg3 = (String)textView1.getText();
Log.v(msg3, "Message 3 " +msg3);
}
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
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");
}
}
Im trying to run the app but xml layout padding attribute values gives me an error. Ironicaly this code is on GitHub and was recommended by Udacity. [Solution]Well i can fix it by applying my own padding values but i dont know if i will be penalized for this maybe they know something i just mistakenly missed.Need help anybody.
code snippet as followed:
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"