package
com.example.gfgapp;
import
android.graphics.Color;
import
android.os.Bundle;
import
android.view.Gravity;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.widget.Button;
import
android.widget.ImageView;
import
android.widget.LinearLayout;
import
android.widget.TextView;
import
android.widget.Toast;
import
androidx.appcompat.app.AppCompatActivity;
public
class
MainActivity
extends
AppCompatActivity {
private
LinearLayout containerLayout;
private
Button previousButton;
private
Button nextButton;
private
LinearLayout stepIndicatorsLayout;
private
int
currentStep =
0
;
private
View[] steps;
private
TextView[] stepIndicators;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
containerLayout = findViewById(R.id.container_layout);
previousButton = findViewById(R.id.previous_button);
nextButton = findViewById(R.id.next_button);
stepIndicatorsLayout = findViewById(R.id.step_indicators_layout);
steps =
new
View[]{
LayoutInflater.from(
this
).inflate(R.layout.layout_general_info, containerLayout,
false
),
LayoutInflater.from(
this
).inflate(R.layout.layout_personal_info, containerLayout,
false
),
LayoutInflater.from(
this
).inflate(R.layout.activity_main3, containerLayout,
false
)
};
initializeStepIndicators();
showCurrentStep();
previousButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
if
(currentStep >
0
) {
currentStep--;
showCurrentStep();
}
}
});
nextButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
if
(currentStep < steps.length -
1
) {
currentStep++;
showCurrentStep();
}
else
{
submitForm();
}
}
});
}
private
void
initializeStepIndicators() {
stepIndicators =
new
TextView[steps.length];
for
(
int
i =
0
; i < steps.length; i++) {
TextView stepIndicator =
new
TextView(
this
);
stepIndicator.setText(String.valueOf(i +
1
));
stepIndicator.setTextColor(Color.WHITE);
stepIndicator.setTextSize(
18
);
stepIndicator.setBackgroundResource(R.drawable.circle_gray);
stepIndicator.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params =
new
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(
10
,
0
,
10
,
0
);
stepIndicator.setLayoutParams(params);
stepIndicatorsLayout.addView(stepIndicator);
stepIndicators[i] = stepIndicator;
if
(i < steps.length -
1
) {
addArrowIndicator(stepIndicatorsLayout);
}
}
}
private
void
addArrowIndicator(LinearLayout stepIndicatorsLayout) {
ImageView arrow =
new
ImageView(
this
);
arrow.setImageResource(R.drawable.ic_arrow);
LinearLayout.LayoutParams params =
new
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.gravity = Gravity.CENTER_VERTICAL;
arrow.setLayoutParams(params);
stepIndicatorsLayout.addView(arrow);
}
private
void
showCurrentStep() {
containerLayout.removeAllViews();
containerLayout.addView(steps[currentStep]);
previousButton.setVisibility(currentStep >
0
? View.VISIBLE : View.INVISIBLE);
nextButton.setText(currentStep < steps.length -
1
?
"Next"
:
"Submit"
);
updateStepIndicators();
}
private
void
updateStepIndicators() {
for
(
int
i =
0
; i < stepIndicators.length; i++) {
if
(i == currentStep) {
stepIndicators[i].setBackgroundResource(R.drawable.circle_green);
}
else
{
stepIndicators[i].setBackgroundResource(R.drawable.circle_gray);
}
}
}
private
void
submitForm() {
Toast.makeText(MainActivity.
this
,
"Hello"
, Toast.LENGTH_SHORT).show();
}
}