I am developing an App using Eclipse. I have a page where it has different check boxes. I want the user if checking lest say options A and B and D then Activity 7 will open and if the user checks options A and C then Activity 5 will open.
Thank you
You can do so like this:
Get the IDs of the checkboxes. Then add an OnClickListener to the checkboxes like so:
OnClickListener checkBoxListener;
checkBoxListener = new OnClickListener()
{
#Override
public void onClick(View arg0) {
if (checkboxA.isChecked() && checkboxC.isChecked())
{
Intent i = new Intent(this,Activity5.class)
startActivity(i);
}
else if (checkboxA.isChecked() && checkboxB.isChecked() && checkboxD.isChecked())
{
Intent i = new Intent(this,Activity7.class)
startActivity(i);
}
}
};
checkboxA.setOnClickListener(checkBoxListener);
checkboxB.setOnClickListener(checkBoxListener);
checkboxC.setOnClickListener(checkBoxListener);
checkboxD.setOnClickListener(checkBoxListener);
Please give this a try.
Related
I am trying to build a flight ticket booking app.
Here I have two options either user selects one-way or round-way(both sides). When a user selects one-way I need to go to the next screen where all one-way flight details will be shown. Other-wise if a user selects round-way then I will show the details of the round trip to the user.
But if I select one-way(Button) and check it's Pressed state it shows False, So I am unable to navigate the user to the next screen and the same flaw is happening for the round-way.
MainActivity
private void searchClickHandeler(){
//Set Onclick Listener
b_search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
*when One way is pressed it is suppose to go to the One way Suggestion Activity*
if (b_oneway.isPressed()){
Toast.makeText(FlightActivity.this,"Oneway Button is pressed",Toast.LENGTH_LONG).show();
Intent oneway = new Intent(FlightActivity.this,FlightSuggestionActivity.class);
startActivity(oneway);
} else if (b_returnway.isPressed()){
Toast.makeText(FlightActivity.this,"RoundTrip Button is pressed",Toast.LENGTH_LONG).show();
Intent returnway = new Intent(FlightActivity.this,FlightRoundTripSuggestionActivity.class);
startActivity(returnway);
}
}
});
}
try this
Intent intent = new Intent(v.getContext(), FlightSuggestionActivity.class);
startActivity(intent);
I'm currently working on a school project in Android Studio and so far I've written a code which generates random equations.
Now I display two equations on the screen and the user then has to decide wether the second equation is bigger or smaller than the first one. If the second is bigger, the user presses the button 'bigger', if the second one is smaller, the user presses the button with 'smaller'.
Now I'm trying to write the code that if the user pressed correct, it generates a new equation, if he was wrong, the process stops.
I was thinking of if statements like so:
final ArrayList<String> arrayListCheck = new ArrayList<String>();
if(doubleAnswer1 > doubleAnswer2){
arrayListCheck.add("smaller");
} else {
arrayListCheck.add("bigger");
}
final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger)){
arrayListCheck.add("bigger");
} else {
arrayListCheck.add("smaller");
}
}
};
buttonBigger.setOnClickListener(listener);
buttonSmaller.setOnClickListener(listener);
In the arraylist arrayListCheck it will store either 'bigger' or 'smaller'. Now I want to check if the elements in the arraylist are both the same (either both 'bigger' or 'smaller'), if so a new equation will be generated. If the elements in the arraylist are different (not both the same), the process will be stopped.
I don't know if that really works, so it would be nice if someone could help me with this.
If there is anything unclear in my question, feel free to ask and I will try to clarify the problem :)
Thank you already in advance for your help!
I wouldn't use an ArrayList for that.
I would do the following:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger) && doubleAnswer1 < doubleAnswer2) {
Log.v("TAG", "you are right");
} else if(v.equals(buttonSmaller) && doubleAnswer1 > doubleAnswer2) {
Log.v("TAG", "you are right");
} else {
Log.v("TAG", "you are wrong");
}
}
};
Arrays are not really neccessary for this kind of simple comparison.
My app supports 3 languages (english, french and arabic). I have already translated all ressources (values string and drawables files) and it work perfect according to the language set in the user device.
The app consists principally of two activities : mainActivity and Game Activity. mainActivity contain a button (Play) which leads to the GameActivity according to the device language. For example if language is english it will launch Game Activity, if f language is french it will launch GameFr Activity, and if language is arabic it will launch GameAr Activity.
Play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (Locale.getDefault().getLanguage().equals("ar")){
Intent intgame=new Intent(MainActivity.this,GameAr.class);
startActivity(intgame);
}
else {
if (Locale.getDefault().getLanguage().equals("fr")){
Intent intgame=new Intent(MainActivity.this,GameFr.class);
startActivity(intgame);
}
else {
Intent intgame=new Intent(MainActivity.this,Game.class);
startActivity(intgame);
}
}
}
});
However, I would like to add 3 ImageView (flags) in the MainActivity through which users can change the language of the application, for this I added the following:
en = (ImageView) findViewById(R.id.en);
fr = (ImageView) findViewById(R.id.fr);
ar = (ImageView) findViewById(R.id.ar);
en.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLocale("en");
Intent uo = new Intent(MainActivity.this,Game.class);
startActivity(uo);
}
});
fr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLocale("fr");
Intent uo = new Intent(MainActivity.this,GameFr.class);
startActivity(uo);
}
});
ar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLocale("ar");
Intent uo = new Intent(MainActivity.this,GameAr.class);
startActivity(uo);
}
});
Nevertheless, when a user with a device which the language is set to english click the French flag, it will get successfully the french activity. However, if he comes back to the previous page and click on the button (Play), the page displayed is the one corresponding to the activity in English but with the resources (string values and Drawables) French.
This is because the following function:
if (Locale.getDefault().getLanguage().equals("ar"))
always test the language of the device, not the language that the user chose in the app.
is there a function that can give me the language chosen by the SetLocale function? or I should use a variable transfer between activities?
how can I fix this, do you have better suggestions?
use this
Locale.setDefault("Your Locale");
Hi there I am new to Android Programming
I am trying to create an application in which, the user clicks button on the first page
the text color in the buttons change color and the change is reflected in another activity page.
To do this I have
1) one fragment class(BookLockerFragment) which reference to an xml file containing the buttons
2) The parent activity file (TabActivity.java)
3) The activity file to reflect the change ( complainResponse.java)
Here is the code:
LodgeComplaintFragment.java
ArrayList<String>userSelectedOptions = new ArrayList<String>();
if(btnSis.getCurrentTextColor()==Color.BLUE){
userSelectedOptions.add("SIS");
}
Button but = (Button) root.findViewById(R.id.searchButton);
.....
but.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
buttonListener.onMakeBookingButtonPressed(userSelectedOptions);
}
});
TabMainActivity.java
public void onMakeBookingButtonPressed(ArrayList<String> list) {
// TODO Auto-generated method stub
Intent intent = new Intent(TabMainActivity.this,
complainResponse.class);
intent.putStringArrayListExtra("userSelectOptions",list);
startActivity(intent);
}
complainResponse.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
setContentView(R.layout.complainresponse);
userInput = intent.getStringArrayListExtra("userSelectOptions");
// Creates the window used for the UI
if (userInput != null) {
if (userInput.get(0) != null) {
textview1 = (TextView) findViewById(R.id.textView1);
textview1.setText(userInput.get(0));
}
}
}
Error occurs at this line:
if (userInput != null) {
//of complainResponse.java
Logcat:
java.lang.IndexOutOfBoundsException
Please help
There's nothing in the ArrayList that you pass to your activity.
I suspect this bit of code isn't being executed -
if(btnSis.getCurrentTextColor()==Color.BLUE){
userSelectedOptions.add("SIS"); <------------ never gets here
}
To verify this, run the application in debug mode, and place a breakpoint at the if statement
userInput.get(0) != null
this is the cause of error in my opinion, list can be initialized but empty.
instead you should use,
if (!userInput.isEmpty())
I use .setError function. But it checks the text field only when the save button is clicked, go to SecondActivity and press Back button on device to see it can't be left empty!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_card);
save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText Name2 = (EditText)findViewById(R.id.txtName);
if( Name2.getText().toString().length() == 0 )
Name2.setError( "First name is required!" );
Intent intent = new Intent(NewCard.this, Template.class);
startActivity(intent);
}
});
Any idea on how i can check when button is clicked but not proceeding to SecondActivity if Name2 is blank? And beside length(), i also want to check for numbers, undesired characters etc. if possible. Thanks for assistance.
You just need to create an else for your if, then move your call to startActivity() into that like so:
// Expand this condiditional to perform whatever other validation you want
if( Name2.getText().toString().length() == 0 ) {
Name2.setError( "First name is required!" );
} else {
// Validation passed, show next Activity
Intent intent = new Intent(NewCard.this, Template.class);
startActivity(intent);
}