Using a simple seekbar in android - android

How to get the values from SeekBar and pass them as intents to another activity
I have three seekBars as below
PRICEbar
DISTANCEbar
RATINGbar
I want to get the selected value from the three seek bars and pass them as intents to another activity through DoneIntent
How to make this happen
Filters.java
public class Filters extends Activity implements OnSeekBarChangeListener{
// declare text objects variables
private SeekBar PRICEbar,DISTANCEbar, RATINGbar;
private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;
Button back;
Button Done;
private RadioGroup rdg;
private RadioButton indian;
private RadioButton thai;
private RadioButton chinese;
private String selectedType="";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// load the layout
setContentView(R.layout.filters);
/** Finding all the views in this Activity. */
back=(Button) findViewById(R.id.TopNavigationBarFilterBackButton);
Done=(Button) findViewById(R.id.SEARCH_BUTTON_ID);
Done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent DoneIntent=new Intent(Filters.this,AndroidTabRestaurantDescFilterListView.class);
DoneIntent.putExtra("REST1",selectedType);
startActivity(DoneIntent);
}
});
PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
PRICEbar.setOnSeekBarChangeListener(this);
PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
PRICEtextProgress.setText("Price:: Rs "+progress);
seekBar.setMax(100);
}
});
}

use PRICEbar.getProgress(); to get current value of your seekbar. and then pass them as intent with this code:
Intent intent = new Intent(MainActivity.this, YourNewActivity.class);
intent.PutExtra("PriceBar", PRICEbar.getProgress());
intent.PutExtra("DistanceBar", DISTANCEbar.getProgress());
intent.PutExtra("RatingBar", RATINGbar.getProgress());
startActivity(intent);

Related

Change TextView size using a SeekBar

It works well when I try to Change text size in (EditText) by putting a constant value in code <<<<
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText Text = (EditText)findViewById(R.id.editText);
SeekBar f = (SeekBar)findViewById(R.id.seekBar);
Text.setTextSize(55);
Text.setText("Hello");
}
But I Can't get it to work by using SeekBar:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText Text = (EditText)findViewById(R.id.editText);
SeekBar f = (SeekBar)findViewById(R.id.seekBar);
Text.setTextSize((float)f.getProgress());
Text.setText("Hello");
}
You can change the text size using this listener:
f.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
Text.setTextSize(progress);
}

Android animation using radio button

I created one animation in which I used one radio button as object and a matrix background. when the activity starts the object will move along the each cells in the matrix. now I want the object to be stopped when i click on it. how can i do this??
public class MainActivity extends Activity {
RadioButton rad;
Animation animation1;
Context context = this;
Button move;
Handler handler = new Handler();
Runnable handlerTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridView1);
rad = (RadioButton) findViewById(R.id.radioButton1);
// move=(Button)findViewById(R.id.button1);
gridview.setAdapter(new ImageAdapter(this));
animation1 = AnimationUtils.loadAnimation(this, R.anim.animation);
// userInputHandler();
/*
* move.setOnClickListener(new View.OnClickListener() {
*
* #Override public void onClick(View arg0) { // TODO Auto-generated
* method stub
*/
handlerTask = new Runnable() {
public void run() {
if (rad.isChecked()) {
animation1.cancel();
} else {
rad.startAnimation(animation1);
}
}
};
handlerTask.run();
// }
// });
}
public void onAnimationEnd(Animation animation) {
rad.setVisibility(View.VISIBLE);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
rad.setVisibility(View.VISIBLE);
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
rad.setVisibility(View.VISIBLE);
}
}

how do i update the database table on the basis of checkbox/unchecked

how can i retrieve the checkboxes value to update the database table on pressing the update button...
TeacherLoggedInClass
public class TeacherLoggedInPage extends Activity implements OnClickListener {
Button UpdateAttendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.attendancesheet);
UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
UpdateAttendanceButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
The isChecked() method return a boolean value with the information you need.
I would also suggest you use another way with implementing the Listener. This is what Google recommends:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click... for example your checkBox.isChecked()
}
});
}
If you want to get the single checkbox value and set database, you can follow below code,
public class TeacherLoggedInPage extends Activity implements OnClickListener {
Button UpdateAttendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.attendancesheet);
UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
UpdateAttendanceButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (cbBox.isChecked()) {
// do your operation
} else {
// do your operation
}
}
}
if you need to use a list of checkbox status to update databases, you'd better to create a collection to cache every checkbox status , and with bulkInsert or transaction to commit.

Force close error

What is the force close problem of this program?
public class MyActivity extends Activity {
TextView t=(TextView)findViewById(R.id.textView1);
Button r=(Button)findViewById(R.id.button2);
private OnClickListener i=new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("fghffghfhgf");
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
r.setOnClickListener(i);
}
}
You need to get your TextView and Button after inflating the layout.
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//here inflate the layout
setContentView(R.layout.main);
//now you can get your widgets
final TextView t= (TextView)findViewById(R.id.textView1);
Button r=(Button)findViewById(R.id.button2);
r.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("fghffghfhgf");
}
};
);
}
}
I really recommend you to check this to build your first app.

Update a TextView on resuming main activity

Friends in this code provided below, i want to refresh my text view upon resume from play intent. But whenever i try to define my textview out of OnCreate but inside my main class (after static int score), my app crashes.
public class MainProjectActivity extends Activity {
/** Called when the activity is first created. */
static int Score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Display Scores
final TextView displayScores = (TextView)findViewById(R.id.scoreDisplay);
displayScores.setText("Your Score : "+ Score);
//Play Game button activity
Button gameButton = (Button)findViewById(R.id.PlayButton);
gameButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent play = new Intent(getApplicationContext(), com.sample.game.PlayScreen.class);
startActivity(play);
}
});
I'd start with adding super.onResume();:
#Override
protected void onResume(){
super.onResume();
// The rest
}
I would also remove that:
final TextView displayScores = (TextView)findViewById(R.id.scoreDisplay);
displayScores.setText("Your Score : "+ Score);
from onCreate,and add it to onResume() since every time onCreate is called, onResume is called as well.
also change from public to protected onResume()
try this:
public class MainProjectActivity extends Activity {
/** Called when the activity is first created. */
TextView displayScores;
static int Score = 0;
#Override
protected void onResume(){
super.onResume();
// code to update the date here
displayScores.setText("Your Score : "+ Score);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Display Scores
displayScores = (TextView)findViewById(R.id.scoreDisplay);
displayScores.setText("Your Score : "+ Score);
//Play Game button activity
Button gameButton = (Button)findViewById(R.id.PlayButton);
gameButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent play = new Intent(getApplicationContext(), com.sample.game.PlayScreen.class);
startActivity(play);
}
});
#Override
protected void onResume() {
// TODO Auto-generated method stub
displayScores.setText("Your Score : "+ Score);
super.onResume();
}

Categories

Resources