0

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)).

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 1
    Please read this **[Support different screen sizes](https://developer.android.com/training/multiscreen/screensizes)** – AskNilesh Jan 24 '19 at 06:20
  • it is very time consuming i have refered this documents can u provide solution to this or any example, because i cannot able to get proper understanding through this documentory links @Nilesh – Drashti Vyas Jan 24 '19 at 06:25
  • you need to use `ConstraintLayout` for this also check this https://stackoverflow.com/q/8255985/7666442 – AskNilesh Jan 24 '19 at 06:27
  • can i do that without using constraintLayout? using LayoutParams ? – Drashti Vyas Jan 24 '19 at 06:35
  • have u check this answer https://stackoverflow.com/a/8256573/7666442 – AskNilesh Jan 24 '19 at 06:36
  • constraint layout is a pain in the mouse but it gets the job done. just make sure you dock correct handles to correct surfaces or handles. – Fractal Jan 24 '19 at 07:07
  • Use [sdp library](https://github.com/intuit/sdp) for dimension (almost works). – Abhay Koradiya Jan 24 '19 at 07:11
  • Have you considered layout_weight? It automatically resizes elements to the screen, by making proportionally sizing items in relation to other elements. – S. Czop Jan 24 '19 at 07:22
  • 1
    i got my answer , u can check i have posted on my question .. anyways ,Thank u all . @NileshRathod Fractal AbhayKoradiya S.Czop – Drashti Vyas Jan 24 '19 at 10:04

2 Answers2

0

In your layout, it can support any device depend on it' s weight attribute. the question is you have no idea about layout params. for the problem.you can links to LayoutParams.

For the recyclerView you mentioned, you can get the button's layout params in the onCreateViewHolder.

 @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.story_item, parent,
            false);
    Button button = view.findViewById(R.id.txtId);
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();
    //for example:one quater of the device's width
    layoutParams.width="your width";
    layoutParams.height="your height";
    //and so on,the layoutParams also has extra parameter
    button.setLayoutParams(layoutParams);
    ...
    }
Longalei
  • 453
  • 3
  • 8
0

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);