Writing Android Application Code
Example Prompt:
Example Response:
public class Item {
private String name;
public Item(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter {
private List- itemList;
public ItemAdapter(List- itemList) {
this.itemList = itemList;
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
Item currentItem = itemList.get(position);
holder.textView.setText(currentItem.getName());
}
@Override
public int getItemCount() {
return itemList.size();
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public ItemViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_view);
}
}
}
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ItemAdapter itemAdapter;
private List- itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
itemList = new ArrayList<>();
itemList.add(new Item("Item 1"));
itemList.add(new Item("Item 2"));
itemList.add(new Item("Item 3"));
itemAdapter = new ItemAdapter(itemList);
recyclerView.setAdapter(itemAdapter);
}
}
Related Tasks:
Identifying and Fixing Bugs
Example Prompt:
Example Response:
Related Tasks:
Designing User Interfaces
Example Prompt:
Example Response:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="24sp"
android:layout_marginBottom="24dp" />
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text" />
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_marginTop="16dp" />
Related Tasks:
Integrating Third-Party APIs
Example Prompt:
Example Response:
implementation 'com.google.android.gms:play-services-maps:18.0.2'
package="com.example.myapp">
...>
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE" />
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
private MapView mapView;
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(@NonNull GoogleMap map) {
googleMap = map;
LatLng location = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(location).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(location));
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
Related Tasks:
Writing Unit and Integration Tests
Example Prompt:
Example Response:
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:3.11.2'
testImplementation 'androidx.arch.core:core-testing:2.1.0'
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class UserViewModel extends ViewModel {
private MutableLiveData userName;
public UserViewModel() {
userName = new MutableLiveData<>();
}
public LiveData getUserName() {
return userName;
}
public void setUserName(String name) {
userName.setValue(name);
}
}
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
import androidx.lifecycle.Observer;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
public class UserViewModelTest {
@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();
private UserViewModel userViewModel;
private Observer observer;
@Before
public void setUp() {
userViewModel = new UserViewModel();
observer = Mockito.mock(Observer.class);
userViewModel.getUserName().observeForever(observer);
}
@Test
public void testSetUserName() {
String testName = "John Doe";
userViewModel.setUserName(testName);
// Verify that the observer is notified with the correct value
verify(observer, times(1)).onChanged(testName);
}
@Test
public void testInitialUserName() {
// Verify that the initial value is null
verify(observer, times(1)).onChanged(null);
}
}
Related Tasks: