Skip to content

Instantly share code, notes, and snippets.

@Jabriko
Created May 8, 2018 01:58
Show Gist options
  • Save Jabriko/bdb1eb60178c91396eb6f7993a370cec to your computer and use it in GitHub Desktop.
Save Jabriko/bdb1eb60178c91396eb6f7993a370cec to your computer and use it in GitHub Desktop.
package id.indrasudirman.tablayoutwithfab;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public TabLayout tabLayout;
public ViewPager viewPager;
final String answerQuestion1 = "ngr";
final String answerQuestion2 = "ivfvgrq";
final String answerQuestion3 = "yvirq";
int[] colorIntArray = {R.color.red,R.color.yellow,R.color.green};
int[] iconIntArray = {R.drawable.ic_menu_forward, R.drawable.ic_check, R.drawable.ic_tab_contacts};
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.action_bar);
setSupportActionBar(toolbar);
tabLayout = findViewById(R.id.tabs);
viewPager = findViewById(R.id.viewpager);
setupViewPager(viewPager);
final FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = tabLayout.getSelectedTabPosition();
switch (position) {
case 0:
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
break;
case 1:
checkAnswer(view);
break;
case 2:
Snackbar.make(view, "Berta, Susukan, Banjarnegara", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
break;
}
}
});
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (viewPager.getCurrentItem() == 0) {
viewPager.setCurrentItem(-1, false);
return true;
}
else if (viewPager.getCurrentItem() == 1) {
viewPager.setCurrentItem(1, false);
return true;
}
else if (viewPager.getCurrentItem() == 2) {
viewPager.setCurrentItem(2, false);
return true;
}
return true;
}
});
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
animateFab(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
tabLayout.setupWithViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(),"HAL 1");
adapter.addFrag(new TwoFragment(), "HAL 2");
adapter.addFrag(new ThreeFragment(), "HAL 3");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
protected void animateFab(final int position) {
final FloatingActionButton fab = findViewById(R.id.fab);
fab.clearAnimation();
// Scale down animation
ScaleAnimation shrink = new ScaleAnimation(1f, 0.1f, 1f, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
shrink.setDuration(100); // animation duration in milliseconds
shrink.setInterpolator(new AccelerateInterpolator());
shrink.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// Change FAB color and icon
fab.setBackgroundTintList(ContextCompat.getColorStateList(getApplicationContext(), colorIntArray[position]));
fab.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), iconIntArray[position]));
// Rotate Animation
Animation rotate = new RotateAnimation(60.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(150);
rotate.setInterpolator(new DecelerateInterpolator());
// Scale up animation
ScaleAnimation expand = new ScaleAnimation(0.1f, 1f, 0.1f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
expand.setDuration(150); // animation duration in milliseconds
expand.setInterpolator(new DecelerateInterpolator());
// Add both animations to animation state
AnimationSet s = new AnimationSet(false); //false means don't share interpolators
s.addAnimation(rotate);
s.addAnimation(expand);
fab.startAnimation(s);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
fab.startAnimation(shrink);
}
private void checkAnswer(View view) {
ArrayList<String> incorrectAnswerList = new ArrayList<String>();
int numberOfQuestionCorrect = 0;
if (checkQuestion1()) {
numberOfQuestionCorrect++;
} else {
incorrectAnswerList.add("Soal No 1");
}
if (checkQuestion2()) {
numberOfQuestionCorrect++;
} else {
incorrectAnswerList.add("Soal No 2");
}
if (checkQuestion3()) {
numberOfQuestionCorrect++;
} else {
incorrectAnswerList.add("Soal No 3");
}
StringBuilder sb = new StringBuilder();
for (String s : incorrectAnswerList)
{
sb.append(s);
sb.append("\n");
}
if (numberOfQuestionCorrect == 3) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setTitle("Selamat!")
.setMessage("Anda berhasil, nilai Anda : " + numberOfQuestionCorrect + "/3\nIni Sempurna. Anda dapat melanjutkan ke pelajaran berikutnya.")
.setCancelable(false)
.setPositiveButton("Simple Past Tense 2",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setTitle("Gagal!")
.setMessage("Anda gagal, Nilai Anda adalah : " + numberOfQuestionCorrect + "/3\nAnda belum dapat melanjutkan pelajaran berikutnya.\n\n" + "Perbaiki jawaban Anda : \n\n" + sb.toString())
.setCancelable(false)
.setPositiveButton("Mulai test lagi",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
viewPager.setCurrentItem(viewPager.getCurrentItem());
}
})
.setNegativeButton("Keluar aplikasi",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
moveTaskToBack(true);
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
private boolean checkQuestion1() {
EditText editTextQuestion1 = findViewById(R.id.answer_simple_past_test1);
//Convert to plaintext
Rot13 mRot13 = new Rot13();
mRot13.methodRot13(answerQuestion1);
String rot13 = mRot13.methodRot13(answerQuestion1);
//Check the answer after converted to plaintext
return editTextQuestion1.getText().toString().equalsIgnoreCase(rot13);
}
private boolean checkQuestion2() {
EditText editTextQuestion2 = findViewById(R.id.answer_simple_past_test2);
//Convert to plaintext
Rot13 mRot13 = new Rot13();
mRot13.methodRot13(answerQuestion2);
String rot13 = mRot13.methodRot13(answerQuestion2);
//Check the answer after converted to plaintext
return editTextQuestion2.getText().toString().equalsIgnoreCase(rot13);
}
private boolean checkQuestion3() {
EditText editTextQuestion3 = findViewById(R.id.answer_simple_past_test3);
//Convert to plaintext
Rot13 mRot13 = new Rot13();
mRot13.methodRot13(answerQuestion3);
String rot13 = mRot13.methodRot13(answerQuestion3);
//Check the answer after converted to plaintext
return editTextQuestion3.getText().toString().equalsIgnoreCase(rot13);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment