Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8474519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:38:54+00:00 2026-06-10T17:38:54+00:00

I whould like to create a scala Views helper for Android Using this combination

  • 0

I whould like to create a scala Views helper for Android

Using this combination of trait and class

class ScalaView(view: View) {
  def onClick(action: View => Any) =
    view.setOnClickListener(new OnClickListener {
      def onClick(view: View) {
        action(view)
      }
    })
}

trait ScalaViewTrait {
  implicit def view2ScalaView(view: View) =
    new ScalaView(view)
}

I’m able to write onClick() like so

class MainActivity extends Activity with ScalaViewTrait {
//....
val textView = new TextView(this)
textView.onClick(v => v.asInstanceOf[TextView] setText ("asdas"))
}

My concern is that i want to avoid casting v to TextView

v will always be TextView if is applied to a TextView LinearLayout if applied to LinearLayout and so on.

Is there any way that v gets dynamic casted to whatever view is applied?

Just started with Scala and i need your help with this.

UPDATE

Solved see my answer below

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:38:56+00:00Added an answer on June 10, 2026 at 5:38 pm

    I managed to do it.
    Here is how

    trait ScalaViewTrait {
      implicit def view2ScalaView[T <: View](view: T) =
        new ScalaView[T](view)
    }
    
    class ScalaView[T <: View](view: T) {
      def onClick(action: T => Any) =
        view.setOnClickListener(new OnClickListener {
          def onClick(view: View) {
            action(view.asInstanceOf[T])
          }
        })
    }
    

    And now i can write onClick like this

    textView.onClick(v => v.setText("asdsa"))
    

    v is actually a TextView when onClick is applied to a TextView

    PS. Maybe i’ll write a bunch of helpers and publish them on GitHub. <3 Scala

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create this shape using just css. I am pretty sure
I'm trying to create an instance of a trait using this method val inst
I would like to create a Java class that follows the Scala setters/getters convention.
Is it possible to create types like e.g. String(20) in scala? The aim would
I would like to create a class that runs something (a runnable) at regular
I would like to create project in scala, with proper directory structure, test directory
In Scala, I would like to create a TestNG DataProvider that converts all the
I would like to deserialise Scala case classes that have been serialised using lift-json.
I am using Play framework 2.0.1 in scala to create an application that has
i would like create a array of structure which have a dynamic array :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.