French Teacher App
Output:
Resource Link:
https://drive.google.com/drive/folders/1vp4TVMTACXblVQM-HGPc1UrqWZoAoRV6?usp=drive_link
Designing the Layout
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="French Teacher App"
android:textColor="@color/white"
android:textSize="32sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.629"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.311" />
<Button
android:id="@+id/blackBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="67dp"
android:text="Black"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/greenBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Green"
android:textColor="#3ED92C"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/blackBtn" />
<Button
android:id="@+id/purpleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:text="Purple"
android:textColor="#9A038D"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/blackBtn" />
<Button
android:id="@+id/redBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="152dp"
android:text="Red"
android:textColor="#B80000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/blackBtn" />
<Button
android:id="@+id/yellowBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="216dp"
android:text="Yellow"
android:textColor="#F4C11D"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/blackBtn" />
Java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button blackBtn, yellowBtn, redBtn, purpleBtn, greenBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
blackBtn = findViewById(R.id.blackBtn);
redBtn = findViewById(R.id.redBtn);
yellowBtn = findViewById(R.id.yellowBtn);
purpleBtn = findViewById(R.id.purpleBtn);
greenBtn = findViewById(R.id.greenBtn);
redBtn.setOnClickListener(this);
blackBtn.setOnClickListener(this);
yellowBtn.setOnClickListener(this);
purpleBtn.setOnClickListener(this);
greenBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// Find the button by ID and play the correct sound
int clickedBtnId = v.getId();
if (clickedBtnId == R.id.redBtn){
PlaySounds(R.raw.red);
} else if (clickedBtnId == R.id.blackBtn) {
PlaySounds(R.raw.black);
} else if (clickedBtnId == R.id.greenBtn) {
PlaySounds(R.raw.green);
}else if (clickedBtnId == R.id.purpleBtn){
PlaySounds(R.raw.purple);
}else if (clickedBtnId == R.id.yellowBtn){
PlaySounds(R.raw.yellow);
}
}
public void PlaySounds(int id){
MediaPlayer mediaPlayer = MediaPlayer.create(
this,
id
);
mediaPlayer.start();
}
}
Comments
Post a Comment