package
com.gtappdevelopers.kotlingfgproject;
import
android.graphics.Color;
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.LinearLayout;
import
android.widget.TextSwitcher;
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;
String languages[] = {
"Java"
,
"C"
,
"Kotlin"
,
"C++"
};
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 Text 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);
TextSwitcher textSwitcher =
new
TextSwitcher(
this
);
textSwitcher.setFactory(
new
ViewSwitcher.ViewFactory() {
@Override
public
View makeView() {
TextView textView =
new
TextView(MainActivity.
this
);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
textView.setTextSize(32f);
textView.setPadding(
100
,
100
,
100
,
100
);
textView.setTextColor(Color.BLACK);
return
textView;
}
});
textSwitcher.setText(languages[index]);
Animation textIn = AnimationUtils.loadAnimation(
this
, android.R.anim.slide_in_left
);
textSwitcher.setInAnimation(textIn);
Animation textOut = AnimationUtils.loadAnimation(
this
, android.R.anim.slide_out_right
);
textSwitcher.setOutAnimation(textOut);
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
;
}
textSwitcher.setText(languages[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
< languages.length) {
index = index +
1
;
}
else
{
index =
0
;
}
textSwitcher.setText(languages[index]);
}
});
containerLL.addView(headingTV);
containerLL.addView(textSwitcher);
containerLL.addView(prevBtn);
containerLL.addView(nextBtn);
}
}