1

I am making an android application and implementing a table so I am making all Text Boxes and Edit Boxes dynamically and also giving their Text Size dynamically but it is not coming according to their device size. Please help me thanks in advance. I wanted to make my app size compatible according to device.

tbrow = new TableRow(context);
        tbrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 45));
        // tbrow.setPadding(5, 0, 5,0);
        // serial no.
        TextView tv = new TextView(context);

        tv.setGravity(Gravity.CENTER);
        if (buttonchangeCount > 0 && i < saveDataCount) {
            cursor.moveToNext();
            int id = cursor.getInt(cursor.getColumnIndex("loans_id"));
            receiptId.add("" + id);
            tv.setText(id + "");
        } else {
            tv.setText((addButtCount) + "");
        }
        tv.setPadding(10, 10, 5, 10);
        tv.setTextColor(res.getColor(R.color.mainColor));
        if (Login.width >= 1000) {
            tv.setTextSize(25);
            tv.setWidth(121);
        } else if (Login.width >= 480 && Login.width < 1000) {
            tv.setTextSize(15);
            tv.setWidth(242);
            tv.setHeight(96);

        } else if (Login.width >= 320 && Login.width < 480)
            tv.setTextSize(12);
        tv.setBackgroundResource(R.drawable.cell_shape_color);
        sr_no.add(tv);
        tbrow.addView(tv);
        // Bank loan type.
        final Button butt = new Button(context);
        butt.setId(i);

        butt.setGravity(Gravity.CENTER);
        butt.setPadding(5, 10, 5, 10);
        bankLoanType.add(butt);

        if (buttonchangeCount > 0 && i < saveDataCount) {
            String str = cursor.getString(cursor.getColumnIndex("loan_type"));
            butt.setText(str);
            loanstatuslist.clear();
            loanstatuslist.add("New");
            loanstatuslist.add("Renew");
            int id1 = butt.getId();
            if (cursor.getString(cursor.getColumnIndex("loan_type")).equalsIgnoreCase("Term Loan(TL)")) {
                loanstatuslist.remove(1);
                loan_status1.get(id1).setText("New");
            }
            // } else {
            // //loan_status1.get(id1).setText("Select Loan Status");
            // }
            //
        } else {
            // butt.setText("Select Loan Type");
        }
        butt.setTextColor(res.getColor(R.color.mainColor));

        if (Login.width >= 1000) {
            butt.setTextSize(25);
            butt.setWidth(300);
        } else if (Login.width >= 480 && Login.width < 1000)

        {
            butt.setTextSize(14);
            butt.setWidth(604);
            butt.setHeight(92);
        } else if (Login.width >= 320 && Login.width < 480)
            butt.setTextSize(12);
        butt.setBackgroundResource(R.drawable.cell_shape_color);
        butt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                final int id11 = butt.getId();
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                View layout = BankLoanFragment.this.inflater.inflate(R.layout.list_view,
                        BankLoanFragment.this.container, false);
                ListView listView = (ListView) layout.findViewById(R.id.listView);
                final ArrayList<String> list = new ArrayList<String>();
                list.add("Cash Credit Limit(CCL)");
                list.add("Term Loan(TL)");
                OnlyTextAdapter adapter = new OnlyTextAdapter(context, list);
                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                builder.setView(layout);
                final AlertDialog ad = builder.create();
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                        butt.setText(list.get(position).toString());
                        String bt = butt.getText().toString();
                        final int id1 = butt.getId();
                        Log.d("bankLoan", "id1=" + id1);
                        // =
                        // bankLoanType.get(id1).getText().toString().trim();
                        //

                        if (bt.equals("Select Loan Type")
                                && (loanNumber.get(id1).getText().toString().trim().length() <= 0)) {
                            Log.d("bankLoan", "in IF case");
                            cmnBehave.alertMsg("Select Loan type and LoanNumber first.", "Loan Status");
                            // ad.dismiss();
                        } else {
                            loanstatuslist.clear();
                            String str = loan_status1.get(id1).getText().toString();
                            loanstatuslist.add("New");
                            loanstatuslist.add("Renew");
                            if (bt.equals("Term Loan(TL)")) {
                                loanstatuslist.remove(1);
                                loan_status1.get(id1).setText("New");
                                // ad.dismiss();
                            } else
                                loan_status1.get(id1).setText(str);
                        }

                        ad.dismiss();
                    }
                });
                // if(cursor.getInt(cursor.getColumnIndex("bank_loan_status")))
                if (dbutil.getnewStatusIsExists(shgid, meetingDate,
                        (loanNumber.get(id11).getText().toString().trim())) == 0) {
                    // cmnBehave.alertMsg("You have already taken Loan, you
                    // can renew Loan.", "Action");
                    ad.show();
                }

            }
        });
user3839710
  • 125
  • 11

2 Answers2

0

You can Use one Layout file to Use the all the Screens.

one Layout for all screens

In above Picture the layout file is one for all screens by the Use the different values(dimens) folder.

values-w320dp ===> For Mobiles

values-w480dp ===> For 7 inch Tablets

values-w820dp ===> For 9 and 10 Tablets

The same drawable like in the Picture

drawable-normal-hdpi ==>For Mobiles

drawable-large-hdpi ==>For 7 inch Tablets

drawable-xlarge ==>For 9 and 10 Tablets

In Layout File :

android:layout_marginRight="@dimen/login_edittext"

the login_edittext must be mention in all dimens folder at respective values

In values-w320dp inside the dimens.xml

<dimen name="login_edittext">15dp</dimen>

In values-w480dp inside the dimens.xml

<dimen name="login_edittext">30dp</dimen>

In values-w820dp inside the dimens.xml

<dimen name="login_edittext">40dp</dimen>

I think It will be help you..

Gopal
  • 580
  • 8
  • 26
0

If you are dynamically creating views then you should use Display Metrics. And check device width and height dynamically and based on that give weights to your dynamic views.

Use below code for getting screen width and height.

Code

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

ALSO check below links if helpful for you :) link1

link 2

Community
  • 1
  • 1
Developine
  • 12,483
  • 8
  • 38
  • 42