0

I have a ListView with RelativeLayout, refer below:

MainActivity.java

adapter = new ArrayAdapter<String>(this, R.layout.listviewitemtext, sortedmessage){
        public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
            View row = convertView;
        TextView text1 = (TextView) view.findViewById(id.msgThread);
            String msgBody=msgThread.getItemAtPosition(position).toString();
            char lastChar=msgBody.charAt(msgBody.length()-1);
            if( lastChar=='1'){
                text1.setText(msgBody.substring(0, msgBody.length() - 1));
                text1.setBackgroundResource(drawable.incoming_bubble_telegram);
                text1.setTextColor(Color.BLACK);
                text1.setPadding(60,60,60,60);
            }
            else if( lastChar=='0'){
                text1.setText(msgBody.substring(0, msgBody.length() - 1));
                text1.setBackgroundResource(drawable.outgoing_bubble_telegram_selected);
                text1.setTextColor(Color.WHITE);
                text1.setPadding(60, 60, 60, 60);

            }
        return view;

    };};
    msgThread.setAdapter(adapter);`

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.clyde.listviewtest.MessageThread">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="500dp"
        android:id="@+id/msgList"
        android:divider="@android:color/transparent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:background="#FFFFFF"
        />
</RelativeLayout>

listviewitemtext.xml

<TextView
        android:id="@+id/msgThread"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:maxWidth="1000px"
        android:layout_alignParentRight="true"
 />

What I want to achieve is align the textview on the right. layout_alignParentRight doesn't seem to work. And when I use RelativeLayout on my TextView my app crashes/not responding. Any help would be much appreciated.

Note: Please take note that Listview is styled in the MainActivity.xml, and TextView is styled in a separate xml file (listviewitems.xml). I apply this listviewitems.xml as a custom layout in my adapter.

Stacktrace:

>08-29 20:55:39.371    2474-2474/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.clyde.listviewtest, PID: 2474
    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
            at com.example.clyde.listviewtest.MessageThread$1.getView(MessageThread.java:65)
            at android.widget.AbsListView.obtainView(AbsListView.java:2836)
            at android.widget.ListView.makeAndAddView(ListView.java:1903)
            at android.widget.ListView.fillSpecific(ListView.java:1364)
            at android.widget.ListView.layoutChildren(ListView.java:1698)
            at android.widget.AbsListView.onLayout(AbsListView.java:2640)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2414)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2123)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1264)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6943)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
            at android.view.Choreographer.doCallbacks(Choreographer.java:590)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
     Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
            at com.example.clyde.listviewtest.MessageThread$1.getView(MessageThread.java:65)
            at android.widget.AbsListView.obtainView(AbsListView.java:2836)
            at android.widget.ListView.makeAndAddView(ListView.java:1903)
            at android.widget.ListView.fillSpecific(ListView.java:1364)
            at android.widget.ListView.layoutChildren(ListView.java:1698)
            at android.widget.AbsListView.onLayout(AbsListView.java:2640)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:16916)
            at android.view.ViewGroup.layout(ViewGroup.java:5405)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2414)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2123)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1264)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6943)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
            at android.view.Choreographer.doCallbacks(Choreographer.java:590)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Clyde Winux
  • 285
  • 1
  • 3
  • 12
  • can you tell for the purpose of xmlns:android="http://schemas.android.com/apk/res/android" in TextView – Mohammad Tauqir Aug 29 '15 at 12:36
  • you want the textView in the Listview or in the relativeLayout? – bendaf Aug 29 '15 at 12:37
  • @bendaf my textView is populated as items on my ListView using an adapter. And I want them to float right. I googled and searched on stackoverflow and someone suggested to use RelativeLayout in order for the android:layout_alignParentRight to work. – Clyde Winux Aug 29 '15 at 12:39
  • I think you should use the RelativLayout outside of the TextView not the ListView :). I will write my answer... – bendaf Aug 29 '15 at 12:40
  • @bendaf it's very long, I updated my OP. – Clyde Winux Aug 29 '15 at 12:57
  • oh, yes you have to make an own ArrayAdapter for this, like in this post: http://stackoverflow.com/a/15832564/3162918 I will search if there is an easier solution for this – bendaf Aug 29 '15 at 13:00
  • @bendaf I included the code on my main java file. Please check it. :) – Clyde Winux Aug 29 '15 at 13:04

1 Answers1

0

If you want just the gravity of the textView right than you can use the standard ArrayAdapter and here is your answer:

<TextView
    android:id="@+id/msgThread"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxWidth="1000px"
    android:gravity="right"/>

If you want the TextView's width to wrap_content than you should create a new ArrayAdapter and an own layout for your list item like in this post. I will write here just the difference, your row.xml should be like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/msgThread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="1000px"
        android:layout_alignParentRight="true"
        android:gravity="right"/>
</RelativeLayout>
Community
  • 1
  • 1
bendaf
  • 2,981
  • 5
  • 27
  • 62
  • It's still crashes. Please take note that Listview is styled in the MainActivity.xml, and TextView is styled in a separate xml file (listviewitems.xml). I apply this listviewitems.xml as a custom layout in my adapter. – Clyde Winux Aug 29 '15 at 12:49
  • I see, what is the stacktrace? Could you share it and your initialization code also? – bendaf Aug 29 '15 at 12:53
  • gravity will only set the alignment of the text and since the TextView's layout_width is wrap_content because I am using an image background. I just want the TextView to float right. – Clyde Winux Aug 29 '15 at 13:16
  • Ok I will complete my answer – bendaf Aug 29 '15 at 13:18
  • 1
    Have a quick look at this tutorial http://www.vogella.com/tutorials/AndroidListView/article.html#listview_listviewexample – Want2bExpert Aug 29 '15 at 13:32