MainActivity.java
package edu.byuh.cis.cs203.photobrowser;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private int currentImageIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout.LayoutParams nice = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1);
final String[] roomNames = {
"The Bank",
"The Gold Room",
"The Green Room",
"The Machine Room",
"The Milk Room",
"The Office",
"The Pink Room",
"The Warm Room",
"The White Room"
};
final int[] imageIDs = {
R.drawable.bank,
R.drawable.gold_room,
R.drawable.green_room,
R.drawable.machine_room,
R.drawable.milk_room,
R.drawable.office,
R.drawable.pink_room,
R.drawable.warm_room,
R.drawable.white_room
};
currentImageIndex = 0;
TextView tv = new TextView(this);
tv.setText(roomNames[currentImageIndex]);
tv.setTextSize(50);
tv.setGravity(Gravity.CENTER);
tv.setLayoutParams(nice);
ImageView iv = new ImageView(this);
iv.setImageResource(imageIDs[currentImageIndex]);
iv.setLayoutParams(nice);
Button left = new Button(this);
left.setText("<");
left.setLayoutParams(nice);
left.setOnClickListener(v -> {
currentImageIndex = currentImageIndex - 1;
if (currentImageIndex < 0) {
currentImageIndex = imageIDs.length-1;
}
iv.setImageResource(imageIDs[currentImageIndex]);
tv.setText(roomNames[currentImageIndex]);
});
Button right = new Button(this);
right.setText(">");
right.setLayoutParams(nice);
right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentImageIndex = (currentImageIndex+1) % imageIDs.length;
iv.setImageResource(imageIDs[currentImageIndex]);
tv.setText(roomNames[currentImageIndex]);
}
});
LinearLayout lnl = new LinearLayout(this);
lnl.setOrientation(LinearLayout.VERTICAL);
LinearLayout buttons = new LinearLayout(this);
buttons.setOrientation(LinearLayout.HORIZONTAL);
lnl.addView(tv);
lnl.addView(iv);
buttons.addView(left);
buttons.addView(right);
lnl.addView(buttons);
setContentView(lnl);
}
}