Last active
August 29, 2015 14:08
-
-
Save caioketo/1b8dd1ba903f8b457c91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.caioketo.owrpg; | |
/** | |
* Created by Caio on 31/10/2014. | |
*/ | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v7.widget.Toolbar; | |
public abstract class BaseActivity extends ActionBarActivity { | |
private Toolbar toolbar; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(getLayoutResource()); | |
toolbar = (Toolbar) findViewById(R.id.toolbar); | |
if (toolbar != null) { | |
setSupportActionBar(toolbar); | |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
} | |
} | |
protected abstract int getLayoutResource(); | |
protected void setActionBarIcon(int iconRes) { | |
toolbar.setNavigationIcon(iconRes); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.caioketo.owrpg; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.ImageView; | |
import com.cocosw.bottomsheet.BottomSheet; | |
import java.util.Random; | |
/** | |
* Created by Caio on 31/10/2014. | |
*/ | |
public class DiceActivity extends BaseActivity { | |
private String strResult = ""; | |
private int intResult = 0; | |
private final int[] MAX_DICES = {4, 6, 8, 10, 12, 20}; | |
private final int[] DICES_ICONS = {R.drawable.d4icon, R.drawable.d6icon, R.drawable.d8icon, R.drawable.d10icon, | |
R.drawable.d12icon, R.drawable.d20icon}; | |
private final String[] DICES_NAMES = {"d4", "d6", "d8", "d10", "d12", "d20"}; | |
private int diceIndex = 0; | |
private Random random = new Random(); | |
private ImageView imgDice; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
imgDice = (ImageView)findViewById(R.id.imgDice); | |
findViewById(R.id.done).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
throwDice(); | |
} | |
}); | |
} | |
@Override | |
protected int getLayoutResource() { | |
return R.layout.activity_dice; | |
} | |
public void throwDice() { | |
intResult = random.nextInt(MAX_DICES[diceIndex]) + 1; | |
strResult = "You rolled " + Integer.toString(intResult) + " from a " + DICES_NAMES[diceIndex]; | |
done(); | |
} | |
public void done() { | |
Intent intent = new Intent(); | |
intent.putExtra("STR_RESULT", strResult); | |
intent.putExtra("STR_RESULT", intResult); | |
setResult(RESULT_OK, intent); | |
finish(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
new BottomSheet.Builder(this).sheet(R.menu.dices).listener(new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
switch (which) { | |
case R.id.d4: | |
diceIndex = 0; | |
break; | |
case R.id.d6: | |
diceIndex = 1; | |
break; | |
case R.id.d8: | |
diceIndex = 2; | |
break; | |
case R.id.d10: | |
diceIndex = 3; | |
break; | |
case R.id.d12: | |
diceIndex = 4; | |
break; | |
case R.id.d20: | |
diceIndex = 5; | |
break; | |
} | |
imgDice.setImageResource(DICES_ICONS[diceIndex]); | |
} | |
}).grid().show(); | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.caioketo.owrpg; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Toast; | |
/** | |
* Created by Caio on 31/10/2014. | |
*/ | |
public class HomeActivity extends BaseActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
final Context ctx = this; | |
findViewById(R.id.btn_dice).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Intent i = new Intent(ctx, DiceActivity.class); | |
startActivityForResult(i, 0); | |
} | |
}); | |
} | |
@Override | |
protected int getLayoutResource() { | |
return R.layout.home_activity; | |
} | |
@Override | |
public void onActivityResult(int requestCode, int responseCode, | |
Intent intent) { | |
if (requestCode == 0) { | |
if (responseCode == RESULT_OK) { | |
String msg = intent.getExtras().getString("STR_RESULT", "ERRO"); | |
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment