Change button text and action present in custom dialog box - android

I have a custom dialog box which consist of some text and a "next" button. Now, If my application finishes and ends, then the same custom dialog box appears but I need to change the button text with "close" and so the respective action. Here is the code:
private void CustomizedDialog(String text1, String text2) {
final Dialog customDialog = new Dialog(this);
customDialog.getWindow();
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(R.layout.custom_dialog);
TextView firstTextView = (TextView)customDialog.findViewById(R.id.textView1);
firstTextView.setText(text1);
TextView secondTextView = (TextView) customDialog.findViewById(R.id.textView2);
secondTextView.setText(text2);
if (currentInfo == (information.size() - 1)) {
View closeButton = customDialog.findViewById(R.id.answer_next_button);
closeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
customDialog.dismiss();
}
});
} else {
View nextButton = customDialog.findViewById(R.id.answer_next_button);
nextButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("next button", "next button clicked");
customDialog.dismiss();
if (currentInfo < (information.size() - 1)) {
currentInfo++;
nextInformation();
}
}
});
}
customDialog.show();
}
How can i change next button and its associated action in the custom dialog box with the close button and its action when the application reaches its end? Any help would be really appreciated. Thanks :)

Assuming your conditionals are ok, you should access the first condition if it's the last entry, to change the text you simply need to cast the View to a Button and call the .setText(String t) method:
Button closeButton = (Button)customDialog.findViewById(R.id.answer_next_button);
closeButton.setText("Close");
As for the associated action it all seems correct, just add any other actions inside the onClick method of the first condition.

Related

How to get the text value of a button and load it into another button in Android

I am creating a word game application , I have created a grid view consisting of buttons. When I click a button in the grid view a popup windows opens which contains all the English alphabets.Now when I click any letter in my pop up window , I want that letter to appear in my grid that is the string in my pop window's button must appear in my grid view's button. , how do I do it?
This is my button's code:
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View container = layoutInflater.inflate(R.layout.activity_popup,null);
popupWindow = new PopupWindow(container,800,1100,true);
popupWindow.showAtLocation(constraintLayout, Gravity.NO_GRAVITY,150,400);
b1=(Button) findViewById(R.id.b1);
String s1 = b1.getText().toString();
button1.setText(s1);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionevent){
popupWindow.dismiss();
return true;
}
});
}
});
My popup window's code:
b1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String s1 = "A";
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
});
Screenshots of the app
This is my grid layout where the user must enter the letters
When I click any button on the grid this is the pop up window which is displayed.
If I run this , the app is getting stopped.
It might help us if you would share the exact exception that is thrown by your application.
If I unterstand you correctly, then b1 is a button contained in your activity_popup- layout. So you might want to try b1 = (Button) container.findViewById(R.id.b1) instead. If thats not working, post your exception!

Change button text over other button android

I create an application in android studio and I need advice, I got one button, and I need to change the text on the second button clicks through to the first. I have a code that changes only TextView but not the text on the button.
NewText = (TextView)findViewById(R.id.textView1);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
final TextView finalNewText1 = NewText;
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Set Text on button click via this function.
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
Same concept as you did for textView
Button SecondButton,ChangeText; // declaring the buttons
SecondButton = (Button)findViewById(R.id.button2);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//This changes the text on the second button
SecondButton.setText("New Text Here");
}
});
SecondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do anything
}
});
Button ChangeText;
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//part to change the button text
Button tmp_button = (Button)findViewById(R.id.ch_txt_ger);
tmp_button.setText("Frohe Weihnachten");
//part to change the textview text
TextView NewText
NewText = (TextView)findViewById(R.id.textView1);
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
After Clicking outlooking
Here you go: You can define a temporary button variable and make the change on it if setting the same button on its own clicking is causing problems.
And if the text will not change according to user, and if you know it like On/OFF, Red/Green you can also code it with a selector file which would make the java code look more clean.
A tiny advise: Defining the TextViews and Buttons that will get affected should all be written in the same function and close to the place where they are being changed for you to keep track of where you coded them.
I would add one thing, in case if you want to save the new button name when you close and reopen your app, you could use Shared Preferences: https://developer.android.com/training/basics/data-storage/shared-preferences.html

When I clicked Button then Cursor should move to next EditText in diffrent Frame?

I have 4 edittext and 1 button.When i clicked Button then Cursor will go Blank Edittext in next Frame.
Please guide me the correct way to achieve my objective.
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
groupno =(EditText)findViewById(R.id.editText1);
start=(EditText)findViewById(R.id.editText3);
end=(EditText)findViewById(R.id.editText4);
groupno.setEnabled(false);
start.setEnabled(false);
end.setEnabled(false);
}
}
Add this inside the setonlick method.U can use this code to move any edittext.
nextEdittext.requestfocus();
use this code :
use outside button click --
EditText.setFocusableInTouchMode(false);
and on button click press :--
ButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
countryText.setFocusableInTouchMode(true);
countryText.requestFocus();
}
});
Try this on your button click.
View view = getCurrentFocus().focusSearch(View.FOCUS_FORWARD);
int id = view.getId();
if (id != View.NO_ID) {
findViewById(id).requestFocus();

Android: seekbar in button

I have a Button in my android application, and I need to expand it on click and display a Seekbar inside it, like at the picture below. What is the best practice to do that?
Use below code to handle tap of button to set visiblility of seekbar invisible if visible or visible if invisible.
Button click = (Button) findViewById(R.id.but);
Seekbar seek = (Seekbar) findViewById(R.id.seek);
click.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
if (seek.getVisibility() == View.VISIBLE) {
seek.setVisibility(View.INVISIBLE);
} else {
seek.setVisibility(View.VISIBLE);
}
}
});

Button click to change background image of clicked button

I am trying to change the background image of a button that is clicked. The button whose image I am trying to toggle is the same button that is clicked. I ultimately want the program to test the current background image and change it to the other picture given the result of the test.
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (testButton.equals(getResources().getDrawable(R.drawable.fakepicture))) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
}
}//end void onClick
});//end test button on click listener
try
testButton.getBackground().equals(getResources().getDrawable(R.drawable.fakepicture));
However ToggleButton might suit your case better.
As others have said, your equals method is comparing the button itself with the image, but you need to compare the background drawables.
I recommend loading the images drawables you want to use and then using their references later to make things more clear, something like this:
final Drawable first = getResources().getDrawable(
android.R.drawable.arrow_up_float);
final Drawable second = getResources().getDrawable(
android.R.drawable.arrow_down_float);
final Button testButton = (Button) findViewById(R.id.toggleButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (testButton.getBackground().equals(first)) {
testButton.setBackgroundDrawable(second);
} else {
testButton.setBackgroundDrawable(first);
}
}
});
as the other friends answered , it is preferable to use the ToggleButton in Android ,
and in your case, if you want to keep your code , so your method should be like this :
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 )
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (status == 0) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
status=1 ; // change the status to 1 so the at the second clic , the else will be executed
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
status =0;//change the status to 0 so the at the second clic , the if will be executed
}
}//end void onClick
});//end test button on click listener
You can simply use ToggleButton: Android ToggleButton and use StateList for the changing of the background: StateList using the check attribute.
You can use Buttons or Image buttons..
private ImageButton mod1,mod2;
mod1 = (ImageButton) findViewById(R.id.mod1);
mod2 = (ImageButton) findViewById(R.id.mod2);
mod1.setOnClickListener(this);
mod2.setOnClickListener(this);
public void onClick(View v) {
mod1.getDrawable().clearColorFilter();
mod2.getDrawable().clearColorFilter();
switch (v.getId()) {
case R.id.mod1:
mod1.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
case R.id.mod2:
mod2.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
}
}

Categories

Resources