My question is I am trying to show a car park booking system in my app. I want to show all the free spaces in green and when user book space the same area should turn to red. Below is the image:
I have tried so many ways but did not get any success. Below is my code:
public class CarParkingActivity extends AppCompatActivity {
private ImageMap mImageMap;
private RelativeLayout stage;
private RelativeLayout.LayoutParams lparams;
private SparseArray<ImageMap.Bubble> spaces;
private String title;
Tracker mytracker;
private List<String> blocks; //list of 'occupied' spaces
@Override
protected void onCreate(Bundle savedInstanceCarParking) {
super.onCreate(savedInstanceCarParking);
setContentView(R.layout.activity_car_parking);
blocks = new ArrayList<>();
blocks.add("p1");
blocks.add("p3");
blocks.add("p6");
blocks.add("p13");
blocks.add("p9");
blocks.add("p200");
// find the image map in the view
mImageMap = (ImageMap) findViewById(R.id.map);
stage = (RelativeLayout) findViewById(R.id.stage);
mImageMap.setImageResource(R.drawable.carpark2);
title = getIntent().getStringExtra("option");
getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// add a click handler to react when areas are tapped
mImageMap.addOnImageMapClickedHandler(new ImageMap.OnImageMapClickedHandler() {
@Override
public void onImageMapClicked(int id, ImageMap imageMap) {
mImageMap.showBubble(id);
if (blocks.contains(imageMap.getArea(id).getName())) {
MySingleton.getInstance().showToast(getApplicationContext(), "Sorry, this car space is occupied");
spaces = mImageMap.getParkings();
drawParkSpace(spaces.get(id)._x, spaces.get(id)._y, false);
} else {
//open booking view
Intent it = new Intent(getApplicationContext(), BookingCarParkingActivity.class);
it.putExtra("parkId", imageMap.getArea(id).getName().toUpperCase());
startActivity(it);
}
}
@Override
public void onBubbleClicked(int id) {
Log.i("app", "onBubbleClicked: id: " + id);
}
});
}
/**
* Draw red or green rect on the view.
*
* @param x
* @param y
* @param isOk: true equals green
*/
private void drawParkSpace(float x, float y, Boolean isOk) {
Drawable imageView = null;
imageView = ContextCompat.getDrawable(CarParkingActivity.this, isOk ? R.drawable.carok : R.drawable.carnook);
lparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView imageView1 = new ImageView(CarParkingActivity.this);
imageView1.setImageDrawable(imageView);
imageView1.setLayoutParams(lparams);
imageView1.setX(x);
imageView1.setY(y);
stage.addView(imageView1);
}
@Override
protected void onResume() {
super.onResume();
mImageMap.loadMap("menu"); //load menu mapped image
//List<ImageMap.PolyArea> list = mImageMap.getPolys();
//ImageMap.PolyArea poly = list.get(0);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return false;
}
}
Right now each car is clickable but I need to change the color like shown in the image and when the user book space the same area should need to turn red. Please help me to sort out this task. I am new in android but trying hard to solve this issue. Please accept my apologies if my question is not strong enough. Also many thanks in advance. this is the second activity when i booked the space
For your need, you have to keep two list and two paint object to track booked and not booked space.
-list of available space.
-list of booked space.
-booked space paint
-yet to book space paint
then in your Bubble class onDraw method draw paint based on the booked status
change initDrawingTools as follows
Hope this will help you to proceed further.