0

I am currently creating an Android app that I want to support multiple screen sizes/densities. When I set up layouts in xml, everything looks fine across different screen sizes. However, there are rows I need to add programatically.

Whenever I add the rows, I can't seem to get things to look consistent across different devices. I believe using the dp unit when settings up heights, widths in xml, along with wrap_content and match_parent when appropriate, have allowed things to easily translate between different devices.

Programatically, when I try to set a layout height/width, I have been trying to convert the anticipated dp value to a pixel value. I've done so using this:

    public static int convertToPixels(int value) {
        Resources resources = mContext.getResources();
        int x = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,resources.getDisplayMetrics());
        return x;
    }

Overall, the heights look ok, but the widths don't look good. For instance, the width of the row will look such that on a smaller device, the information neatly displays across the row, which is what I want. However,when I try to run the app on a tablet, for instance, the information will only stretch to half of the row, which doesn't look good. I want the sizes to scale and neatly display just like on the smaller device.

If anyone has any idea what my problem might be, I would greatly appreciate it. Below is the source for adding a row using java:

        LinearLayout row = new LinearLayout(mContext);
        row.setOrientation(LinearLayout.HORIZONTAL);
        row.setId(Integer.parseInt(txn.getId().toString()));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(60));
        params.setMargins(0, convertToPixels(1), 0, 0);

        row.setLayoutParams(params);
        row.setBackgroundColor(mContext.getResources().getColor(R.color.white));

        LinearLayout imageLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(convertToPixels(40), convertToPixels(40));
        imageParams.gravity = Gravity.CENTER;
        imageLayout.setLayoutParams(imageParams);
        ImageView image = new ImageView(mContext);


        if (txn.getTransactionStateID() == Constants.TXN_STATUS_OK) {
            image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ok));
        } else if (txn.getTransactionStateID() == Constants.TXN_STATUS_SUSPICIOUS) {
            image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.alert));
        } else if (txn.getTransactionStateID() == Constants.TXN_STATUS_RED_FLAG) {
            image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.flag));
        }

        imageLayout.addView(image);

        row.addView(imageLayout);

        LinearLayout txnMiddleLayout = new LinearLayout(mContext);
        txnMiddleLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams txnTopParams = new LinearLayout.LayoutParams(convertToPixels(400), convertToPixels(60));
        txnTopParams.setMargins(convertToPixels(10), 0, 0, 0);
        txnMiddleLayout.setLayoutParams(txnTopParams);

        TextView txnTopContents = new TextView(mContext);
        txnTopContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
        txnTopContents.setText(txn.getTopLineContents());
        txnTopContents.setTextColor(Color.BLACK);
    //    txnTopContents.setTextSize(convertToPixels(16));
        TextView txnBottomContents = new TextView(mContext);
        txnBottomContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
        txnBottomContents.setText(txn.getBottomLineContents());
    //    txnBottomContents.setTextSize(convertToPixels(12));

        txnMiddleLayout.addView(txnTopContents);
        txnMiddleLayout.addView(txnBottomContents);

        row.addView(txnMiddleLayout);


        LinearLayout txnBottomLayout = new LinearLayout(mContext);
        txnBottomLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams txnBottomParams = new LinearLayout.LayoutParams(convertToPixels(120), convertToPixels(60));
        txnBottomLayout.setLayoutParams(txnBottomParams);

        TextView amount = new TextView(mContext);
        amount.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
        amount.setText(txn.getAmountStr());
        amount.setTextColor(Color.BLACK);
 //      amount.setTextSize(convertToPixels(16));

        TextView date = new TextView(mContext);
        date.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
        date.setText(txn.getDateStr());
    //    date.setTextSize(convertToPixels(12));

        txnBottomLayout.addView(amount);
        txnBottomLayout.addView(date);

        row.addView(txnBottomLayout);

        txnList.addView(row);
arjun kumar
  • 183
  • 3
  • 14

1 Answers1

0

I eventually found a solution. Instead of trying to set an exact width value, I set the width of the layout or textview to 0, and used layout_weight instead.

https://stackoverflow.com/a/3996104/4056947

Community
  • 1
  • 1
arjun kumar
  • 183
  • 3
  • 14