How to change Button app:icon property programitically - java

I have a button with centralized icon+text, and I need to change the app:icon and android:text properties when some event occurred. I know that there is setText() method to change the text, but is there a way to change the icon?
XML:
<Button
android:id="#+id/bottom_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="#dimen/default_margin"
android:layout_marginVertical="#dimen/bottom_bar_content_vertical_margin"
android:backgroundTint="#color/light_green"
android:text="#string/next"
android:textAllCaps="false"
app:icon="#drawable/ic_baseline_next_plan_24"
app:iconGravity="textStart" />
This is a function that is called after appropriate event, and I need to change icon in that function. icon and text are ids of desirable drawable and string:
private void setBottomButton(int icon, int text) {
button.setText(getString(text));
}

You can use the methods:
setIcon
setIconResource
Example:
button.setIcon(drawable)
button.setIconResource(iconId)
Here the doc.

If you check the docs, you'll see the code equivalent for each XML attribute.
Check this link : enter link description here
Searching for drawableLeft shows:
android:drawableLeft:
setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)

Related

No speakable text present at Android Studio

When adding a field for entering a number(Number widget), the error "No speakable text present at Android Studio" takes off
enter image description here
content_main.xml: enter image description here
activity_main.xml: enter image description here
The problem is you are missing content labeling for the view, you should add content description so the user could simply understand what data he should enter into the view
for example, if you want the user to enter the number of cookies he wants you should add a content description as seen below:
android:contentDescription="Enter How Much Cookies You Want"
You should also add an android:hint so the user would have it in front of them an example of the data you want inputted for example:
android:hint="e.g 5"
So your views XML code should look as follows
<EditText
android:id="#+id/editTextNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:minHeight="48dp"
android:contentDescription="Enter How Much Cookies You Want"
android:hint="e.g 8" />
The solution is simple you just need to add the text into the hint part.
search hint in search-bar ant type something in hint block.
and hit Enter.enter image description here
The problem is missing constraints. Any view you add in Constraint layout, you must set the margins otherwise you will get those errors and even if your app managed to run, your edit text will not be place properly.
Add this to your editText;
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Let me know if it worked.
Remember you can twick this to your desired position.

How to achieve the following Image + Text in button in android app?

I am trying to make a clickable phone number button with an icon in my app. I was checking for some references and found google maps implementation good. How can I achieve this in my app?
I have tried the Image Button view but that does not solve the problem. I have put 'onClick' attribute for text & image views, but the button animation isn't there and both text & image icon does not look together.
Please guide me as to what view/s we have to use to achieve the result as in the image and how to get that animation on click of the button. Or is there any better way to achieve this?
I am aware of intents, so that part is clear.
If you can let me know how to make that phone number copied to the clipboard automatically on hold of that button, that would be really great.
Try this code
Change spacing dimens according to your use and also change icon.
<LinearLayout
android:id="#+id/callButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/spacing_small"
android:padding="#dimen/spacing_small"
android:clickable="false"
app:srcCompat="#drawable/ic_download"
android:tint="#color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/amaranth"
android:gravity="center_vertical"
android:paddingLeft="#dimen/spacing_xxhuge"
android:paddingTop="#dimen/spacing_medium"
android:paddingBottom="#dimen/spacing_medium"
android:text="000 0000 000"
android:textColor="#color/grey_70"
android:textSize="#dimen/textsize_large" />
</LinearLayout>
and set on click listener on callButton. use below code in java code.
And also i have added a code to copy phone number directly on click event. You have to save text in clipboard.
LinearLayout callButton = findViewById("callButton");
callButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
// You have to get text from phoneNumber textview. and set it to clipboard.
clipboard.setPrimaryClip(clip);
}
});
Some questions:
Do you have any experience with Android development?
If so, do you have anything up and running?
I'm gonna assume you do have experience but you're asking before you start coding anything. There are many ways to implement this, the way that would be easiest would be to have a custom listview (here's a simple and easy tutorial for that) and use an item in the listview to display a phone number. Each listview item has a setOnItemLongClickListener which you can use and inside it use the ClipboardManager to copy or use an intent to the phone calling service.
list.setOnItemLongClickListener(new OnItemLongClickListener() { //list is my listView
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int pos, long id) {
//Whatever you wanna do
ClipboardManager clipboard = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
}
});
I believe this is what you want to achieve
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Your other layouts-->
<TextView
android:background="?selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:padding="#dimen/activity_vertical_margin"
android:drawableStart="#drawable/ic_phone"
android:drawablePadding="16dp"
android:text="The mobile number here"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
android:background="?selectableItemBackground"
This will add the default system animation of ripple(or anything) on click.
Also android:focusable=" and android:clickable="true" is necessary for this to work.
If you want to customize the click events, you better be using selectors in the background of your view.
For the 'Copy to Clipboard' feature you can refer to the other answers.
Happy Coding!

How to display hint of EditText AFTER a default value on the EditText? (Android Java)

I would like the hint to display after a default value I put in the EditText.
For example, the contents of the EditText must look like:
192.168.1.1 (Hint: First IP address) - all inside the EditText, and when the user clicks it, it defaults to show only 192.168.1.1.
The EditText takes a string as input. I want the hint to just be there, but not count as the input string.
Please help.
Thanks
Regards
Ok, I got your point. So you want a floating label when an edit text receive a focus. You just have to wrap your edit text inside TextInputLayout.
Here is my updated sample:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_id"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edit_txt_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/str_reource_id"
android:inputType="depends_on_your_use_case"/>
</android.support.design.widget.TextInputLayout>
Define ip_hint and ip you want in resources then in your onCreate:
editText.setText(R.string.ip_hint);
editTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(final View view, final boolean hasFocus) {
if (hasFocus)
editTxt.setHint(R.string.ip);
else if(editText.getText().equals(context.getResource(R.string.ip))
editText.setText(R.string.ip_hint);
}
});
There is no way to achieve this task. Use hint as input string and append with input string.
Late to the show, but you can also add a formatted string in your hint in onCreate,
<EditText
android:id="#+id/foo"
android:hint="#string/hint_foo"
...
where hint_foo is defined in string resource,
<string name="hint_foo">Foo: %1$s</string>
and then initialise with getString() in activity,
TextView hinter = (TextView) findViewById(R.id.foo);
hinter.setHint(getString(R.string.hint_foo, ip_string));

Android button background color changes button size

I am using a built-in theme for my Android app:
<style name="AppTheme" parent="android:Theme.Black">
<!-- Customize your theme here. -->
</style>
I am happy with that theme, except I want to change the background color of a button. Here is how it looks by default:
Here's what happens when I add a background color to this button (android:background="#color/play_bg"):
Hey!? It basically changed all the button's size, padding and margins!
So I managed to get the expected result using the backgroundTint property (android:backgroundTint="#color/play_bg"):
Unfortunately, this is only supported since version 21 of the API, which is not acceptable for me.
So two questions:
Why does changing the background messes with the rest of the button's properties?
How do I get the expected result without backgroundTint?
And a bonus question: How can I get the expected result programmatically (I have dynamic buttons in my app, so this would be very useful)?
You can change this color in your Java File. When your main class loads you can take an object of this button and then change color.
Here is how you define this button in Manifest file :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY"
android:id="#+id/btn1"
... />
Now in your Java file when you are adding this XML layout you need to
Button b = (Button)findViewByID(R.id.btn1);
b.getBackground().setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY);
You may also use COLOR:
COLOR.RED
The code below sometimes does not work for me :-
b.setBackgroundColor(int color)
In my case I will be doing in this process
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#color/play_as"
android:padding="8dp"
android:text="Button" />
Or you can use this link which is more easy way of creating the buttons

ellipsize disable initial text

I have a strange behavior with the use of singleLine=true and ellipsize=start with a Button.
First of all, the declaration of my button :
<Button
android:id="#+id/enterDeparture"
android:layout_width="175dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:background="#drawable/field_button"
android:text="#string/research_enterDeparture"
android:textColor="#drawable/field_button_textcolor"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:ellipsize="start"
android:singleLine="true" />
With this declaration, no text is displayed inside the button. But if I write Log.d(TAG, "the text is : " + findViewById(R.id.enterDeparture));, the LogCat gives me the correct value...
I tried to set the text programatically, either in the onCreateView() and in the onResume() methods : same behavior. But if I set the text really later of if I put an AlertDialog over my screen, the content come back immediatly...
To finish, if I remove the two lines android:ellipsize="start" and android:singleLine="true", everything is normal : my text is displayed in the first time.
EDIT
I tried to remove the singleLine=true line : the initial content is actually displayed but the ellipsize behavior doesn't work anymore...
So I tried with maxLines=1 : the initial content is displayed, but the "..." are not shown anymore (the content is just truncated).
Try changing the height value:
android:layout_height="wrap_content"
You could be elipsising your text to ... and because the button padding it could be being hidden.
Also you want to try this instead:
Log.d(TAG, "the text is : " + ((Button)findViewById(R.id.enterDeparture)).getText())

Categories