setActionView deprecated [duplicate] - android

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)

Related

Android Studio Kotlin - Onclick listener not being recognized

So this is my first time using Kotlin and Android studio and I really got to say thus far my experience has been horrible. No other language or IDE has put me in the position where I had to do more than hours research to try and make a simple button load the next page and then still being unable to do it.
This is my Login.kt page
and there you can see its having problems with the findviewbyID and setonlclicklistener.
Top of my Activity_login.xml page
The button im trying to create the onclick event for.
Please keep in mind I did try just the normal creation of setonclick for the button but then I didnt seem to recognize that I have a button with the ID of login_btn
change the init button to
val button=findViewById<Button>(R.id.login_btn)
in my case i have removed onclick from xml.
val button : Button = findViewById(R.id.login_btn);
button.setOnClickListener {
}
If no answers above can help you, you can use viewbinding to access your view. I had so many problems while using findViewById as a newbie. Here is the documentation,
https://developer.android.com/topic/libraries/view-binding

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 display Edittext in Android [duplicate]

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,

Casting result of findViewById is redundant [duplicate]

This question already has answers here:
No need to cast the result of findViewById?
(6 answers)
Closed 5 years ago.
I'm new to Android and I'm working through some exercises on using various views. One such example is:
TextView messageView = (TextView) findViewById(R.id.message);
My question is this: what would be the benefit of casting TextView? My IDE tells me that casting the method is redundant. Are there any use cases where I would want to cast in this way?
Before API level 26, the method findViewById returned the reference of View class. So you needed to cast it.
//old signature
public View findViewById(int id){
//
}
But starting from API level 26, it has been updated and it returns subclass of View using template so that you can assign the returned reference without casting.
//new signature
public <T extends View > T findViewById(int id){
//
}
The example which you referred used the older API level while building the project, so you can see the casting there. It was compulsory earlier but is not necessary now. So you are getting the warning.

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