package
com.gtappdevelopers.kotlingfgproject;
import
android.graphics.Typeface;
import
android.os.Build;
import
android.os.Bundle;
import
android.view.Gravity;
import
android.view.View;
import
android.view.ViewGroup;
import
android.view.animation.Animation;
import
android.view.animation.AnimationUtils;
import
android.widget.Button;
import
android.widget.ImageSwitcher;
import
android.widget.ImageView;
import
android.widget.LinearLayout;
import
android.widget.TextView;
import
android.widget.ViewSwitcher;
import
androidx.annotation.RequiresApi;
import
androidx.appcompat.app.AppCompatActivity;
public
class
MainActivity
extends
AppCompatActivity {
private
LinearLayout containerLL;
int
images[] = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4};
int
index =
0
;
@RequiresApi
(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
containerLL = findViewById(R.id.idLLContainer);
LinearLayout.LayoutParams txtLayoutParam =
new
LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
txtLayoutParam.gravity = Gravity.CENTER;
TextView headingTV =
new
TextView(
this
);
headingTV.setText(
"Dynamic Image Switcher in Android"
);
headingTV.setTextSize(20f);
headingTV.setTextColor(getResources().getColor(R.color.black));
headingTV.setTypeface(Typeface.DEFAULT_BOLD);
headingTV.setPadding(
20
,
20
,
20
,
20
);
headingTV.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
headingTV.setLayoutParams(txtLayoutParam);
ImageSwitcher imageSwitcher =
new
ImageSwitcher(
this
);
imageSwitcher.setFactory(
new
ViewSwitcher.ViewFactory() {
@Override
public
View makeView() {
ImageView imageView =
new
ImageView(MainActivity.
this
);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(
20
,
20
,
20
,
20
);
return
imageView;
}
});
imageSwitcher.setImageResource(images[index]);
Animation imageIn = AnimationUtils.loadAnimation(
this
, android.R.anim.slide_in_left
);
imageSwitcher.setInAnimation(imageIn);
Animation imageOut = AnimationUtils.loadAnimation(
this
, android.R.anim.slide_out_right
);
imageSwitcher.setOutAnimation(imageOut);
LinearLayout.LayoutParams buttonParam =
new
LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
buttonParam.setMargins(
10
,
10
,
10
,
10
)
Button prevBtn =
new
Button(
this
);
prevBtn.setText(
"Previous"
);
prevBtn.setAllCaps(
false
);
prevBtn.setLayoutParams(buttonParam);
prevBtn.setTextColor(getResources().getColor(R.color.white));
prevBtn.setBackgroundColor(getResources().getColor(R.color.purple_200));
prevBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
if
(index -
1
>=
0
) {
index = index -
1
;
}
else
{
index =
3
;
}
imageSwitcher.setImageResource(images[index]);
}
});
Button nextBtn =
new
Button(
this
);
nextBtn.setText(
"Next"
);
nextBtn.setAllCaps(
false
);
nextBtn.setLayoutParams(buttonParam);
nextBtn.setTextColor(getResources().getColor(R.color.white));
nextBtn.setBackgroundColor(getResources().getColor(R.color.purple_200));
nextBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
if
(index +
1
< images.length) {
index = index +
1
;
}
else
{
index =
0
;
}
imageSwitcher.setImageResource(images[index]);
}
});
containerLL.addView(headingTV);
containerLL.addView(imageSwitcher);
containerLL.addView(prevBtn);
containerLL.addView(nextBtn);
}
}