Android. SetText doesn't change the text for Button - android

I have custom view - custom keyboard.
Each button is MaterialButton.
I need to implement shift button to change letters case, so I've done it this way
private fun onCaseChanged() {
buttons.forEach { button ->
(button as? TextView)?.let {
val text = it.text.toString()
it.text = if (isUpperCase)
text.toUpperCase(Locale.getDefault())
else
text.toLowerCase(Locale.getDefault())
}
}
}
All is working correct except UI - text on buttons doesn't change.
When I change buttons to TextViews - all works fine. But not with any type of Button.
So for now it seems like only way for me.
Bu I'm still curious - if there's another way to fix this problem with leaving MaterialButtons

Try android:textAllCaps="false" in the Button declaration in your XML files.
If that successfully works, I suggest changing the default button style in your theme so that it defaults to false if that's what you need.
Example
<Button
android:id="#+id/button"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>

Related

Android, onClick of Button fires before completing the built in animation

I'm new to android development. Developing a flipflop game.
On click button changes the colour and then it checks if the two turned buttons match or not.
I haven't added any animation below is my on click code.
fun onButtonClick(view: View) {
val id = getResources().getResourceEntryName(view.id)
val btnItem = getButtonItem(id)
view.setBackgroundColor(Color.parseColor(btnItem.color))
btnItem.isTurned = true
btnItem.resId = view.id
checkMatch()
}
and here is what each button looks like
<Button
android:onClick="onButtonClick"
android:id="#+id/btn02"
android:layout_margin="5dp"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight=".50"/>
The problem is, when I click the button, the colour is changed after finishing the onclick execution, not sure if it's setBackgroundColor which is taking time or the onClick itself delays this. Even If i put a Thread.sleep right after setBackgroundColor it will sleep and don't even change the colour.
In checkMatch i want to match the turned cards and if they don't match i set the background colour back. but this all completes before even showing the hidden colour.
What should I do to complete animation and set background colour and then proceed further ?
I hope this makes sense.

Making Button text partially bold doesn't work

I want to make text inside Button partially bold but this doesn't work.
I have my text placed in string values. String styling is completely ignored and text appeared as "normal" style inside button.
<string name="gps_yes_button_text"><b>Yes, </b>confirm this location!</string>
Button:
<Button
android:id="#+id/gps_confirm_button"
android:layout_width="#dimen/gps_button_width"
android:layout_height="#dimen/gps_button_height"
android:layout_gravity="center"
android:elevation="#dimen/gps_button_elevation"
android:text="#string/gps_yes_button_text"
android:textColor="#color/colorText"
android:textSize="#dimen/gps_text_large"
android:textAllCaps="false"
android:background="#drawable/gps_rounded_button"/>
Please try to set the button text by coding as below.
String buttonText = "<b>Yes, </b>confirm this location!";
button.setText(Html.fromHtml(buttonText));

Changing button color causes the button to get bigger

Here is what the button looks like after changing the color
Here is what it looks like with "show layout bounds" on
I have an "add note" button which shows a dialog for the user to enter a note.
I want to change the color of the button if a note is saved.
I've tried this:
btnNote.setBackgroundColor(view.getContext().getResources().getColor(R.color.NN));
and this:
btnNote.getBackground().setColorFilter(ContextCompat.getColor(view.getContext(), R.color.NN), PorterDuff.Mode.MULTIPLY);
But in both cases the button also becomes slightly bigger.
How can I change only the color of the button?
this is my button from my layout file:
<Button
android:id="#+id/btnNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Add note"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/btnDelete"
app:layout_constraintTop_toTopOf="parent" />
I have tried all options and finally its working properly when, I Use android:backgroundTint attribute in your XML Button View.
Show below Snippet:-
android:backgroundTint="#6567dc"
Problematically
If in your gradle file minSdkVersion is below 21 than you should change it to 21 OR you can wrap code in to if condition that check device SDK support as below
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
btnAccept.setBackgroundTintList(ContextCompat.getColorStateList(this, R.color.yourcolor));
}
using above code output:
After color changed
I think it dosen't become bigger,you can open the switch named "show layout bound" in developer option,the button's layout has not been changed,you can custom an drawable as the background.
Instead of using background , try backgroundTint
In xml,
android:backgroundTint="#yourcolor"
in java,
setBackgroundTintList(ColorStateList list)
For AppCompatButton ,
button.setSupportBackgroundTintList(ContextCompat.getColorStateList(this, R.color.yourcolor));
You can use ,
ViewCompat.setBackgroundTintList(AppCompatButton, ColorStateList)
You can use background and don't use colour instead use drawable with different colours as you are using shape as background and setting colour to it does changes the colour but shape is not maintained so you need to add drawable shape with the colour you want to change

Change button android:background to different drawable

I am new to Android and Java but have managed to teach myself and find most answers to questions on stackoverflow without needing to ask questions. Until now....
Here goes, I have many colored buttons which, when clicked, change color to a range of different colors.
There are many buttons defined for example as:
<Button
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/orange_button"
android:gravity="center"
android:onClick="onClick" />
Could someone please advise me how to change the android:background using code to change the above example to yellow, for example, when the button is clicked.
In the code below clickedButton is the ID of the button for which I need to change the background.
public void onClick(View v) {
int id=v.getId();
String clickedButton = getResources().getResourceEntryName(id);
Change button to Yellow here??
// Temporary code below to check which button was pressed
// and convert its number to an integer to eventually access an array
final TextView tvGameTimer = (TextView) findViewById(R.id.tvGameTimer);
int buttonNumber = Integer.parseInt(clickedButton.substring(6,8));
tvGameTimer.setText("" + buttonNumber);
}
I am using custom button styles to define the button colors:
res/drawable/yellow_button.xml
res/drawable/blue_button.xml
res/drawable/red_button.xml
res/drawable/orange_button.xml
res/drawable/green_button.xml
For now I just need to work out how to change the button from Orange to Yellow. I can then add the logic to change the colors as and when the app requires.
Many thanks for any help.
I am supposing that the onClick method you post is of the same Button whose background you are trying to change. Use this code.
v.setBackgroundResource(R.drawable.yellow_button);
If onClick is not the method of the same button then, use
findViewById(R.id.button17).setBackgroundResource(R.drawable.yellow_button);
Button btn = (Button) findViewById(R.id.button17);
btn.setBackground(this.getResources().getDrawable(R.drawable.yellow_button));
Try this.

Show/Hidden Soft Keyboard event in Android

I need to get the Show/Hidden event from the SoftKeyboard in Android.
I did a research, but nothing worked.
I wanted that 'cause we're working with a tablet 5.0'' with low resolution, so when you edit a EditText, the keyboard rise in full screen, and then or you press the "enter or next" key, or you press the back button to hide the keyboard... I need to update some fields with the new value, but I can't use the TextWatcher 'cause have some business logics on what's the right fields to update, and 'cause I just want to update when the user really finish the input.
And the onFocusChanged isn't a option, 'cause we don't want our customers needing to cliking in the next field, and hiding the keyboard to see the new values.
I don't want to override the onTouchEvent too, see if where the user cliked isn't the same field that he is editing.
Sorry for the specific problem and more specific solution that I'm asking for.
And sorry for my bad English :D
Use tree view observer to detect any change in view height and that time you can check whether keyboard state
final View activityRootView = findViewById(R.id.chat_pane_root_view);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if(mgr.isAcceptingText())
{
//keyboard is up
}
}
});
First of all in your declairation of your activity in manifest file declaire like this
<activity android:name="Map" android:windowSoftInputMode="stateHidden">
from this property your keyboard is being hidden,
after this you are declaire the imeOption property in your main.xml file edittext like this
<EditText
android:id="#+id/edttexttitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:hint="#string/tasktitle"
android:imeOptions="actionNext"
android:inputType="text"
android:textSize="#dimen/font_size_medium"
android:textColor="#color/darkgray1" />
this code is for your first edittext if more then one and if only one edittext then your need to change the imeOption of edittext like
android:imeOptions="actionGo"

Categories

Resources