1

I am trying to make an edittext set it's size according to the size of the device - 200 pixels(for the two buttons)

tried :

Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x - 200;

        LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);

        editTextmessage = findViewById(R.id.input_chat_message);

        editTextmessage.setLayoutParams(lparams);

        editTextmessage.getLayoutParams().width = width;

and: editTextmessage.setWidth(size.x - 200); any solution for this? ps: I have googled and even searched stackoverflow prior to asking this question edit: activity_chat.xml:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ChatActivity">

    <include
        android:id="@+id/chat_toolbar"
        layout="@layout/app_bar_layout"

        />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/chat_toolbar"
        android:id="@+id/chat_list"
        android:layout_above="@id/private_chat_linear_layout"/>

    <LinearLayout
        android:id="@+id/private_chat_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">

        <EditText
            android:id="@+id/input_chat_message"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:hint="@string/write_your_message_here"
            android:padding="0dp"
            android:background="@drawable/inputs"/>

        <ImageButton
            android:id="@+id/send_message_button_chat"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/send_message" />
        <ImageButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/send_file_button_chat"
            app:srcCompat="@drawable/share_file"
            android:background="#D6D7D7"
            />

    </LinearLayout>




</RelativeLayout>

ask for more code if required

edit: Thanks @Deepika for the code, now it works. but differently on different devices. for eg: On a Redmi 2: IMG on a Redmi 2 device On a Redmi7S: IMG on a Redmi 7S device On a Oneplus 7 Pro: IMG on a Oneplus 7 Pro device so how do I fix this?

Alireza Bideli
  • 834
  • 2
  • 9
  • 31
satyamedh
  • 47
  • 1
  • 12

1 Answers1

1

I am not sure if this is what you are looking for but I tried it this way.

    EditText editText = findViewById(R.id.input_chat_message);

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
   // int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;

   int editTextWidth = width - 200;
   editText.setLayoutParams(new LinearLayout.LayoutParams(editTextWidth,LinearLayout.LayoutParams.WRAP_CONTENT));

I hope it helps.

Edit: In xml, it can be done this way. I'm using the android:weightSum attribute here for LinearLayout.

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/chat_list"
        android:layout_above="@id/private_chat_linear_layout"/>

    <LinearLayout
        android:weightSum="1"
        android:id="@+id/private_chat_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">

        <EditText
            android:layout_weight=".74"
            android:id="@+id/input_chat_message"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:hint="Write your message"
            android:padding="0dp" />

        <ImageButton
            android:layout_weight=".13"
            android:id="@+id/send_message_button_chat"
            android:layout_width="0dp"
            android:layout_height="50dp"
            app:srcCompat="@drawable/send_message"
            tools:ignore="VectorDrawableCompat" />

        <ImageButton
            android:layout_weight=".13"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:id="@+id/send_file_button_chat"
            app:srcCompat="@drawable/share_file"/>

    </LinearLayout>

</RelativeLayout>
user9078185
  • 112
  • 6
  • that returns 0 :( – satyamedh Sep 29 '19 at 11:30
  • can you post the xml file code so that I can understand what exactly you're trying to do? – user9078185 Sep 30 '19 at 05:12
  • If you want to place the buttons properly next to editTexts, you can just do it from xml. You'll have to use the android:weightSum attribute in LinearLayout. Here's a link if you need more info about it. https://stackoverflow.com/questions/7452741/what-is-androidweightsum-in-android-and-how-does-it-work – user9078185 Oct 06 '19 at 14:20
  • Thanks, that fixed it, but can you help me with this? – satyamedh Oct 06 '19 at 15:45
  • https://stackoverflow.com/questions/57294639/how-to-delete-item-from-firebaserecyclerview-without-actually-deleting-it-in-fir – satyamedh Oct 06 '19 at 15:45