Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need help fixing my code. the problem is The functionality of the buttons: Count: Adds 1 to the current count Double: Doubles the current

i need help fixing my code. the problem is The functionality of the buttons:
Count: Adds 1 to the current count
Double: Doubles the current count value when pressed
Reset: Sets the count value back to 0
Toast: Displays a toast message of your choice
Toggle: Turns the background image's visibility from VISIBLE to GONE and when pressed again it changes it from GONE to VISIBLE
Additional Requirements
Adding an ImageView that displays behind the count text (This is what the Toggle buttons change the visibility of)
Change the name of the app in the manifest file and make it "Layout App"
At least one onClick method should be attached in the onCreate method (with an anonymous function or lambda expression)
The TOAST and TOGGLE buttons are inside a linearLayout (horizontal) to achieve the side-by-side look (hint: layout_weight is used)
All displayed text should be a String resource rather than a hardcoded String
A screenshot image of the completed app, looking similar to the one above, is to be added to your zipped folder my code is this and i already have the activity_main and strings file just need the java code. buttons: {
private int count =0;
private ImageView backgroundImage;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
TextView countText = findViewById(R.id.countText);
Button btnCount = findViewById(R.id.btnCount);
Button btnDouble = findViewById(R.id.btnDouble);
Button btnReset = findViewById(R.id.btnReset);
Button btnToast = findViewById(R.id.btnToast);
Button btnToggle = findViewById(R.id.btnToggle);
// Set onClick listeners
btnCount.setOnClickListener(v ->{
count++;
countText.setText(getString(R.string.count_default_text, count));
});
btnDouble.setOnClickListener(v ->{
count *=2;
countText.setText(getString(R.string.count_default_text, count));
});
btnReset.setOnClickListener(v ->{
count =0;
countText.setText(getString(R.string.count_default_text, count));
});
btnToast.setOnClickListener(v -> showToast(getString(R.string.toast_message)));
btnToggle.setOnClickListener(v -> toggleVisibility());
}
private void showToast(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
private void toggleVisibility(){
int visibility =(backgroundImage.getVisibility()== View.VISIBLE)? View.GONE : View.VISIBLE;
backgroundImage.setVisibility(visibility);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions