0

I am trying to put message n date below it in same bubble Messeage TextView. so i used LayoutParams to make bubble left or right.

But I'm getting this error.

FATAL EXCEPTION: main
                            Process: com.rana.sahaj.myyu, PID: 18103
                                                                 java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
                                                                     at com.rana.sahaj.myyu.SingleMessageRoomActivity$MessagesAdapter.getView(SingleMessageRoomActivity.java:326)
                                                                     at android.widget.AbsListView.obtainView(AbsListView.java:2257)
                                                                     at android.widget.ListView.makeAndAddView(ListView.java:1790)
                                                                     at android.widget.ListView.fillDown(ListView.java:691)
                                                                     at android.widget.ListView.fillFromTop(ListView.java:752)
                                                                     at android.widget.ListView.layoutChildren(ListView.java:1616)
                                                                     at android.widget.AbsListView.onLayout(AbsListView.java:2089)
                                                                     at android.view.View.layout(View.java:14828)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                                                                     at android.view.View.layout(View.java:14828)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                     at android.view.View.layout(View.java:14828)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                                                                     at android.view.View.layout(View.java:14828)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                     at android.view.View.layout(View.java:14828)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                                                                     at android.view.View.layout(View.java:14828)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                     at android.view.View.layout(View.java:14828)
                                                                     at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2026)
                                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1783)
                                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1039)
                                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5648)
                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                                     at android.view.Choreographer.doFrame(Choreographer.java:544)
                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                                     at android.os.Handler.handleCallback(Handler.java:733)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5052)
                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                     at dalvik.system.NativeStart.main(Native Method)

The getView() is this of SingleMessageRoom Activity.

  @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View convertView;

        if(view == null){
            convertView = layoutInflater.inflate(R.layout.message, parent, false);
        } else {
            convertView = view;
        }

        Message message = getItem(position);

        TextView nameView = (TextView)convertView.findViewById(R.id.message);
        nameView.setText(message.getText());

        TextView timeView=(TextView)convertView.findViewById(R.id.timeMessage);
        timeView.setText(message.getTime());

        LinearLayout mLinearLayout=(LinearLayout)convertView.findViewById(R.id.layoutBubble);


        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)mLinearLayout.getLayoutParams();

        int sdk = Build.VERSION.SDK_INT;
       if (message.getSender().equals(EmailPref)){
            if (sdk >= Build.VERSION_CODES.JELLY_BEAN) {
                mLinearLayout.setBackground(this.getContext().getResources().getDrawable(R.drawable.mybubbleown2));
                nameView.setTextColor(this.getContext().getResources().getColor(R.color.white));


            } else{
                mLinearLayout.setBackground(getDrawable(R.drawable.mybubbleown2));
               nameView.setTextColor(this.getContext().getResources().getColor(R.color.white));

            }
            layoutParams.gravity = Gravity.RIGHT;
        }else{
            if (sdk >= Build.VERSION_CODES.JELLY_BEAN) {
                mLinearLayout.setBackground(this.getContext().getResources().getDrawable(R.drawable.myotherbubbleown2));

                nameView.setTextColor(this.getContext().getResources().getColor(R.color.black));

            } else{
                mLinearLayout.setBackground(getDrawable(R.drawable.myotherbubbleown2));

                nameView.setTextColor(this.getContext().getResources().getColor(R.color.black));

            }
            layoutParams.gravity = Gravity.LEFT;
        }

        mLinearLayout.setLayoutParams(layoutParams);

        return convertView;

    }
Sahaj Rana
  • 1,993
  • 4
  • 25
  • 42
  • 1
    LayoutParams are provided by parent view. So ListView provides AbsListView.LayoutParams for your LinearLayout, LinearLayout provides LinearLayout.LayoutParams for its children and so on. Either use `AbsListView.LayoutParams layoutParams` or perhaps `mLinearLayout.setPadding()`. – Eugen Pechanec Apr 24 '16 at 23:24
  • okie so by this the error is removed but both types of messages are coming on left side.. – Sahaj Rana Apr 24 '16 at 23:33
  • ie the sender and recievers – Sahaj Rana Apr 24 '16 at 23:34
  • 1) Quick question: Why aren't you setting this up in layout/message.xml instead? Android Studio has a fancy preview mode which could help you set this up. 2) It can be either because `message.getSender().equals(EmailPref)` is never true OR your text views have `android:layout_width="match_parent"` so setting `android:layout_gravity` has no effect on them. Use `TextView.setGravity()` to set gravity *within* a text view. – Eugen Pechanec Apr 24 '16 at 23:40
  • you gave me a idea I am trying it n will let u know if it works ! – Sahaj Rana Apr 24 '16 at 23:51

0 Answers0