status bar goes black while showing bottom sheet dialog - java

how do I prevention the status bar from loosing it's color while a bottom sheet is showing up?
I use a bottom sheet view with custom layout.
tried modify the style of the dialog but still no luck
here is my code
if (position == 1) {
View modalbottomsheet = getActivity().getLayoutInflater().inflate(R.layout.others_sheet, null);
dialog = new BottomSheetDialog(getActivity());
dialog.setContentView(modalbottomsheet);
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
dialog.show();
contact_num = (EditText) modalbottomsheet.findViewById(editText);
choose_contact = (Button) modalbottomsheet.findViewById(choose);
choose_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == choose_contact.getId()) {
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE);
}
}
});
hidetext = (EditText) modalbottomsheet.findViewById(R.id.hidetext);
recharge_btn = (Button) modalbottomsheet.findViewById(R.id.rechargeothers_btn);
recharge_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId()== recharge_btn.getId()){
String phone = ((EditText) dialog.findViewById(R.id.editText)).getText().toString();
String hidden = ((EditText) dialog.findViewById(R.id.hidetext)).getText().toString();
String pre = "*805*2";
String hash = "%23";
String dail = "tel:" + pre + phone + hidden + hash;
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dail)));
dialog.hide();
}
}
});
}

Related

Android Studio, pass to next page if one out of two text fields are filled

So I basically want to know how would I allow the user to proceed to the next page when they hit pay if either txtCode or StreetCode is filled, only one of these two needs to be filled in order to pass to the next page but how would i get that to work as currently they both must filled to pass to the next page.
btnPay = (Button)findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
/** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
txtReg = (EditText)findViewById(R.id.txtReg);
txtCode = (EditText)findViewById(R.id.txtCode);
txtStreetName = (EditText)findViewById(R.id.txtStreetName);
dlCostTime = (Spinner)findViewById(R.id.dlCostTime);
if( txtReg.getText().toString().trim().equals(""))
{
txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
txtStreetName.setError("required!");
}
else{
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
}
Replace this code
if( txtReg.getText().toString().trim().equals(""))
{
txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
txtStreetName.setError("required!");
}
else{
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
}
with this
if( txtReg.getText().toString().trim().equals("")){
txtReg.setError("required!");
}else{
if((txtCode.getText().toString().trim().equals("") && !txtStreetName.getText().toString().trim().equals("")) ||
(!txtCode.getText().toString().trim().equals("") && txtStreetName.getText().toString().trim().equals(""))){
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}else{
//show message enter either code or street name not both
}
Replace this code
btnPay = (Button) findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
/** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
txtReg = (EditText) findViewById(R.id.txtReg);
txtCode = (EditText) findViewById(R.id.txtCode);
txtStreetName = (EditText) findViewById(R.id.txtStreetName);
dlCostTime = (Spinner) findViewById(R.id.dlCostTime);
if (txtCode.getText().toString().trim().length() > 0 || txtStreetName.getText().toString().trim().length() > 0) {
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
} else if (txtStreetName.getText().toString().trim().length() == 0) {
txtStreetName.setError("required!");
} else if (txtReg.getText().toString().trim().length() == 0) {
txtReg.setError("required!");
} else if (txtCode.getText().toString().trim().length() == 0) {
txtCode.setError("required!");
}
}
}

Value in editText doesn't change when save button is clicked

I have two widget in Activity A, one is textView and another is button. Both will be used to access Activity B.
And in Activity B, I have one editText, two textView , one save button and one image button.
If using button in A , Flow: A>>B>>C>>return value and image from C to B then A.
If using textView in A, Flow: pass value and image from A to B , for edit, then click save button to return the edit value and image to A.
Activity B
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
txt = (EditText) findViewById(R.id.editText36);
txt1=(TextView)findViewById(R.id.textView57);
Button b = (Button) findViewById(R.id.button17);
addListenerOnButton();
viewImage = (ImageView) findViewById(R.id.imageView2);
if(getIntent().getBooleanExtra("bitmap",false)) //if textField in A clicked
{
viewImage.setImageBitmap(Global.img);
}
else
{
viewImage.setImageBitmap(null);
}
if(getIntent().getExtras()!=null) { // if have value pass from A
final String Amount = getIntent().getExtras().getString("result");
final String description1 = getIntent().getExtras().getString("description");
txt1.setText(description1);
txt.setText(Amount);
}
b.setOnClickListener(new View.OnClickListener() { // Back to A
public void onClick(View arg0) {
Intent returnIntent = new Intent();
a = "Project";
text = txt.getText().toString(); // amount
returnIntent.putExtra("text", text);
returnIntent.putExtra("a", a);
final int k1 = getIntent().getExtras().getInt("k");
returnIntent.putExtra("k1", k1);
returnIntent.putExtra("c",c);
// returnIntent.putExtra("image",Global.img);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
}
public void onActivityResult(int requestCode,int resultCode, Intent data)
{ //receive value and image from C
if(requestCode==PROJECT_REQUEST_CODE) {
if(data!=null&&data.hasExtra("text")) {
c = data.getStringExtra("text");
txt1.setText(c);
viewImage.setImageBitmap(Global.img);
}
}
else if (requestCode==CAMERA_REQUEST_CODE)
{
}
}
Activity A
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { //receive data from B
int button = data.getIntExtra("k1", 0);
if (button == 1) {
switch (requestCode) {
case 0:
result = data.getStringExtra("text");
name = data.getStringExtra("a");
description=data.getStringExtra("c");
if (Global.img != null) {
v.setImageBitmap(Global.img);
}
as=Long.parseLong(result);
c.setText(" " + name + "------" + "RM " + result);
break;
}
c.setOnClickListener(new View.OnClickListener() { // if c is not null, it can intent to B.Otherwise it is not clickable
#Override
public void onClick(View view) {
if ((name != null && name.trim().length() > 0) && (result != null && result.trim().length() > 0)) {
Toast.makeText(getActivity().getApplicationContext(), "not null", Toast.LENGTH_LONG).show();
if (name.equals("Project")) {
Intent intent = new Intent(getActivity(), Project1.class);
Global.img = null;
v.setDrawingCacheEnabled(true);
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
intent.putExtra("name", name);
intent.putExtra("result", result);
intent.putExtra("description", description);
if (v.getDrawingCache() != null) {
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
if (bitmap == null) {
Log.e("TAG", "getDrawingCache() == null");
}
Global.img = bitmap;
startActivity(intent);
}
}
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getActivity().getApplicationContext(), fk + "", Toast.LENGTH_LONG).show();
AlertDialogRadio(a1);
}
});
public void AlertDialogRadio(final int k) {
final CharSequence[] ClaimsModel = {"B", "Petrol", "Car Maintenance"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select a Claims");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), B.class);
intent.putExtra("k",k);
startActivityForResult(intent, 0);
}else{...}
}
Problem: When the textView is clicked and intent to B, the save button doesn't return the changed value and image to A. The value and image not changing.
It looks like there is some caching because of which the old state is retained and not updated. I suggest you try activity.recreate() function which forces the activity to create a new instance.
Activity.Recreate()

Android button text color does not change

I'm working on a quiz app that has a question and to answer options. When the wrong answer (button) is clicked, the text color for that specific button turns red, as it should. However, when the right button is clicked, the text color for that particular button should turn green, wait a second, and then move on to the next question.
I have spent days trying to figure out why it doesn't work, googled for answers, but I still can't make it work.
I have tried using Handlers and Runnables, but it still doesn't work.
I have posted the full code for the activity below.
Please help!
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.question_layout);
Intent i = getIntent();
subject = (Subject) i.getSerializableExtra(Globals.SUBJECT);
questions = subject.getQuestions();
subjectId = subject.getSubjectId();
appPreferences = new AppPreferences();
livesLeft = 3;
index = 0;
maxQuestions = questions.size();
maxIndex = maxQuestions - 1;
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(maxQuestions);
answer1 = (Button) findViewById(R.id.buttonAnswer1);
answer2 = (Button) findViewById(R.id.buttonAnswer2);
setQuestionsAndAnswers(index);
TextView textView = (TextView) findViewById(R.id.textViewSubjectName);
textView.setText(subject.getSubjectName());
}
private void setQuestionsAndAnswers(int index)
{
currentQuestion = questions.get(index);
// Set question
TextView textViewQuestion = (TextView) findViewById(R.id.textViewQuestion);
textViewQuestion.setText(currentQuestion.getQuestion());
// Set correct answer
correctAnswer = currentQuestion.getCorrectAnswer();
sourceUrl = currentQuestion.getSourceUrl();
sourceText = currentQuestion.getSourceText();
// Set answer #1
initializeButton(answer1, currentQuestion.getAnswer1());
// Set answer #2
initializeButton(answer2, currentQuestion.getAnswer2());
// Set source
TextView textViewSource = (TextView) findViewById(R.id.textViewSource);
textViewSource.setText(sourceText);
textViewSource.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
textViewSource.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(sourceUrl));
startActivity(browserIntent);
}
});
// Update progress
updateProgress(index);
}
private void initializeButton(Button button, String answer)
{
button.setText(answer);
button.setTextColor(Color.WHITE);
button.setBackgroundColor(Color.TRANSPARENT);
button.setEnabled(true);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
onClickContent(view);
}
});
}
private void onClickContent(View view)
{
Context context = getApplicationContext();
final Button button = (Button) view;
String answer = button.getText().toString();
if (answer.equalsIgnoreCase(correctAnswer))
{
button.setTextColor(Color.GREEN);
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if (index == maxIndex)
{
appPreferences.editPreferences(context, Globals.PREFERENCE_KEY + subjectId, true);
playSound(Globals.SOUND_QUIZ_COMPLETED);
Toast toast = Toast.makeText(context, "Quiz \"" + subject.getSubjectName() + "\" finished",Toast.LENGTH_LONG);
toast.show();
finish();
} else
{
playSound(Globals.SOUND_CORRECT_ANSER);
// Go to next question
index++;
setQuestionsAndAnswers(index);
}
} else
{
if (livesLeft == 1)
{
playSound(Globals.SOUND_GAME_OVER);
Toast toast = Toast.makeText(context, "Game over", Toast.LENGTH_LONG);
toast.show();
finish();
} else
{
playSound(Globals.SOUND_WRONG_ANSWER);
button.setTextColor(Color.RED);
button.setEnabled(false);
livesLeft--;
TextView lives = (TextView) findViewById(R.id.textViewLives);
lives.setText("Lives: " + livesLeft);
}
}
}
private void updateProgress(int progress)
{
progressBar.setProgress(progress++);
}
private void playSound(int songId)
{
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), songId);
mp.start();
}
Firstly, Thread.sleep(1000); in your case runs on MAIN thread, causes MAIN thread to sleep, it's not suggested.
You may view.postDelayed to load next question after a delay,
private void onClickContent(View view)
{
Context context = getApplicationContext();
final Button button = (Button) view;
String answer = button.getText().toString();
if (answer.equalsIgnoreCase(correctAnswer))
{
button.setTextColor(Color.GREEN);
if (index == maxIndex)
{
appPreferences.editPreferences(context, Globals.PREFERENCE_KEY + subjectId, true);
playSound(Globals.SOUND_QUIZ_COMPLETED);
Toast toast = Toast.makeText(context, "Quiz \"" + subject.getSubjectName() + "\" finished",Toast.LENGTH_LONG);
toast.show();
finish();
} else
{
playSound(Globals.SOUND_CORRECT_ANSER);
// Go to next question
index++;
// Go to next question after 1000ms
view.postDelayed(new Runnable() {
public void run() {
setQuestionsAndAnswers(index);
}
}, 1000); //here delays 1000ms
}
}
...
}
This is what I use for the same purpose. It should work...
button.setTextColor(Color.GREEN);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Reset Color if you want to...
// Start new question here
}
}, 1000);

Passing dynamic string to activity

I create an activity with dynamic buttons in a loop. I get a list and create a button for each element in the list. The buttons go to the same activity afterward, but with each button I want to pass different string.
I did this in the loop:
tour_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TourListActivity.this,
TourMenuActivity.class);
String info = tour.toString();
intent.putExtra(TOUR_INFO, info);
startActivity(intent);
}
});
But at the end, all the buttons get the same string (the string of the last button).
========================================
full code:
try {
JsonObject respObject = jsonParser.parse(response).getAsJsonObject();
JsonArray tourListArray = respObject.getAsJsonArray("tours");
System.out.println("tourListArray: " + tourListArray.toString());
for(int i = 0; i < tourListArray.size(); i++){
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
tour = tourListArray.get(i).getAsJsonObject();
String tourCode = tour.get("tourcode").getAsString();
Button tour_button = new Button(this);
tour_button.setText("Tour Code: " + tourCode);
tour_button.setGravity(Gravity.LEFT);
tour_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TourListActivity.this,
TourMenuActivity.class);
String info = tour.toString();
intent.putExtra(TOUR_INFO, info);
startActivity(intent);
}
});
ll.addView(tour_button);
LinearLayout yourLL = (LinearLayout) findViewById(R.id.Tours_List);
yourLL.setOrientation(LinearLayout.VERTICAL);
yourLL.addView(ll);
}
} catch (JsonIOException e) {
e.printStackTrace();
}
When you create the button you can:
button.setTag(someString);
and then in the onClick you can:
public void onClick(View v) {
Intent intent = new Intent(TourListActivity.this,
TourMenuActivity.class);
String info = tour.toString();
intent.putExtra(TOUR_INFO, ((Button)v).getTag());
startActivity(intent);
}
The variable tour is defined outside the loop, so each button share the same variable.
At each iteration, you just change the reference stored by this variable.
You could to create a final variable inside your loop, and use it inside the OnClickListener:
for (int i = 0; i < tourListArray.size(); i++) {
...
final String tourInfo = tour.info;
tour_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(
TourListActivity.this,
TourMenuActivity.class
);
intent.putExtra(TOUR_INFO, tourInfo);
startActivity(intent);
}
});
...
}

Which method is called when user presses back button if a dialog is shown?

I defined a dialog the following way:
public void displayAvailableDevices(Vector<UserDevice> availableDevices) {
connector.setDevicesFound(true);
UserDevice[] devices = new UserDevice[availableDevices.size()];
for (int i = 0; i < availableDevices.size(); i++) {
devices[i] = availableDevices.elementAt(i);
}
Arrays.sort(devices);
Vector<UserDevice> sorted = new Vector<UserDevice>();
for (int j = 0; j < devices.length; j++) {
sorted.add(devices[j]);
}
allAvailableDevices = sorted;
connector.stopNeighbourDiscovery();
if (dialog != null) {
dialog.dismiss();
checkedUser = null;
}
dialog = new Dialog(this);
dialog.setContentView(R.layout.available_users);
dialog.setTitle("Available Devices");
RelativeLayout rootLayer = (RelativeLayout) dialog.findViewById(R.id.dialogRoot);
rootLayer.setBackgroundColor(Color.WHITE);
ListView userTable = (ListView) dialog.findViewById(R.id.userList);
adapter = new UserAdapter();
userTable.setAdapter(adapter);
userTable.setOnItemClickListener(new UserListener());
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
connect = (Button) dialog.findViewById(R.id.userList_connect);
connect.setMinWidth(dm.widthPixels / 2);
connect.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (checkedUser != null) {
dialog.dismiss();
connector.connectToDevice(checkedUser);
}
}
});
Button start = (Button) dialog.findViewById(R.id.userList_start);
start.setMinWidth(dm.widthPixels / 2);
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
startProfileView();
}
});
Button refresh = (Button) dialog.findViewById(R.id.userList_refresh);
refresh.setMinWidth(dm.widthPixels / 2);
refresh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
getGameBaseLayer().resetAvailableUserDevices();
connector.refresh();
}
});
Button cancel = (Button) dialog.findViewById(R.id.userList_cancel);
cancel.setMinWidth(dm.widthPixels / 2);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
dialog.show();
if (checkedUser == null) {
connect.setEnabled(false);
}
}
If this dialog is displayed and the user presses the back button, the dialog is dismissed. However I would like to do some further processing, but how can i detect if the user has pressed the back button?
I'm quite positive that this article can help a lot. As far as I understand, pressing 'BACK' is like cancelling the dialog (like pressing 'CANCEL'). They show how to make a dialog uncancelable and how to implement a listener to catch the 'cancel' events.

Categories

Resources