How to display Edittext in Android [duplicate] - android

This question already has answers here:
How to return to default style on EditText if I apply a background?
(2 answers)
Closed 5 years ago.
I have an EditText that looks like this:
When doing an action in the EditText the background is changed with the following code:
edtCampo.setBackgroundColor(Color.RED);
Then another action is made and the EditText background is changed again with the following code:
edtCampo.setBackgroundColor(Color.TRANSPARENT);
The problem is that in the EditText I do not see the line below, what property should I give to make it look like the first image ?

Try to use
int backgroundResource = 0;
if(redBackgroundNeeded){
backgroundResource = R.color.red;
}
view.setBackgroundResource(backgroundResource);

Try to use,
android:backgroundTint="#color/red"
To achieve pro-grammatically,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
edt1.setBackgroundTintList(getResources().getColorStateList(R.color.red));
}
Result,

Related

How to create a new EditText that is placed in the RecyclerView when Button is Pressed? (Kotlin) [duplicate]

This question already has answers here:
How to update RecyclerView Adapter Data
(16 answers)
Closed 1 year ago.
I have a button that when pressed, is supposed to create a new editText inside a recyclerView. And this should be repeatable until a set limit. Ex. 5 EditTexts.
I have tried this code below, but when the button is pressed, the app just crashes.
// add is the btn id
add.setOnClickListener {
val newText = EditText(this)
//recyclerView is Recycler View in activity_main
recyclerView.addView(newText)
}
How could I go about this?
You only need update the data and refresh the adapter.
Review this post.
Update RecyclerView

setActionView deprecated [duplicate]

This question already has an answer here:
How to cast MenuItem to LinearLayout
(1 answer)
Closed 3 years ago.
Bellow code works fine but it gives me setActionView and getActionView is deprecated
val item = menu?.findItem(R.id.action_mini_basket)
MenuItemCompat.setActionView(item,R.layout.toolbar_mini_basket_layout)
val toolbarLayout = MenuItemCompat.getActionView(item)
android developer site says " This method was deprecated in API level 26.1.0. Use setActionView(int) directly. "
I managed to fix getActionView but have no idea about setActionView it only takes one argument.
val item = menu?.findItem(R.id.action_mini_basket)
MenuItemCompat.setActionView(item,R.layout.toolbar_mini_basket_layout) //??
val toolbarLayout = item.actionView as RelativeLayout
thanks
change
MenuItemCompat.setActionView(item,R.layout.toolbar_mini_basket_layout);
to
item.setActionView(R.layout.toolbar_mini_basket_layout);
Compat version isn't needed anymore if you are using AppCompatActivity
According to the Android documentation you indeed need to use setActionView(int).
In this case the parameter is the layout resource you want to use.
So it will be:
MenuItemCompat.setActionView(R.layout.toolbar_mini_basket_layout)

how can I change the text from several TextViews dynamically? [duplicate]

This question already has answers here:
TextViews text change in a for loop
(4 answers)
Closed 4 years ago.
I need to set the text of some textViews already existing in a layout with a for loop. Eg. TextView_01, TextView_02, etc. IS there a way to do something like the following speculative code:
for(1 in 0..6){
TextView_0(change value with i).text = something
}
This isn't the best way to do things, but it's probably the most universal, while avoiding creating a pre-defined array of TextViews:
val base = "TextView_0"
for (i in 1 until 6) {
val textView = findViewById(resources.getIdentifier("${base}i", "id", packageName)
textView.text = something
}
I changed your for loop a little bit, since you had the wrong syntax. I also replaced .. with until, since .. means through the right bound, which probably isn't what you want. If you do need 6 to be a value of i, then change it back to ...
If all the TextViews are under a single parent in XML, give that parent an ID, then loop through its children:
val parent = findViewById(R.id.tvParent)
for (i in 0 until parent.getChildCount()) {
(container.getChildAt(i) as TextView).text = something
}
You can use parent container
for (i in 0 until container.childCount) {
(container.getChildAt(i) as TextView).text = something
}
A better way is to make use of DataBinding and LiveData APIs, you can assign different variables or the same variable to your TextViews' text attributes.

How to create dial link on TextView? [duplicate]

This question already has answers here:
Dialing a phone call on click of textview in android
(6 answers)
Closed 8 years ago.
I am new to android and:
I have a TextView that can show a phone number.
I want to have that part of the text to be 'touchable' and attempt to make a phone call or something similar.
I have tried to implement implicit intent for when the user clicks the TextView but I can't make it work.
Is there any solution to this? Or a different approach?
Thanks.
First make your TextView clickable by adding below in your layout.xml
<TextView
...
...
android:clickable="true">
</TextView>
And in your java code inside OnClickListener of that particular TextView Start phone activty as
TextView tv=(TextView) findViewById(R.id.tv_contact);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("tel://"+ tv.getText().toString().trim())));
}
});'
And if you want link on specific text on textview then this post may help you
Android textview with clickable phone number
Edit
As #Apoorv suggested,you may also use android:autoLink = "phone" as
In the xml file, add the below lines.
<TextView
....
android:autoLink = "phone"
/>
For more info see android:autoLink - Have a Clickable Phone Number link in a TextView, in XML
You should use built-in linkify. Read this tutorial, easy to understand: http://android.okhelp.cz/linkify-text-link-url-in-textview-text-android-example/

Android: Keep screen on [duplicate]

This question already has answers here:
Correct method for setKeepScreenOn / FLAG_KEEP_SCREEN_ON
(5 answers)
Closed 9 years ago.
I try to set the screen to always on however I cant figure out how to do it within a fragment. I have tried to get access to the ViewPager but it returns with a null value. I want the screen to be on for the whole application but it should be able to be changed by the user within the settings of the app.
Here is my code from within my fragment:
private void setIsAlwaysOn(boolean b)
{
ViewPager pager = (ViewPager)getView().findViewById(R.id.pager);
pager.setKeepScreenOn(b);
sharePropertiesEditor.putBoolean(sp_alwaysOn, b);
sharePropertiesEditor.commit();
}
I get a nullpointerexception at line 4 of the visible code above.
Yeah. Can't you use this?
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
For Kotlin, use:
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

Categories

Resources