Batz Pogi Batz Pogi - 2 months ago 11
Android Question

Android studio Hide and Show buttons from a condition

my android app needs a function that has a button that needs to show and hide based on one

TextView
. Here is my code and ill explain further.

public class currentCar extends Fragment implements AsyncResponse, AdapterView.OnItemClickListener {
final String TAG = this.getClass().getName();
private ArrayList<AcceptCars> carList;
private ListView lvCurrent;
private FunDapter<AcceptCars> adapter;
SharedPreferences pref;

public currentCar() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_current_car, container, false);

ImageLoader.getInstance().init(UILConfig.config(getActivity()));

lvCurrent = (ListView) v.findViewById(R.id.lvCurrent);

pref = getActivity().getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
Log.d(TAG, pref.getString("username",""));

PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getActivity(), this);

taskRead.execute("http://carkila.esy.es/carkila/current.php?acceptCarOwner="+pref.getString("username",""));

return v;
}

@Override
public void processFinish(String s) {
carList = new JsonConverter<AcceptCars>().toArrayList(s, AcceptCars.class);

BindDictionary<AcceptCars> dict = new BindDictionary<AcceptCars>();

dict.addDynamicImageField(R.id.ivImg1, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return item.acceptImage;
}
}, new DynamicImageLoader() {
@Override
public void loadImage(String url, ImageView view) {

ImageLoader.getInstance().displayImage(url, view);
}
});
dict.addStringField(R.id.tvCarModel, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Car Model: " + item.acceptModel;
}
});
dict.addStringField(R.id.tvResDate, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Date: " + item.acceptDate;
}
});
dict.addStringField(R.id.tvResTime, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Time: " + item.acceptTime;
}
});
dict.addStringField(R.id.tvResLocation, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Location: " + item.acceptResLocation;
}
});
dict.addStringField(R.id.tvCarType, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Car Type: " + item.acceptType;
}
});
dict.addStringField(R.id.tvCapacity, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Capacity: " + item.acceptCapacity;
}
});
dict.addStringField(R.id.tvFuelType, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Fuel Type: " + item.acceptFuelType;
}
});
dict.addStringField(R.id.tvPlateNumber, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Plate Number: " + item.acceptPlateNumber;
}
});
dict.addStringField(R.id.tvPoster, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Posted by : " + item.acceptCarOwner;
}
});
dict.addStringField(R.id.tvRenter, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Rented by: "+ item.renters;
}
});
dict.addStringField(R.id.tvDestination, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Destination: " + item.acceptDestination;
}
});

dict.addStringField(R.id.tvResPrice, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Destination: " + item.acceptPrice;
}
});
dict.addStringField(R.id.tvresID, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return ""+ item.acceptedID;
}
});
dict.addStringField(R.id.tvstatus, new StringExtractor<AcceptCars>() {
@Override
public String getStringValue(AcceptCars item, int position) {
return "Status: "+ item.payment;
}
});

adapter = new FunDapter<>(
getActivity(), carList, R.layout.layout_rented, dict);
lvCurrent.setAdapter(adapter);
lvCurrent.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
AcceptCars selectedCars = carList.get(i);
Intent in = new Intent(getActivity(), ownerStatus.class);
in.putExtra("cars", selectedCars);
startActivity(in);
}
}


On the start of this
listView
the
item.payment
is "unpaid" and it will go to
ownerStatus.class


OwnerStatus.class



public class ownerStatus extends AppCompatActivity {
TextView tvStatus, tvPaymentStatus, tvresID, tvCarModel, tvCarType, tvCapacity, tvFuelType, tvPlateNumber, tvResDate, tvResTime, tvResLocation, tvPoster, tvRenter, tvDestination, tvPrice;
ImageView ivImage;
Button btnPaid;
Button btnDone;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_owner_status);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
onButtonClick();

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final AcceptCars Cars = (AcceptCars) getIntent().getSerializableExtra("cars");
ImageLoader.getInstance().init(UILConfig.config(ownerStatus.this));
tvCarModel = (TextView) findViewById(R.id.tvCarModel);
tvCarType = (TextView) findViewById(R.id.tvCarType);
tvCapacity = (TextView) findViewById(R.id.tvCapacity);
tvFuelType = (TextView) findViewById(R.id.tvFuelType);
tvPoster = (TextView) findViewById(R.id.tvPoster);
tvPlateNumber = (TextView) findViewById(R.id.tvPlateNumber);
tvResDate = (TextView) findViewById(R.id.tvResDate);
tvResTime = (TextView) findViewById(R.id.tvResTime);
tvResLocation = (TextView) findViewById(R.id.tvResLocation);
tvPrice = (TextView) findViewById(R.id.tvPrice);
tvDestination = (TextView) findViewById(R.id.tvDestination);
tvRenter = (TextView) findViewById(R.id.tvRenter);
tvresID = (TextView) findViewById(R.id.tvresID);
tvPaymentStatus = (TextView) findViewById(R.id.tvPaymentStatus);
tvStatus = (TextView) findViewById(R.id.tvStatus);
ivImage = (ImageView) findViewById(R.id.ivImg1);

if (Cars != null) {
tvresID.setText("" + Cars.acceptedID);
tvCarModel.setText(Cars.acceptModel);
tvCarType.setText(Cars.acceptType);
tvCapacity.setText(Cars.acceptCapacity);
tvFuelType.setText(Cars.acceptFuelType);
tvPlateNumber.setText(Cars.acceptPlateNumber);
tvResDate.setText(Cars.acceptDate);
tvResTime.setText(Cars.acceptTime);
tvResLocation.setText(Cars.acceptResLocation);
tvPoster.setText(Cars.acceptCarOwner);
tvRenter.setText(Cars.renters);
tvDestination.setText(Cars.acceptDestination);
tvPrice.setText(Cars.acceptPrice);
tvPaymentStatus.setText(Cars.payment);
tvStatus.setText(Cars.payment);

ImageLoader.getInstance().displayImage(Cars.acceptImage, ivImage);
}

}

public void onButtonClick() {
btnPaid = (Button) findViewById(R.id.btnPaid);
btnPaid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder a_paid = new AlertDialog.Builder(ownerStatus.this);
a_paid.setMessage("Does the renter paid the transaction?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
HashMap postData = new HashMap();
postData.put("acceptID", tvresID.getText().toString());
postData.put("payment", "paid");
PostResponseAsyncTask taskPaid = new PostResponseAsyncTask(ownerStatus.this, postData, false, new AsyncResponse() {
@Override
public void processFinish(String s) {
if (s.contains("success")) {
Toast.makeText(ownerStatus.this, "renter is paid", Toast.LENGTH_SHORT).show();
Intent in = new Intent(ownerStatus.this, OwnerTabs.class);
startActivity(in);
finish();
} else {
Toast.makeText(getApplicationContext(), "Failed. Zzzz ☻", Toast.LENGTH_LONG).show();
}
}
});
taskPaid.execute("http://carkila.esy.es/carkila/paid.php");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = a_paid.create();
alert.setTitle("Payment");
alert.show();
}
});
//===================================================================================//
btnDone = (Button) findViewById(R.id.btnDone);
btnDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder a_done = new AlertDialog.Builder(ownerStatus.this);
a_done.setMessage("Is the rent is done?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
HashMap postData2 = new HashMap();
postData2.put("acceptID", tvresID.getText().toString());
postData2.put("status", "done");
PostResponseAsyncTask task1 = new PostResponseAsyncTask(ownerStatus.this, postData2, new AsyncResponse() {
@Override
public void processFinish(String s) {
if (s.contains("success")) {
Toast.makeText(ownerStatus.this, "renter is paid", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Failed. Zzzz ☻", Toast.LENGTH_LONG).show();
}
}
});
task1.execute("http://carkila.esy.es/carkila/done.php");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = a_done.create();
alert.setTitle("Done");
alert.show();
}
});
}
}


And on here. When the
Cars.payment
is unpaid the
btnPaid
will show and hide the
btnDone
but when the
Cars.payment
is paid the
btnPaid
will hide and the
btnDone
will now Hide.
Hope you understand how i explained and help me with my problem. Thanks sir :)

Answer

From your comments, are you saying you need to check:

if(Cars.payment) {
    btnDone.setVisibility(View.GONE);
    btnPaid.setVisibility(View.VISIBLE);
 } else {
    btnPaid.setVisibility(View.GONE);
    btnDone.setVisibility(View.VISIBLE);
 }
Comments