-
-
Save udacityandroid/6c8a127ff0a3c29721af to your computer and use it in GitHub Desktop.
int weekday = 5; | |
int weekend = 9; | |
int optimalHours = 7 * 8; | |
int actualHours = weekday; | |
actualHours = actualHours + weekend * 2; | |
int solution = optimalHours - actualHours; | |
display(solution); |
Done!
package com.example.android.practiceset2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// PASTE CODE YOU WANT TO TEST HERE
int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;
int actualHours = weekday;
actualHours = actualHours + weekend * 2;
int solution = optimalHours - actualHours;
display(solution);
}
/**
* Display methods that allow the text to appear on the screen. Don't worry if you don't know
* how these work yet. We'll be covering them in lesson 3.
*/
public void display(String text) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(text);
}
public void display(int text) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(text + "");
}
public void display1(String text) {
display(text);
}
public void display2(String text) {
TextView t = (TextView) findViewById(R.id.display_text_view_2);
t.setText(text);
}
public void display3(String text) {
TextView t = (TextView) findViewById(R.id.display_text_view_3);
t.setText(text);
}
}
i wrote them manually and lol i didnt place a dot between t and setText now that i rewrote that part is all good t.setText
(i need to be more carefull with the punctuations and parantheses) works perfect :)
Getting the following code when I try to copy and paste the MainActivity.java code
error: package android.support.v7.app does not exist
package com.example.android.practiceset2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// PASTE CODE YOU WANT TO TEST HERE
int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;
int actualHours = weekday;
actualHours = actualHours + weekend * 2;
int solution = optimalHours - actualHours;
display(solution);
}
/**
* Display methods that allow the text to appear on the screen. Don't worry if you don't know
* how these work yet. We'll be covering them in lesson 3.
*/
public void display(String text) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(text);
}
public void display(int text) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(text + "");
}
public void display1(String text) {
display(text);
}
public void display2(String text) {
TextView t = (TextView) findViewById(R.id.display_text_view_2);
t.setText(text);
}
public void display3(String text) {
TextView t = (TextView) findViewById(R.id.display_text_view_3);
t.setText(text);
}
}
Getting the following code when I try to copy and paste the MainActivity.java code
error: package android.support.v7.app does not exist
Hi @al130m
You have the same errors like everybody else from the moment when you create a new project and copy paste everything :)
Delete this import line from your code:
import android.support.v7.app.AppCompatActivity;
Then Select "AppCombatActivity" from your MainActivity class:
public class MainActivity extends AppCompatActivity
Press ALT + ENTER. -. new small window shows up ->. select "import class".
That's it.
Basically, you will change the import code to the recent package.
package com.example.android.practiceset2;
import android.os.Bundle;
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);
// PASTE CODE YOU WANT TO TEST HERE
int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;
int actualHours = weekday;
actualHours = actualHours + weekend * 2;
int solution = optimalHours - actualHours;
display(solution);
}
/**
* Display methods that allow the text to appear on the screen. Don't worry if you don't know
* how these work yet. We'll be covering them in lesson 3.
*/
public void display(String text) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(text);
}
public void display(int text) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(text + "");
}
}
this works for me
package com.example.android.practiceset2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
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);
int weekday = 5;
int weekend = 9;
int opimalHours = 7 * 8;
int actualHours = (weekday * 5) + (weekend * 2);
int solution = opimalHours - actualHours;
display(solution);
}
public void display(int n) {
TextView textView = (TextView) findViewById(R.id.display_text_view);
textView.setText(Integer.toString(n));
}
}
and in ActivityMain XML
<TextView
android:id="@+id/display_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="45sp" />
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;
int actualHours = weekday * 5 + weekend * 2;
int solution = optimalHours - actualHours;
display(solution);
}
public void display(int i) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText("" + i);
}
@yyssimon
I think you have a display method in the MainActivity onCreate{}.
Try putting it outside {} of onCreate.
Example:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// PASTE CODE YOU WANT TO TEST HERE
int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;
int actualHours = weekday;
actualHours = actualHours + weekend * 2;
int solution = optimalHours - actualHours;
display(solution);
}
public void display(int i) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText("" + i);
}
In case its 2021 with updated android studio.. and the old code has some buggs..
try this one it worked for me.
package com.example.android.practiceset2;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
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);
int weekday = 5;
int weekend = 9;
int optimalHours = 7 * 8;
int actualHours = weekday;
actualHours = actualHours + weekend * 2;
int solution = optimalHours - actualHours;
display(solution);
}
/**
* Display methods that allow the text to appear on the screen. Don't worry if you don't know
* how these work yet. We'll be covering them in lesson 3.
*/
public void display (int i) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(""+i);
}
}
start learning again