I want to add a difficulty in my game, for example when I click a category like category "A" it will show to the next layout and the user has to pick a difficulty to play "easy, normal or hard" .
sorry for my bad sentence
here is my categories:
public class CategoryActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
Button A = (Button) findViewById(R.id.btn_a);
Button B = (Button) findViewById(R.id.btn_b);
Button C = (Button) findViewById(R.id.btn_c);
Button D = (Button) findViewById(R.id.btn_d);
Button E = (Button) findViewById(R.id.btn_e);
Button F = (Button) findViewById(R.id.btn_f);
Button G = (Button) findViewById(R.id.btn_g);
Button H = (Button) findViewById(R.id.btn_h);
Button I = (Button) findViewById(R.id.btn_i);
Button J = (Button) findViewById(R.id.btn_j);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
B.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg1) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
C.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg2) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
D.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg3) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
E.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg4) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
F.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg5) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
G.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg6) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
H.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg7) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
I.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg8) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
J.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg9) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
}
You should use radio buttons, so the user can select only one option and not two or more.
This is an example taken from the official android devs website.
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
PS: You're question is not really clear. It makes it difficult to help you. I hope my answer is acceptable. Refer to the manuals and do some tutorials.
Related
I want to create a Bottom Sheet Dialog. It looks like the image below
enter image description here
public class MainActivity extends AppCompatActivity {
//this is Button that open dialog
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.nextBtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
MainActivity.this, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.layout_bottom_sheet, (LinearLayout) findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetView.findViewById(R.id.doNotAgree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.exit(0);
}
});
bottomSheetView.findViewById(R.id.agree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
});
}
});
}
}
I have tried many methods such as SharedPreferences. But it not working
SharedPreferences preference = getSharedPreferences("mPreference", MODE_PRIVATE);
boolean isUserAgreed = preference.getBoolean("isUserAgreed", false);
if (!isUserAgreed){
// show bottomSheetDialog
} else {
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
MainActivity.this, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.layout_bottom_sheet, (LinearLayout) findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetView.findViewById(R.id.doNotAgree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.exit(0);
}
});
bottomSheetView.findViewById(R.id.agree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("mPreference", MODE_PRIVATE).edit();
editor.putBoolean("isUserAgreed", true);
editor.apply();
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
});
}
});
I have implemented two chronometers in a same activity and so far they are working fine. But when I change activities and then come back to them, they are at 00:00. I need them to keep counting "forever" until I hit the stop button. How can I do that?
And is it possible to make that counting appear also in another activity, so that I can follow up the counting in either activity? It has to be the same counting. Just two different places where I can follow the up from.
I tried a solution with onRestoreInstanceState but I am really confused. I am really new to coding and simple things are still frustrating to me. I am getting something wrong at the set.Base().
Chronometer cronometro_primeira_marca, cronometro_segunda_marca;
Button btn_iniciar_cronometro_primeira_marca, btn_parar_cronometro_primeira_marca, btn_resetar_cronometro_primeira_marca;
Button btn_iniciar_cronometro_segunda_marca, btn_parar_cronometro_segunda_marca, btn_resetar_cronometro_segunda_marca;
String timer1, timer2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cronometros);
cronometro_primeira_marca = (Chronometer) findViewById(R.id.cronometro_primeira_marca);
cronometro_segunda_marca = (Chronometer) findViewById(R.id.cronometro_segunda_marca);
btn_iniciar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_iniciar_cronometro_primeira_marca);
btn_parar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_parar_cronometro_primeira_marca);
btn_resetar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_resetar_cronometro_primeira_marca);
btn_iniciar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_iniciar_cronometro_segunda_marca);
btn_parar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_parar_cronometro_segunda_marca);
btn_resetar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_resetar_cronometro_segunda_marca);
btn_iniciar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.setBase(SystemClock.elapsedRealtime());
cronometro_primeira_marca.start();
}
});
btn_iniciar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.setBase(SystemClock.elapsedRealtime());
cronometro_segunda_marca.start();
}
});
btn_parar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.stop();
}
});
btn_parar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.stop();
}
});
btn_resetar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.setBase(SystemClock.elapsedRealtime());
}
});
btn_resetar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.setBase(SystemClock.elapsedRealtime());
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putLong("Restored_time1", SystemClock.elapsedRealtime());
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if((savedInstanceState !=null)&& savedInstanceState.containsKey("Restored_time1")){
SystemClock.elapsedRealtime().**setBase**(savedInstanceState.getLong("Restored_time1"));
}
super.onRestoreInstanceState(savedInstanceState);
}
}
I am making a mobile calculator and I am aware there are many that have been created. I tried using others as an example to build mine but I have been stuck for days. I was able to display numbers on the EditText when user selects random numbers, but I cannot figure how to add, sub, multi, divide, the numbers that were selected by the user. Please show an example of how I can solve it. This is my first mobile application.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView editText = (EditText) findViewById(R.id.edit_text);
final Button Button0 = (Button) findViewById(R.id.Button_0);
final Button Button1 = (Button) findViewById(R.id.Button_1);
Button Button2 = (Button) findViewById(R.id.Button_2);
Button Button3 = (Button) findViewById(R.id.Button_3);
Button Button4 = (Button) findViewById(R.id.Button_4);
Button Button5 = (Button) findViewById(R.id.Button_5);
Button Button6 = (Button) findViewById(R.id.Button_6);
Button Button7 = (Button) findViewById(R.id.Button_7);
Button Button8 = (Button) findViewById(R.id.Button_8);
Button Button9 = (Button) findViewById(R.id.Button_9);
Button clear_Btn = (Button) findViewById(R.id.C_Button);
Button decimal_Btn = (Button) findViewById(R.id.Decimal_Button);
Button equal_Btn = (Button) findViewById(R.id.equal_Button);
final Button add_Btn = (Button) findViewById(R.id.Addition_Button);
/////////////////// NUMBER BUTTONS ///////////////////////
Button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 0);
}
});
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editText.setText(editText.getText().toString() + 1);
}
});
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 2);
}
});
Button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 3);
}
});
Button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 4);
}
});
Button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 5);
}
});
Button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 6);
}
});
Button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 7);
}
});
Button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 8);
}
});
Button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 9);
}
});
///////////////////// Sign Buttons /////////////////////
// Clear Button
clear_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText("");
}
});
// Decimal Button
decimal_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + ".");
}
});
// Equal button
equal_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
// Add button
add_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + "+");
}
});
}
}
A couple ways to achieve this, you just need a way to retrieve the current number in the calculator and perform an operation against the new selected number
So store the main number in its own variable, and then perform the desired operation against a new number, before you set the new edittext value
It says Expression expected but no recommendations as to what symbol is missing. I'm using android studio. I suppose it's a simple fix I'm just not sure what symbol.
Heres my code:
public class CloudActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
},
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
}, // Where the problem is
));
}
#Override
public void onClick(View v) {
}
}
You are closing your anonymous inner classes (new View.OnClickListener() {) wrong. You shouldn't be separating them with a comma but closing them off with a );.
For example,
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
match up your {} and () for all of them. So you will want to do the same thing for button3.setOnClickListener(new View.OnClickListener() { and any others you might have.
You can read more about them in the Java Docs
Try This. You weren't closing your inner classes properly. I imagine you accidentally put commas in place of semi-colons?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
});
}
I'm developing an APP for a schoolwork. When I launch my app, I configured 4 buttons, one of them is a calendar.
What I want is when I click on Calendar, this open the Google calendar; or if I don't have installed it, take you to Google Play.
The other buttons are activities in the same application, but these are solved.
The main activity:
public class MainActivity extends Activity {
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
entrarboton();
}
private void entrarboton() {
ImageButton accionentrar = (ImageButton) findViewById(R.id.imageButton0);
accionentrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Calendari.class));
}
});
ImageButton accionentrar2 = (ImageButton)findViewById(R.id.imageButton3);
accionentrar2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Notes.class));
}
});
ImageButton accionentrar3 = (ImageButton)findViewById(R.id.imageButton2);
accionentrar3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Apunts.class));
}
});
ImageButton accionentrar4 = (ImageButton)findViewById(R.id.imageButton4);
accionentrar4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Altres.class));
}
});
}
}