I am new in Android TV development.
I use android leanback library for this. Also I I use BrowseSupportFragment and RowsSupportFragmen.
Here I want to customise the HeaderItem shown in the picture.
Specially I want to change its font. I check lots of things but not getting proper solution for this.
Thank You.
If you use BrowseSupportFragment you can use method setHeaderPresenterSelector() and register own presenter for headers. It should inherard from RowHeaderPresenter
If you want to override all header at once then you can create file
res/values/layout/lb_row_header.xml and redefine layout for default header presenter.
Try this:
override fun onBindRowViewHolder(holder: RowPresenter.ViewHolder?, item: Any?) {
super.onBindRowViewHolder(holder, item)
val textView = holder?.headerViewHolder?.view?.findViewById<RowHeaderView>(R.id.row_header)
textView?.run {
setTextColor(Color.WHITE)
setTextSize(TypedValue.COMPLEX_UNIT_SP, 12.5f)
isAllCaps = true
setFontFamily(context, R.font.hind_bold)
}
}
Related
How do I make a button without any XML? I tried XML but it did not work and it is "Old" I heard.
Yeah, using XML is old but it's the standard way of defining views in Android. Nowadays exist alternatives to that such as Jetpack Compose which takes a more React style when declaring the GUI where you write #Composable functions that produce a UI. Quite nice.
In any case you can create the views yourself programatically but it's much more tedious, less maintainable and imho 💩
With that said, from your activity you can create instances of any of the layouts that you would use in XML and then add more views into it:
class YourActivty: AppCompatActivity() {
override fun onCreate(...) {
val frameLayout = FrameLayout(this).apply {
// Configure the layout's properties
}
val button = AppCompatButton(this).apply {
// Configure all button's properties
}
frameLayout.addView(button)
// Indicate your activity to use the framelayout as its content
setContentView(frameLayout)
}
}
There are some controls in our app which we'd like to update the control type read out by Talkback. For example: A TextView which would better be described as a link, or an ImageView that would better be described as a button.
We could simply update the content description to report out the desired control type, though I am wondering if there is a better way? If there is another way, can it be done both through the view XML and dynamically in the code behind?
Yes, it is possible to change the type. It is called roleDescription. You would change it as follows:
ViewCompat.setAccessibilityDelegate(yourView,
object : AccessibilityDelegateCompat() {
override fun onInitializeAccessibilityNodeInfo(v: View, info: AccessibilityNodeInfoCompat) {
super.onInitializeAccessibilityNodeInfo(v, info)
info.roleDescription = "Button"
}
})
(use string resources and translate the strings to all languages supported by your app)
This cannot be done via XML by default, but you could look into writing your own binding adapter for this.
as I wrote in the title, the Android Talkback actually reads the hint text of my search bars, but I need to change that behavior to read a custom string (different from the hint). I tried to set the contentDescription but it didn't work (the talkback still reads the hint text).
Do you have any advice?
You can set an AccessibilityDelegate on a view to override things like how it describes itself in general:
ViewCompat.setAccessibilityDelegate(yourView, object : AccessibilityDelegateCompat(){
override fun onInitializeAccessibilityNodeInfo(host: View?, info: AccessibilityNodeInfoCompat?) {
super.onInitializeAccessibilityNodeInfo(host, info)
info?.let { it.text = "whatever" }
}
})
or you can override onPopulateAccessibilityEvent (which handles triggered events like AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED) and use event.getText(), which gives you a List<CharSequence> you can add your description to. It really depends on what you're doing and how dynamic it needs to be.
I haven't found a good overview of all this stuff unfortunately, otherwise I'd link you to it. I think the delegates are what you need to look into though
As you will realise while reading this, I am new to programming, and I am very much stuck.
I have 5 text views in the layout with ID´s "box_one_text , box_two_text" and so on, when I try to set the click listeners on MainActivity, I create a list of items calling for each textView by it's ID:
private fun setListeners(){
val clickableViews: List<View> =
listOf(box_one_text, box_two_text, box_three_text, box_four_text,
box_five_text, constraint_layout)
for(item in clickableViews){
item.setOnClickListener { makeColored(it) }
}
}
Everything inside listOf is an error, calling for the "Unresolved reference" I talked about earlier)
How do I fix this?
I also came across this issue and finally found the answer here:
https://github.com/udacity/andfun-kotlin-color-my-views/issues/11
This is because view binding is missing from our code; to solve this problem, there are 2 solutions.
The simple way is to add the below line to app level Gradle file:
id 'kotlin-android-extensions'
The other solution is to follow the data binding steps which is discussed here: https://classroom.udacity.com/courses/ud9012/lessons/4f6d781c-3803-4cb9-b08b-8b5bcc318d1c/concepts/68b85cff-8813-496b-86ba-57ed352d8bcf. I'm not sure if you're following the same course as me, but the course I linked is free and it has a similar (highly likely the same) code exercise as yours.
There are some other ideas from the GitHub URL I included above.
box_one_text, box_two_text, box_three_text, box_four_text, box_five_text, constraint_layout dont refer to the views directly instead they are just the IDs Use findViewById to find the required view and later set OnClickListener
Try the following
val view1 : View = findViewById(R.id.box_one_text)
val view2 : View = findViewById(R.id.box_two_tex)
//pattern follows for the rest of the views too
val clickableViews: List<View> =
listOf(view1, view2, view3 ....)
for(item in clickableViews){
item.setOnClickListener { makeColored(it) }
}
I just got a hold of Butterknife and have been trying my best to standardize all of my 'OnClick's to be bound via Butterknife.
I have found though, that it's difficult to follow Butterknife's standard binding pattern when dynamically populating views (via adapters for example) since the individual views don't have id's
#OnClick(What Do I put here if I have no ID?)
public void OnClickMethod(View view) {
//Body
}
Specifically, I'm having problems adding onClicks to views that are part of a TabLayout. I know I can use the built in
TabLayout.setOnTabSelectedListener()
But ideally I'd like to be consistent in binding all forms of onClick via Butterknife. Is there a clean way of doing this?
Set an id in res/values/ids.xml like :
<item name="my_view" type="id"/>
And then add the id to the view :
myView.setId(R.id.my_view);
#OnClick(R.id.my_view)
public void OnClickMethod(View view) {
//Body
}