androidandroid-layoutkotlin

My layout file is not showing in setContent(R.layout.xml file)..?


I have written my code but in the code when I use view = ..(R.layout.my_message) it shows error as such file does not exist while it is already created. While rewriting name again I got option of activity_list_item which is not exist in my code. What I do to get my layout xml file?

Project Structure Project Structure

I have tried alot and search too but found nothing.

    class MessageAdapter(val context: Context) : RecyclerView.Adapter<MessageAdapter.MessageViewHolder>(){

        companion object {
            const val VIEW_TYPE_MESSAGE_SENT = 1
           const val VIEW_TYPE_MESSAGE_RECEIVED = 2
        }

        private val messages: ArrayList<Message> = ArrayList()

        fun addMessage(message: Message){
            messages.add(message)
            notifyDataSetChanged()
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder {
            val view: View

            if (viewType == VIEW_TYPE_MESSAGE_SENT) {
              view= LayoutInflater.from(parent.context).inflate(R.layout.my_message, parent, false)
                return MessageViewHolder(view)
            } else  {
                view = LayoutInflater.from(parent.context).inflate(R.layout.other_message, parent, false)
                return OtherMessageViewHolder(view)
            }

        }

        override fun getItemCount(): Int {
            return messages.size
        }

        override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
            val message = messages.get(position)

            holder.bind(message)
        }

        open inner class MessageViewHolder(view : View) : RecyclerView.ViewHolder(view){
                private var messageText = view.txtMyMessage
                private var timeText = view.txtMyMessageTime
           open fun bind(message: Message){
             messageText.text = message.message
                timeText.text = fromMillisToTimeString(millis = message.time)
            }
        }

        inner class OtherMessageViewHolder (view: View) : MessageViewHolder(view) {

            internal var messageText: TextView
            internal var timeText: TextView
            internal var nameText: TextView
            internal var profileImage: ImageView

            init {

                messageText = itemView.text_message_body as TextView
                timeText = itemView.text_message_time as TextView
                nameText = itemView.text_message_name as TextView
                profileImage = itemView.image_message_profile as ImageView
            }


            fun bind(message: Message) {
                messageText.text = message.message
                timeText.text = fromMillisToTimeString(message.time)
                Utils.displayRoundImageFromUrl(context, message.getSender().getProfileUrl(), profileImage);
            }
        }
        object DateUtils {
            fun fromMillisToTimeString(millis: Long) : String {
                val format = SimpleDateFormat("hh:mm a", Locale.getDefault())
                return format.format(millis)
            }
        }
        override fun getItemViewType(position: Int): Int {
            val message = messages.get(position)

            return if(message.isSending){
                VIEW_TYPE_MESSAGE_SENT
            } else {
                VIEW_TYPE_MESSAGE_RECEIVED
            }
        }

    }

Expected: (R.layout.my_message, parent, false) (R.layout.other_message, parent, false)

Actual:

other_message and my_message are unresolved reference.


Solution

  • if you'r sure, that the layouts you mentioned are in the res/layout folder, then maybe they just aren't recognized by the AndroidStudio.

    so first try to clean / Rebuild you Project: in AndroidStudio (Mac) click on:

    Build -> Clean Project

    then

    Build -> Rebuilt Project

    if this doesn't help, then you have to clear the Cache and restart the Android Studio (Mac) :

    File -> Invalidate Caches / Restart...