androidandroid-buttonandroid-relativelayoutscreen-sizeandroid-layoutparams

Android support for different screen sizes


I have created this layout. in which showing textview and below it , it has share buttons for insta, fb, copy,tweet.

content.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="wrap_content"
android:orientation="vertical"
    android:id="@+id/Main"
    android:layout_gravity="center"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent">

    <RelativeLayout
        android:id="@+id/Sub_main"
        android:layout_width="match_parent"
        android:gravity="bottom"
        android:background="@drawable/linerlayout_background"
        android:layout_height="match_parent">


        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"


      android:layout_margin="15dp"
        android:id="@+id/txt_hash"/>

        <LinearLayout
            android:id="@+id/sub"
            android:layout_below="@+id/txt_hash"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button">


            <Button android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:background="@drawable/fb"
                android:id="@+id/fb"/>


            <Button android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/insta"
                android:id="@+id/insta"/>


            <Button android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:background="@drawable/tweet"
                android:id="@+id/tweet"/>


            <Button android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/doc"
                android:id="@+id/doc"/>

        </LinearLayout>

</RelativeLayout>

I have recyclerview which has this content to view. Now I want to fit these buttons properly according to every device screen size. I have no idea about layoutparams, I have referred links but I need to understand how to calculate size of a particular button inside(Framelayout/RelativeLayout/LinearLayout/Buttons(4)).


Solution

  • Who want to set screen without using constraintview or creating different .xmls and image files can follow this method as i got the solution.

    i have implemented in my adapter.class

        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int deviceWidth = displayMetrics.widthPixels; 
        int deviceHeight= displayMetrics.heightPixels;
    
        int Linearwidth=    (deviceWidth*692 ) /720;
        int Linearheight =   (deviceHeight *100 ) /1280;
    
        int iv_width = (deviceWidth *78)/720;
        int iv_height=(deviceHeight*78)/1280;
    
        int Text_Linear_Width = (deviceWidth * 710)/720;
        int Text_Linear_Height =(deviceHeight * 319)/1280;
        txt_hash.setTextSize(TypedValue.COMPLEX_UNIT_PX, Text_Linear_Width * 28 / 720);
    
        LinearLayout.LayoutParams lp =new LinearLayout.LayoutParams(iv_width,iv_height);
    
        lp.leftMargin = (Linearwidth*78)/720;
        lp.rightMargin = (Linearheight*78)/1280;
        lp.bottomMargin=(Linearheight*200)/1280;
        lp.topMargin=(Linearheight*200)/1280;
    
    
        fb.setLayoutParams(lp);
        insta.setLayoutParams(lp);
        tweet.setLayoutParams(lp);
        doc.setLayoutParams(lp);