I have made a simple android project with a single activity containing three buttons and a seek bar,Now i want is that when i press 1st button the seek bar should directly progressed 33%(without showing progress),when 2nd button pressed the seek bar should progressed again by 33% second time and when third button pressed the seek bar should full progressed..my code is as below:
Main.java
package com.example.seekabardemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends Activity {
Button btn,btn1,btn2;
SeekBar sk;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
btn1=(Button)findViewById(R.id.button2);
btn2=(Button)findViewById(R.id.button3);
sk=(SeekBar)findViewById(R.id.seekBar1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Thank you....Please help me..!
Please help me ..thank you..!
write this three statements in click events of each button respectively.
sk.setProgress(sk.getMax()/3);
sk.setProgress(sk.getMax()*2/3);
sk.setProgress(sk.getMax());
To set a seekBar progress just :
sk.setProgress(ProgressToSet);
to get the maximum possible progress just:
sk.getMax();
to get a porcentage of a seekbar just:
sk.setProgress(sk.getMax()/100*YourPercentage);
Related
package com.example.geoquiz;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends ActionBarActivity {
Button mTrueButton;
Button mFalseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button)findViewById(R.id.button1);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
}
});
mFalseButton = (Button)findViewById(R.id.button2);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, 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);
}
}
Please help guys. Getting a NullPointerException at mTrueButton.setOnClickListener(new View.OnClickListener().
I know other ways to set the OnClickListener but just wanted to know what's wrong with this code. Thanks in advance
You need to initialize your mTrueButton. Likely with a call to findViewById() after setContentView(), like you're initializing mFalseButton.
You forgot to initialize mTrueButton buttons:
mTrueButton= (Button)findViewById(R.id.buttonId);
You need to initialize mTrueButton like you did with mFalseButton
For eg.
mTrueButton = (Button)findViewById(R.id.button1); \\ button1 is whatever id you have given to mTrueButton
Until you do that mTrueButton is null and trying to call any method on it will result in a NullPointerException
Just initialize your mTrueButton like this:
mTrueButton = (Button)findViewById(R.id.button1);
before setting onClickListener
You forgot to initialize the buttons:
Button mTrueButton;
Button mFalseButton;
just add the findViewById() method to each button so you can access to them.
mTrueButton = (Button)findViewById(R.id.button1);
I have a program and when I try to start it, it crashes. I really don't understand why, even Eclipse show's that there are no errors.
I can show you the code of a page on which I think is the problem.
package ctect.android.maxipro;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class BasicScreenActivity extends Activity {
private Button butonul1;
private Button butonul2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_screen);
butonul1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View currentView) {
// TODO Auto-generated method stub
butonul2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View currentView) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(currentView.getContext(), NeedForSpeedActivity.class);
startActivityForResult(myIntent, 0);
Intent myIntent2 = new Intent(currentView.getContext(), Fifa2012Activity.class);
startActivityForResult(myIntent2, 0);
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.basic_screen, menu);
return true;
}
}
if somebody understands, please help me.
You forgot to assign an object to butonul1 before using it. You need to add this line before butonul1.setOnClickListener:
butonul1= (Button) findViewById(R.id.butonul1);
This is assuming that you gave it the id butonul1 in your layout file.
I have made a simple checkbo[x demo program in android,in that i've put a chekbox and a button.I want is that when chekbox is pressed and button is pressed thet toast showing "i accept" else when button pressed toast generated as "i dont accept"...i have made it but problem is when i unchecked the chekbox..it shows toast directly(without clicking button);
mainActivity.java
package com.example.chechboxdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
CheckBox c1;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c1=(CheckBox)findViewById(R.id.checkBox1);
b=(Button)findViewById(R.id.button1);
c1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(c1.isChecked()){
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "i accept", 1).show();
}
});
}
else{
Toast.makeText(getApplicationContext(), "i don't accept", 1).show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You shouldn't be using the checkChanged listener unless you are wanting something to happen when the user checks/unchecks the check box. Since you want the action to happen only when the button is clicked you need only your onClickListener and it should be outside of your onCheckChangedListener
Try like this:
public class MainActivity extends Activity {
CheckBox c1;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c1=(CheckBox)findViewById(R.id.checkBox1);
b=(Button)findViewById(R.id.button1);
c1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// You don't need to use onCheckChangeListener.
}
});
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(c1.isChecked()){
Toast.makeText(getApplicationContext(), "i accept", 1).show();
}else{
Toast.makeText(getApplicationContext(), "i don't accept", 1).show();
}
}
});
}//end onCreate
}//end MainActivity
move c1.isChecked() inside Button Click to check if CheckBox is checked or not on Button click then show toast message according to CheckBox status. change your code as :
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(c1.isChecked()){ //<<< check here if checkbox clicked or not
Toast.makeText(getApplicationContext(),
"i accept", 1).show();
}else{
Toast.makeText(getApplicationContext(),
"i don't accept", 1).show();
}
}
});
I have just started to learn how to program android apps, And im a COMPLETE NOOB.
I cant figure out how to fix this! please help! The format may look butchered... It's cause i have no clue what im doing!
package com.smiggle.bmxhandbook;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button button;
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick1(View arg0) {
Intent myIntent = new Intent(MainActivity.this, Trick.class
MainActivity.this.startActivity(myIntent);
}
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 void onClick(View v) {
// TODO Auto-generated method stub; }
}
It keeps on giving me a syntax error!
Try this code instead:
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this, Trick.class);
MainActivity.this.startActivity(myIntent);
}
});
}
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;
}
}
Your class had a ridiculously high amount of syntax and logical errors:
Arbitrary onCreate1() method, which will never me called.
Arbitrary onClick() method, even though your Activity never implemented the interface
Your anonymous inner class' onClick() method was named onClick1()
The statement with the Intent was missing a ); at the end.
The inner class body was missing a closing });
The addListenerOnButton() method was missing a closing }
Your entire class was missing a closing }
I sincerely recommend you spend a few months (or even a year) learning Java before coming to Android.
You have this wide open
Intent myIntent = new Intent(MainActivity.this, Trick.class
You need to close that with a right parenthesis and and a semicolon.
Also that onClick should not have a 1 in it:
public void onClick1(View arg0) {
I have an activity class with many buttons. If i click one button then it will go to next page then go back to main class.If I click another button in main class, it will go to next page together with data. Do anyone know how to write the function in activity class?
Can I write like this in a class? But when i run it only one button is working , when i clicked other i get error. I am new to android ,so please give me suggestion.
public class MyClass extends Activity {
private Button button,button1,button2;
public void onCreate(){.... initControl();}
public void initControl() { button=(Button)findViewById(R.id.button); .....
button.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
button1.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
button2.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
}
thanks for help.
First initialize button1, button2 before overriding onCreate(). Then assign the values in oncreateMethod call initializemthod
Have a look on the following code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class WebViewTest extends Activity {
Button button1 = null;
Button button2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.firstbutton);
button2 = (Button) findViewById(R.id.secondbutton);
initControl();
}
public void initControl() {
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
}
Thanks
Deepak
You have to add on click method