Lauri
Lauri

Reputation: 25

adding padding to textview and adding scrollview

the picture phone

So I am wondering how to create a space between the text and the bubble. I tried it with padding, but somehow it doesn't work.

The padding doesn' t show on the View for some reason.

Furthermore I am wondering how to make the chat to a scroll view so the chat can continue the whole way down.

this is how I am adding Texts at the time:

protected void create(Context context, View v, EditText _writetexthere, int margin) {
        String inputmessage = _writetexthere.getText().toString().trim();
        RelativeLayout relativeLayout = v.findViewById(R.id.rel);
        TextView message = new TextView(context);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //margin bubble
        lp.setMargins(700,margin,5,5);

        message.setLayoutParams(lp);
        message.setText(inputmessage);

        //text size
        message.setTextSize(15);
        message.setBackgroundResource(R.drawable.messagemine);
        message.setTextColor(Color.parseColor("#FFFFFF"));
        relativeLayout.addView(message);
    }

Thanks,

Regards Laurenz

my xml chat fragment:

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/rel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/chatwall"
        android:orientation="vertical">

        <TextView
            android:id="@+id/phonenumberchat"
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:background="@android:color/white"
            android:gravity="center"
            android:textColor="@android:color/black"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="69dp"
            android:layout_height="47dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="70dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="5dp"
            android:layout_weight="1"
            android:background="@drawable/sendbutton"
            android:onClick="sendmessage"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:srcCompat="@android:drawable/ic_menu_send" />

        <EditText
            android:id="@+id/writeTextHere"
            android:layout_width="290dp"
            android:layout_height="47dp"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="10dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/editextround"
            android:hint="Write Text here"
            android:inputType="textPersonName"
            android:paddingLeft="15dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="708dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="57dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />
        </ScrollView>
    </RelativeLayout>

Upvotes: 2

Views: 633

Answers (1)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15423

Try to use padding with margin to achieve desired result. And you should use LinearLayout instead of RelativeLayout from your xml.

protected void create(Context context, View v, EditText _writetexthere, int margin) {
    String inputmessage = _writetexthere.getText().toString().trim();
    LinearLayout linearLayout = v.findViewById(R.id.message_layout);
    TextView message = new TextView(context);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    //Gravity to align right
    lp.gravity = Gravity.RIGHT;
    lp.setMargins(0, margin, 0, margin);

    //padding will give space for bubble
    message.setPadding(50, margin, 50, margin);

    message.setLayoutParams(lp);
    message.setText(inputmessage);

    //text size
    message.setTextSize(15);
    message.setBackgroundResource(R.drawable.test_enable);
    message.setTextColor(Color.parseColor("#FFFFFF"));
    linearLayout.addView(message);
}

And change your ScrolView like below to hold message with scrolling

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/phonenumberchat"
    android:layout_above="@+id/writeTextHere">

    <LinearLayout
        android:id="@+id/message_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>

Output:

enter image description here

Upvotes: 1

Related Questions