i want to set a different background color to all application activities through a click on a button, but at now i ve done it only for one activity and i can't do it for all activities. this is the rude code:
optionlayout = (RelativeLayout) findViewById(R.id.optionlayout);
backgroundbutton = (Button) findViewById(R.id.backgroundbutton);
int counter = 0;
backgroundbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (counter ==0) {
optionlayout.setBackgroundColor(0xFF5774B3);
counter++;
}else if (counter == 1){
optionlayout.setBackgroundColor(0xFF0CC258);
counter++;
} else if (counter==2){
optionlayout.setBackgroundColor(0xCC000000);
counter++;
}else if (counter== 3){
optionlayout.setBackgroundColor(0xFFFFFFFF);
counter = 0;
}
}
});
i want to change the
optionlayout.setBackgroundColor(0xFF0CC258);
with an action that changes the background in all activities permanently. thanks
You can use shared preferences and store your counter there, and in onCreate of activities first look in your shared preferences to determine what color to set your background.
You can't. There's no way to change a property of a whole application through an activity. You can create a custom class that extends Activity and implements OnClickListener.
Something like that:
class MyCustomActivity extends Activity implements View.OnClickListener{
#Override
public void onClick(View v) {
//your code inside onClick()
}
}
And then you can extend your activities to this one, for example:
class MainActivity extends MyCustomActivity { ... }
You could try to use the extras method.
Pass the changes on from activity to activity maybe it helps.
Its a longer process to set up your page each time but i don't see any other way. I'm still open to suggestions
// you can have multiple extras.
String bgColor = "yourColor";
Intent newIntent = new Intent(thisActivity.this, theNextActivity.class);
// Start new activity
dispatchIntent.putExtra("bgColor", bgColor);
// Send color to new activity
startActivity(newIntent);
finish();
// Get the background color in the new page
Bundle extras = getIntent().getExtras();
String bgColor = extras.getString("bgColor");
// The trick is to get the string into your background color format such as
0xFF5774B3
Related
I want make application on Android with multiple polygon, and when i click one polygon it show something like open window and show detail of the polygon. Is there any solution to make function like that?
Yes you can use ImageButtons for polygon images and handle their click events by opening new activity or dialog for showing information about the polygons.
You will handle the click events like this:
ImageButton ib = findViewById(R.id.polygon1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//show dialog or new activity with details
Intent i=new Intent();
int polygon_id = id_of_polygon;
i.putExtra("id", polygon_id);
i.startActivity(this,DetailsActivity.class);
finish();
}
});
You can get this value in details activity like this:
int id;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
id= 0;
} else {
id= extras.getInt("id");
}
}
Then in this activity you can show details about the polygon.
All this code will be added to the onCreate() methods of the activities.
I created an activity on android studio and I have put there something like 20 ImageButtons. I want to use it as on each click on an image it will move to a new activity. All of the Image Buttons are working on the same principle, my app is a game, and each image represents a level. I want to build one function that will be used on all buttons and will move the user to a new activity according to the data(the properties of the image button) and use that data on the new activity. Every level has its own activity and the main activity is the menu of the game.
Below is my code:
public ImageButton beatsCall; public void Beats(){ beatsCall=(ImageButton)findViewById(R.id.beats); beatsCall.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) { Intent toy = new Intent(Levels.this,Beats.class); startActivity(toy); } }); }
You need to provide more information and code. However, you may want to try set a distinct onClickListener and then set all the imageButtons to that listener that will perform an action depending on the button clicked. For example, say you have 4 imageButtons and you want to perform a different action (in your case, start a new activity) for each different button click.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
//Start activity 1 here, for example
Intent intent = new Intent(this, YourNewActivity1.class);
String message = v.getId().toString;
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
break;
case R.id.textView2:
//Start activity 2 here
break;
case R.id.textView3:
//Start activity 3 here
break;
case R.id.textView4:
//Start activity 4 here
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
This is assuming you have the imageButtons set up in your layout file and you have them initialized in your activity.
In your new activity, you can get the message as such:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
if (some condition with message){
do something
}
You may also check out this documentation for further information regarding intents.
Something like this? In your xml make your images clickable and give them ID's like this...
<ImageView
android:id="#+id/level_1_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
/>
Then call a function like this in your Activity's onCreate
private void setupButtons() {
findViewById(R.id.level_1_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), LevelOne.class));
}
});
findViewById(R.id.level_2_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), LevelTwo.class));
}
});
}
You could assign a tag via android:tag to each of your views and then use your single listener to switch on the view's tag to branch the behavior you want.
Hi i am creating a simple educational game in which user has to press three images in order to proceed to the next level and in next level user has to press 5 images.
i have gone through the on click but i using onIntent intent = new Intent(this.getApplicationContext(), Activity.class);
this.startActivity(intent, 0); i am only able to start new activity on single button pressed but i wanted to start new activity when user has finished pressing three image Buttons.
Thanks in advanced.
you can use global int variable and increase it every button click and if its more than your button number open new activity
public int btn = 0;
MyButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if(btn >= 2) {
//open your activity
}
else{
btn++;
}
}
});
Based on my understanding of your problem,I have provided a possible simple implementation.
Use a simple data structure like queue or stack. When a image is tapped, add information about the image to the data structure. After adding the information to data structure see if number of items in data structure is equal to 3 ? if yes check data structure has info about the required three images and not just info about the same image (happens if user taps on same image more than once). If condition is met then call startActivity(). Generalize this so that you can reuse the logic in different activities, irrespective of the number of images.
Hi i finally achieved my desired activity. now with this after checking two check box app will start new activity.
`private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay;
OnClickListener checkBoxListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
checkBoxListener =new OnClickListener() {
public void onClick(View v) {
if(chkIos.isChecked()&&chkAndroid.isChecked()) {
Intent i1=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
if(chkAndroid.isChecked()) {
Intent i2=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
}
}
}
};
chkIos.setOnClickListener(checkBoxListener);
chkAndroid.setOnClickListener(checkBoxListener);
}
}
`
thanks for all your answers.
I am creating a small calc app with EditText views and Im running into an runtime exception when the user leaves an EditText view empty causing the ParseInt to try and Parse nothing. Ive read that I need to 'Try' and 'Catch' this error before it occurs, but Im unsure of where and how to do this!
Any advice is much appreciated!
Here is my code:
public class HandlerExamples extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v) {
String a,b,t;
double vis;
EditText txtbox1 = (EditText)findViewById(R.id.A);
EditText txtbox2 = (EditText)findViewById(R.id.B);
EditText txtbox3 = (EditText)findViewById(R.id.t);
TextView tv = (TextView) findViewById(R.id.Answer);
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
t = txtbox3.getText().toString();
vis = ((Integer.parseInt(a)*1) + (Integer.parseInt(b)*2)) / (Double.parseDouble(t));
tv.setText(double.toString(vis));
}
}
Thanks so much!
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.xx:
//do things xx click
break;
case R.id.yy:
//do things yy click
break;
}
}
you can get the view id to know whick widget was clicked.
Changwei Yao defined one way you can do this, but here's the way most Android programmers would do this (programmatically), since it's a little easier to read and figure out what your widgets are doing:
But first, remove the implements OnClickListener from your Activity, as it's not needed.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// what you want your button to do when clicked
}
}
editText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// what you want your EditText to do when clicked
// (such as editText.setText(""))
}
}
Another way to do the same thing is to define android:onClick="insert_method_name_here" for the widgets that you want perform an action when clicked. In your case, in your main.xml (since that's what you're using in your Activity), you could write something like...
<Button android:id="#+id/testButton"
(other attributes you wish to apply to the button)
android:onClick="buttonAction" />
<EditText
(other attributes)
android:onClick="textAction" />
And then, in your Activity, you define the methods buttonAction(View v) and textAction(View v). Note that these methods must be public void, and must take the sole argument View v.
(One advantage of the XML method is that you don't necessarily have to define an android:id attribute for these widgets, unless you need to be able to manipulate them or extract information from them in your code (which means you will need to define an android:id for your EditText since you'll likely want the user's input))
If you only need to exclude the empty text field then hotveryspicy's solution is probably the quickest. For a secure solution: catching the NumberFormatException will filter anything that can not be converted to an integer.
int vis;
try {
vis = Integer.parseInt(a);
}
catch(NumberFormatException nfe) {
Log.e(TAG,"trying to convert:"+a+" to integer failed");
vis = 0;
}
EditText etHomePhone = (EditText)findViewById(R.id.et_pi_home_phone);
EditText etMobilePhone = (EditText)findViewById(R.id.et_pi_home_phone);
etHomePhone.setOnClickListener(showPopUpClickListener);
etMobilePhone.setOnClickListener(showPopUpClickListener);
private View.OnClickListener showPopUpClickListener = new View.OnClickListener() {
public void onClick(View v) {
/* I like to get both EditText.getText().toString() value in this one ClickListener
is it Possible, there is something in ActionScript call event.currentTarget...
*/
}
};
If you declare your etHomePhone and etMobilePhone variables as final and define your OnClickListener inline in the same method, then you can refer to those variables directly. Like so:
final EditText etHomePhone = (EditText)findViewById(R.id.et_pi_home_phone);
final EditText etMobilePhone = (EditText)findViewById(R.id.et_pi_home_phone);
View.OnClickListener showPopUpClickListener = new View.OnClickListener() {
public void onClick(View v) {
String home = etHomePhone.getText().toString();
String mobile = etMobilePhone.getText().toString();
// Do something with home and mobile
}
};
etHomePhone.setOnClickListener(showPopUpClickListener);
etMobilePhone.setOnClickListener(showPopUpClickListener);
EDIT:
If you only want to get the text for the EditText that was clicked instead of both of them, then you can just cast the View that it delivered via onClick(View v):
public void onClick(View v) {
EditText editText = (EditText)v;
String phoneNumber = editText.getText().toString();
// Do something with phoneNumber
}
This is a snippet, so I can't see the context but you can structure a single OnClickListener to catch all events in your activity. First, your activity will need to implement the OnClickListener interface:
public class YourActivity extends Activity implements OnClickListener {...
In the OnCreate() method, register each UI element which should respond to clicks like this:
yourObject.setOnClickListener(this);
Next, for the activity's onClick() method, create a switch structure using R.id like this:
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.A_UI_Element:
//do what you need for this element
break:
case R.id.A_Different_UI_Element:
//do what you need for this element
break;
//continue with cases for each element you want to be clickable
}
}
Don't forget the break at the end of each case.
One thing I noticed in your code: both editText declarations refer to the same resource. This gives you two handles to the same UI element, not 2 different elements.