i have 9 buttons in my activity, i use push right animation to show my buttons on Create, i change duration to set display, so i have 9 animation.xml with diffrent duration (from 1000 to 5000 for ex).
also i have to repeat 3 line code for 9 time in activity ,
final Button b = (Button)findViewById(R.id.Button06);
Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
b.startAnimation(anim);
so, i need to know is there any easier way to make something like that with Less code?
for ex, use 1 animation.xml and define button to run animation One after another?!
my full code:
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button b = (Button)findViewById(R.id.Button06);
Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
b.startAnimation(anim);
final Button b1 = (Button)findViewById(R.id.Button03);
Animation anim1=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in1);
b1.startAnimation(anim1);
final Button b2 = (Button)findViewById(R.id.button1);
Animation anim2=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in2);
b2.startAnimation(anim2);
final Button b3 = (Button)findViewById(R.id.Button08);
Animation anim3=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in3);
b3.startAnimation(anim3);
final Button b4 = (Button)findViewById(R.id.Button04);
Animation anim4=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in4);
b4.startAnimation(anim4);
final Button b5 = (Button)findViewById(R.id.Button01);
Animation anim5=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in5);
b5.startAnimation(anim5);
final Button b6 = (Button)findViewById(R.id.Button07);
Animation anim6=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in6);
b6.startAnimation(anim6);
final Button b7 = (Button)findViewById(R.id.Button05);
Animation anim7=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in7);
b7.startAnimation(anim7);
final Button b8 = (Button)findViewById(R.id.Button02);
Animation anim8=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in8);
b8.startAnimation(anim8);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startButtonAnimation(b);
}
});
}
public void startButtonAnimation(Button btn){
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
btn.setAnimation(shake);
btn.startAnimation(shake);
shake.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(getApplicationContext(), Activity2.class));
overridePendingTransition(R.anim.animation, R.anim.animation2);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Yes you can create method
private void buttonAnim(int buttonId, long duration){
Button b = (Button) findViewById(buttonId);
Animation anim=AnimationUtils.loadAnimation(MainActivity.this,your_anim_id);
anim.setDuration(duration);
b.startAnimation(anim);
}
And use it to call your animation for ex:
buttonAnim(R.anim.Button06, 1000);
Additionally you can put your buttons in array and do it in loop as is shown in the code below:
int[] buttonsArrays = new int {
R.anim.Button01,
R.anim.Button02,
R.anim.Button06,
...
}
int[] durations = new int {
1000,
5000,
1000,
...
}
And in your code:
for(int index=0; index <buttonsArrays.length; index++){
buttonAnim(buttonsArrays[index],durations[index]);
}
But remember the lengths of arrays have to be the same
Or you can use Pair object as is shown in the code below:
Pair<Integer, Long>[] pairs = new Pair[]{
new Pair(R.id.Button01, 1000l),
new Pair(R.id.Button02, 5000l),
...
};
And in your code:
for(Pair<Integer,Long> pair : pairs ){
buttonAnim(pair.first,pair.second);
}
It will be most save
EDIT
Please find my proposition of your class:
public class MainActivity extends ActionBarActivity {
private final ButtonSupport[] buttonSupports = new ButtonSupport[]{
new ButtonSupport(R.id.Button06,1000l, YourClassActivity.class),
new ButtonSupport(R.id.Button03, 2000l,YourClassActivity2.class),
new ButtonSupport(R.id.button1, 3000l,YourClassActivity3.class),
new ButtonSupport(R.id.Button08, 4000l,YourClassActivity4.class),
new ButtonSupport(R.id.Button04, 5000l,YourClassActivity5.class),
new ButtonSupport(R.id.Button01, 6000l,YourClassActivity6.class),
new ButtonSupport(R.id.Button07, 7000l,YourClassActivity7.class),
new ButtonSupport(R.id.Button05, 8000l,YourClassActivity8.class),
new ButtonSupport(R.id.Button02, 9000l,YourClassActivity9.class),
};
private static class ButtonSupport{
final int buttonId;
final long duration;
final Class<? extends Activity> clazz;
ButtonSupport(int buttonId, long duration, Class<? extends Activity> clazz) {
this.buttonId = buttonId;
this.duration = duration;
this.clazz = clazz;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (ButtonSupport buttonSupport : buttonSupports) {
animButton(buttonSupport);
}
}
private void animButton(final ButtonSupport buttonSupport) {
final Button button = (Button) findViewById(buttonSupport.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startButtonAnimation(v, buttonSupport.clazz);
}
});
Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.your_animation);
anim.setDuration(buttonSupport.duration);
button.startAnimation(anim);
}
public void startButtonAnimation(View btn, final Class<? extends Activity> clazz) {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
btn.setAnimation(shake);
btn.startAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(getApplicationContext(), clazz));
overridePendingTransition(R.anim.animation, R.anim.animation2);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Related
So I am trying to create an android app to show some simple battery information. And now I want to take that info and plot a graph inside the app. I have the following codes:
public class MainActivity extends ActionBarActivity {
private TextView level,voltage, status1,temp,health1,tech,sour,amp;
Thread myThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
level=(TextView)findViewById(R.id.level);
voltage=(TextView)findViewById(R.id.volt);
status1=(TextView)findViewById(R.id.stat);
temp=(TextView)findViewById(R.id.temp);
health1=(TextView)findViewById(R.id.healt);
tech=(TextView)findViewById(R.id.tech);
sour=(TextView)findViewById(R.id.source);
Button b = (Button) findViewById(R.id.ex);
Button g = (Button) findViewById(R.id.graphButton);
amp=(TextView)findViewById(R.id.current);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
g.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//how can i jump to DynamicGraphActivity
}
});
this.registerReceiver(this.myBatteryReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver myBatteryReceiver
= new BroadcastReceiver(){
#SuppressLint("InlinedApi")
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
int lv = arg1.getIntExtra("level", 0);
level.setText("Level: "
+ String.valueOf(lv) + "%");
voltage.setText("Voltage: "
+ String.valueOf((float)arg1.getIntExtra("voltage", 0)/1000) + "V");
temp.setText("Temperature: "
+ String.valueOf((float)arg1.getIntExtra("temperature", 0)/10) + "c");
tech.setText("Technology: " + arg1.getStringExtra("technology"));
int status = arg1.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
String strStatus;
if (status == BatteryManager.BATTERY_STATUS_CHARGING){
strStatus = "Charging";
} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING){
strStatus = "Dis-charging";
} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
strStatus = "Not charging";
} else if (status == BatteryManager.BATTERY_STATUS_FULL){
strStatus = "Full";
} else {
strStatus = "Unknown";
}
status1.setText("Status: " + strStatus);
//int source=arg1.getIntExtra("source", BatteryManager.BATTERY_STATUS_UNKNOWN);
if(Build.VERSION.SDK_INT >= 21){
BatteryManager battery = (BatteryManager)getSystemService(Context.BATTERY_SERVICE);
int current=battery.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
int currentAvg=battery.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
int energy=battery.getIntProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);
int capacity=battery.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
int bCapacity=battery.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
String string1 = "Current: "+ current*1000+" uA"+"\n";
string1+="Average Current: "+currentAvg+" uA"+"\n";
string1+="Remaining energy: "+energy+" nWh"+"\n";
string1+="Capacity: "+capacity+" uAh"+"\n\n";
amp.setText(string1);
}
int health = arg1.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);
String strHealth;
if (health == BatteryManager.BATTERY_HEALTH_GOOD){
strHealth = "Good";
} else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT){
strHealth = "Over Heat";
} else if (health == BatteryManager.BATTERY_HEALTH_DEAD){
strHealth = "Dead";
} else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){
strHealth = "Over Voltage";
} else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){
strHealth = "Unspecified Failure";
} else{
strHealth = "Unknown";
}
health1.setText("Health: " + strHealth);
}
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
2nd Activity or DynamicGraphActivity:
public class DynamicGraphActivity extends Activity {
private static GraphicalView view;
private LineGraph line = new LineGraph();
private static Thread thread;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
thread = new Thread() {
public void run()
{
for (int i = 0; i < 15; i++)
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point p = MockData.getDataFromReceiver(i); // We got new data!
line.addNewPoints(p); // Add it to our graph
view.repaint();
}
}
};
thread.start();
}
#Override
protected void onStart() {
super.onStart();
view = line.getView(this);
setContentView(view);
}
}
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.content.Context;
import android.graphics.Color;
public class LineGraph {
private GraphicalView view;
private TimeSeries dataset = new TimeSeries("Rain Fall");
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used to customize line 1
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph
public LineGraph()
{
// Add single dataset to multiple dataset
mDataset.addSeries(dataset);
// Customization time for line 1!
renderer.setColor(Color.WHITE);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
// Enable Zoom
mRenderer.setZoomButtonsVisible(true);
mRenderer.setXTitle("Day #");
mRenderer.setYTitle("CM in Rainfall");
// Add single renderer to multiple renderer
mRenderer.addSeriesRenderer(renderer);
}
public GraphicalView getView(Context context)
{
view = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view;
}
public void addNewPoints(Point p)
{
dataset.add(p.getX(), p.getY());
}
}
import java.util.Random;
public class MockData {
// x is the day number, 0, 1, 2, 3
public static Point getDataFromReceiver(int x)
{
return new Point(x, generateRandomData());
}
private static int generateRandomData()
{
Random random = new Random();
return random.nextInt(40);
}
}
public class Point {
private int x;
private int y;
public Point( int x, int y)
{
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Now inside the button g, I want to call DynamicGraphActivity class so that it calls the class and plots a graph using some random values. but its not working. When i click on the button, it doesnt do anything. How can I fix this?
And my another question is, how can I plot the battery info such as voltage, charge or discharge over time using these codes/
Any help would be greatly appreciated.
Thank you
Well there is a difference between any class or an activity.
Activity have a visual layout maps to it.Check this link for more info.
Inside your g button click listener add this code.
g.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DynamicGraphActivity.class);
startActivity(intent);
}
});
Also you need to add the other activity in the AndroidManifest.xml as well under application tag.(You already have activity tag inside application tag if you create a new app.But you don't need any intent filters for newer activity)
<activity
android:name=".DynamicGraphActivity"
/>
I noticed one more thing you are using same layout(main.xml) for both activities.
Create another layout in layouts folder and map it to the DynamicGraphActivity.
Lets say you create second.xml.
So your DynamicGraphActivity oncreate() have:
setContentView(R.layout.second);
Intent 2ndActivity = new Intent(this, blabla.class);
startActivity(2ndActivity);
For the button and for the extra value you can use Bundle,Parcel or just use putExtra.
And for the battery info, Read from google developer
In your button click put this code
Intent intent = new Intent(MainActivity.this, DynamicGraphActivity.class);
startActivity(intent);
and define this activity in your AndroidManifest.xml
<activity
android:name=".DynamicGraphActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
and for Battery information you can refer this links...
http://developer.android.com/reference/android/os/BatteryManager.html
http://mobiledevtuts.com/android/android-sdk-get-device-battery-information/
Get battery level and state in Android
I want to calculate a value from the data in 2 text edits and show the result in a textview. How can I do it?
BMI calculator:
private TextView deinbmi;
private EditText weight;
private EditText height;
public void calculator ()
{
double ergebnis;
weight=(EditText)findViewById(R.id.text_calculator_weight);
height=(EditText)findViewById(R.id.text_calculator_height);
double a=Double.parseDouble(weight.getText().toString());
double b=Double.parseDouble(height.getText().toString());
ergebnis=a*b;
deinbmi.setText(""+ergebnis);
}
I don't have an idea; I just started with Java. I can program with C++ but just console.
If I search on the internet for this problem, everyone has another version of solving this, but none of these answers worked for me?
Try This Working code.
you Missing the Textview Inside oncreate, Then Add a Button for Getting the Answer
public class Mainactivity extends Activity implements OnClickListener
{
private TextView deinbmi;
private EditText weight;
private EditText height;
Button Answer;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.page1_landing);
weight=(EditText)findViewById(R.id.text_calculator_weight);
height=(EditText)findViewById(R.id.text_calculator_height);
deinbmi=(Textview)findViewById(R.id.text);
Answer=(Button)findViewById(R.id.answerbuttton);
Answer.SetOnclicklistener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.answerbuttton:
calculator ();
break;
}}
public void calculator ()
{
double ergebnis;
double a=Double.parseDouble(weight.getText().toString());
double b=Double.parseDouble(height.getText().toString());
ergebnis=a*b;
deinbmi.setText(""+ergebnis);
}
}
public class calculator extends ActionBarActivity {
private TextView deinbmi;
private EditText weight;
private EditText height;
public void calculator ()
{
double ergebnis;
weight=(EditText)findViewById(R.id.text_calculator_weight);
height=(EditText)findViewById(R.id.text_calculator_height);
double a=Double.parseDouble(weight.getText().toString());
double b=Double.parseDouble(height.getText().toString());
ergebnis=a*b;
deinbmi.setText(""+ergebnis);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_calculator, menu);
//rechnen ?!
Button credits_calculate = (Button)
findViewById(R.id.button_calculate);
credits_calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calculator();
}
});
//back
Button credits_back = (Button)
findViewById(R.id.button_calculator_back);
credits_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
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);
}
}
is my activity
i tryed to use your code but
Answer=(Button)findViewById(R.id.answerbuttton);
Answer.SetOnclicklistener(this);
he didnt understand this
I´m new in Android Programming and I have a question for my project.
I want to Change my Background for my Start-Activity (it´s a Reaction Game).
There is an extra activity for all settings (BackgroundColor, Sound etc.),
and I´m lost at this part :/ I want to choose one Color in the Setting-Activity (blue & red), and change the background to the choosen color in the start-Acitivity. Here is the Code for the Activity-Setting, the RadioButtons for the Colors are in one Group (RadioGroup_Color). Does anybody know how to fix this?
I always get an error when I execute this code:
public class activity_settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_settings);
colorchange();
}
public void colorchange() {
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RadioGroup_Color);
final RelativeLayout background = (RelativeLayout) findViewById(R.id.start);
final Button button_save = (Button) findViewById(R.id.button_save);
button_save.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
changeOption(background);
}
});
final RadioButton changeToBlue = (RadioButton) findViewById(R.id.button_blue);
changeToBlue.setOnCheckedChangeListener(new radioGroup.OnCheckedChangeListener() {
public void onClick(View v) {
changeToBlue(background);
}
});
final RadioButton changeToRed = (RadioButton) findViewById(R.id.button_red);
changeToRed.setOnCheckedChangeListener(new radioGroup.OnCheckedChangeListener() {
public void onClick(View v) {
changeToRed(background);
}
});
}
public void changeOption(RelativeLayout background) {
if (background.isEnabled()) {
background.setEnabled(false);
} else {
background.setEnabled(true);
}
}
public void changeToBlue(RelativeLayout background) {
background.setBackgroundColor(0x0000FF00);
background.invalidate();
}
public void changeToRed(RelativeLayout background) {
background.setBackgroundColor(0x0000FF00);
background.invalidate();
}
#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_activity_settings, 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);
}}
The Error-Message:
Error:(38, 91) error: <anonymous com.example.clecks.reaction_game.activity_settings$2> is not abstract and does not override abstract method onCheckedChanged(CompoundButton,boolean) in OnCheckedChangeListener
EDIT
After suggestions and fixes, I still get:
Error:(8, 8) error: com.example.clecks.reaction_game.OnCheckedChangeListener is not abstract and does not override abstract method onCheckedChanged(CompoundButton,boolean) in android.widget.CompoundButton.OnCheckedChangeListener.
When i click on it there opens a new class named OnCheckedChangeListener.java:
public class OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener { }
Let's take your actual error:
Error:(38, 91) error: is not abstract and does not override abstract method onCheckedChanged(CompoundButton,boolean) in OnCheckedChangeListener
You miss a onCheckedChanged(CompoundButton,boolean) implementation on a OnCheckedChangeListener.
Looking at your code I can see two OnCheckedChangeListener inplementations; here is one:
changeToBlue.setOnCheckedChangeListener(new radioGroup.OnCheckedChangeListener() {
public void onClick(View v) {
changeToBlue(background);
}
});
Those implementations are wrong.
You need to override onCheckedChanged(CompoundButton,boolean) (onClick(View) does not exists in OnCheckedChangeListener) as the error reports. Also new radioGroup.OnCheckedChangeListener is wrong and causes the main error.
Fix your code with:
changeToBlue.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton c, boolean b) {
changeToBlue(background);
}
});
Have a look at the simple official documentation.
I use animation to display my buttons, i have 9 buttons so i used duration for display, but it is run animation for all buttons at same time and just with diffrent duration, of course it is correct, but i want to run animation for button1 first and when it is done, do it again for button2 and when it is done too, do it for next buttons....
I mean run same animation with same speed for different buttons one after another,not same time. now my apk is this:
https://drive.google.com/file/d/0B9Q0pN8FVwEORGRQVE1kQmtvS28/view?usp=sharing
it must be like this:
http://up.vbiran.ir/uploads/4517142605716735676_animationn.gif
my full code:
public class MainActivity extends ActionBarActivity {
public MediaPlayer player;
public MediaPlayer playerp;
private final ButtonSupport[] buttonSupports = new ButtonSupport[]{
new ButtonSupport(R.id.Button06, 1000l, YourClassActivity.class),
new ButtonSupport(R.id.Button03, 2000l,YourClassActivity2.class),
new ButtonSupport(R.id.button1, 3000l,YourClassActivity3.class),
new ButtonSupport(R.id.Button08, 5000l,YourClassActivity4.class),
new ButtonSupport(R.id.Button04, 6000l,YourClassActivity5.class),
new ButtonSupport(R.id.Button01, 7000l,YourClassActivity6.class),
new ButtonSupport(R.id.Button07, 9000l,YourClassActivity7.class),
new ButtonSupport(R.id.Button05, 10000l,YourClassActivity8.class),
new ButtonSupport(R.id.Button02, 11000l,YourClassActivity9.class),
};
private static class ButtonSupport{
final int buttonId;
final long duration;
final Class<? extends Activity> clazz;
ButtonSupport(int buttonId, long duration, Class<? extends Activity> clazz) {
this.buttonId = buttonId;
this.duration = duration;
this.clazz = clazz;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (ButtonSupport buttonSupport : buttonSupports) {
animButton(buttonSupport);
}
}
private void animButton(final ButtonSupport buttonSupport) {
final Button button = (Button) findViewById(buttonSupport.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startButtonAnimation(v, buttonSupport.clazz);
}
});
Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
anim.setDuration(buttonSupport.duration);
button.startAnimation(anim);
}
public void startButtonAnimation(View btn, final Class<? extends Activity> clazz) {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
btn.setAnimation(shake);
btn.startAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
player = MediaPlayer.create(MainActivity.this, R.raw.music);
player.setLooping(true); // Set looping
player.setVolume(1,1);
player.start();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
player.stop();
startActivity(new Intent(getApplicationContext(), clazz));
overridePendingTransition(R.anim.animation, R.anim.animation2);
playerp = MediaPlayer.create(MainActivity.this, R.raw.musicp);
playerp.setLooping(false); // Set looping
playerp.setVolume(1,1);
playerp.start();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
animation xml code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="1000"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
</set>
I guess the easiest change to make it work looks like this:
int ANIM_OFFSET=100;
....
for (int i=0;i<buttonSupports.length;i++) {
ButtonSupport buttonSupport =buttonSupports[i];
animButton(buttonSupport,i);
}
....
anim.setStartOffset(i*ANIM_OFFSET);
I am using Sliding menu from library jfeinstein, i have two sliding menu 'menuLeft' 'menuRight' in my activity one from left side and one from right,i have toggle buttons for respective sliding menu,how ever if menuLeft is open and if i slide from right to left in order to close menuLeft ,menuRiht also gets opened,what can be the solution to avoid this misbehaviour
here's my activity which contains SlidingMenu's
public class ChatListActivity extends SherlockActivity {
private SlidingMenu menuLeft;
private SlidingMenu menuRight;
private Button btnSliderLeftToggle;
private Button btnSliderRightToggle;
private ListView lvSliderLeft;
private ListView lvSliderRight;
private int width;
private int height;
private DBContacts db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.chatlist_layout);
db = new DBContacts(this);
WindowManager wmanager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = wmanager.getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
menuLeft = new SlidingMenu(this);
menuRight = new SlidingMenu(this);
initLeftSlider();
initRightSlider();
btnSliderLeftToggle = (Button) findViewById(R.id.mnuSlidingleftToggle);
btnSliderLeftToggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
ChatListActivity.this.menuLeft.toggle();
}
});
btnSliderRightToggle = (Button) findViewById(R.id.mnuSlidingRightToggle);
btnSliderRightToggle.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
ChatListActivity.this.menuRight.toggle();
}
});
menuLeft.setOnOpenedListener(new OnOpenedListener()
{
#Override
public void onOpened()
{
lvSliderLeft = (ListView) findViewById(R.id.lvSlidingmenuLeft);
MySLidingMenuLeftAdapter adapter = new MySLidingMenuLeftAdapter(ChatListActivity.this,
R.layout.crow_listview_lvslidingleft_chatlist);
lvSliderLeft.setAdapter(adapter);
}
});
menuRight.setOnOpenedListener(new OnOpenedListener()
{
#Override
public void onOpened()
{
lvSliderRight = (ListView) findViewById(R.id.lvSlidingmenuRight);
String column[] = new String[] { DBContacts.USERNAME};
int[] viewId = { R.id.txtContactName};
Cursor dataBaseCursor = db.getAllContacts();
MySLidingMenuRightAdapter customContactListAdapter = new MySLidingMenuRightAdapter(
ChatListActivity.this, R.layout.crow_lvslidingmenu_right_chatlist, dataBaseCursor, column,
viewId, 0);
lvSliderRight.setAdapter(customContactListAdapter);
}
});
}
private void initRightSlider()
{
menuRight.setMode(SlidingMenu.RIGHT);
menuRight.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menuRight.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menuRight.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menuRight.setMenu(R.layout.sliding_menu_chatlist_right);
menuRight.setFadeDegree(0.35f);
}
private void initLeftSlider()
{
menuLeft.setMode(SlidingMenu.LEFT);
menuLeft.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menuLeft.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menuLeft.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menuLeft.setMenu(R.layout.sliding_menu_chatlist_left);
menuLeft.setFadeDegree(0.35f);
}
#Override
public void onBackPressed()
{
super.onBackPressed();
System.exit(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Refresh");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
new SendNativeContacts(this).execute();
return true;
}
}
I have never faced this issue when i used both Left and Right SlidingMenu but for your problem you can have look at SlidingMenu Issues and you can try this solution for your problem. It may help you.
EDIT :
try this
You will need a patch which calls onOpened and onClosed methods for right menu it may have been included in latest code. Write logs to check method calls.
slidingMenuLeft.setOnOpenedListener(new OnOpenedListener() {
#Override
public void onOpened(int pos) {
slidingMenuRight.setSlidingEnabled(false);
}
});
slidingMenuLeft.setOnClosedListener(new OnClosedListener() {
#Override
public void onClosed() {
slidingMenuRight.setSlidingEnabled(true);
}
});
slidingMenuRight.setOnOpenedListener(new OnOpenedListener() {
#Override
public void onOpened(int pos) {
slidingMenuLeft.setSlidingEnabled(false);
}
});
slidingMenuRight.setOnClosedListener(new OnClosedListener() {
#Override
public void onClosed() {
slidingMenuLeft.setSlidingEnabled(true);
}
});