- What does the Android R class do?
Answer:
It manages the resources in an Android app, for example: ids defined in the
layout file, colors in the
colors.xml file and strings in the strings.xml file. You can
use the id to create a Java object like this:
TextView tv = findViewById(R.id.txt_temp);
- Show how display a string dynamically from the strings.xml resource file.
Answer:
TextView tv = findViewById(R.id.txt_name);
tv.setText(getString(R.string.first_name));
- Show how to change a color dynamically from the
colors.xml resource file. Answer:
LinearLayout layout = findViewById(R.id.main);
layout.setBackgroundColor(getColor(R.color.purple));
// or
layout.setBackgroundColor(0xFFFF00FF);
- How does Method 4 work for attaching an onClick event
listener to a widget?
Answer: at the end of the onCreate method, add this code to attach a lambda function
event listener to a widget, in this case a button:
Button button1 = findViewById(R.id.btn1);
button1.setOnClickListener( view -> {
// body of onClick listener goes here.
});
- In the
developer.android.com/reference website, look up the setText method of the
TextView class, which
is in the package android.widget.
Answer: on this Android API Reference page, to find the setText method, select
Android Platform on the left navigation bar >> In the Package Index, scroll down
to the android.widgit package and select it. >> Scroll
down past the interfaces to the classes; keep scrolling to find the
TextView class and select it. >> Scroll down past the
XML attributes to the Java methods; keep scrolling to find the
setText method with signature (CharSequence).
- What is an interface?
Answer: An interface is a specification of methods that must be added to a class to
make it compatible with other software. As an example, if a class implements the
Comparable interface (in the java.lang package), than an array or arraylist of objects
of that class can be sorted by the Arrays.sort or Collections.sort.
If I is an interface and A is a class, this class can implement I like this:
public class A implements I {
// body of class
}
If f is a method of the interface I, where I is defined like this:
public interface I {
public void f( );
}
then the function I can be defined like this:
@Override
public void f( ) {
System.out.println("In the method f.");
}
- How does is an interface similar to and different from an abstract class?
Similarities: (a) interfaces and abstract classes cannot be used to create objects.
Only classes that implement an interface or extend an abstract class can create
objects, (b) interfaces and abstract classes can contain abstract methods that
must be overridden.
Differences: (a) all of the methods defined by an interface must be overridden,
an abstract class can contain abstract methods, but it can also contain ordinary
methods with bodies that are inherited by derived classes, (b) an interface cannot
contain a constructor, whereas an abstract class can contain a constructor, (c)
an interface cannot contain instance variables, whereas an abstract class can
contain instance variables.