how to support all screen sizes when using more than one spinner?

Refresh

February 2019

Views

6 time

0

I am making a new app and I am a beginner so kindly help me with this, my layout is working great if I run without any java code for layout but when I use Java code, my layout is going beyond the screen

I have tried to make different layout folders such as layout-sw320dp....

<TableLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="1dp">
        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CIVIL 1-1"
            android:textSize="17sp"
            android:layout_gravity="center"
            android:textStyle="bold"

            />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="1dp">

        <TextView
            android:id="@+id/t1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="English - 1"
            android:textSize="17sp"
            android:textStyle="bold"></TextView>

        <Spinner
            android:spinnerMode="dialog"
            android:id="@+id/s2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </Spinner>

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="1dp">

        <TextView
            android:id="@+id/t2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mathematics - 1"
            android:textSize="17sp"
            android:textStyle="bold"></TextView>

        <Spinner
            android:spinnerMode="dropdown"
            android:id="@+id/s1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </Spinner>

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="1dp">


        <TextView
            android:id="@+id/t3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Chemistry"
            android:textSize="17sp"
            android:textStyle="bold"></TextView>

        <Spinner
            android:spinnerMode="dropdown"
            android:id="@+id/s3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </Spinner>

    </TableRow>


    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="1dp">

        <TextView
            android:id="@+id/t4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Engineering Mechanics"
            android:textSize="17sp"
            android:textStyle="bold"></TextView>

        <Spinner
            android:spinnerMode="dropdown"
            android:id="@+id/s4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </Spinner>
    </TableRow>


    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="1dp">

        <TextView
            android:id="@+id/t5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Computer Programming"
            android:textSize="17sp"
            android:textStyle="bold"></TextView>

        <Spinner
            android:spinnerMode="dropdown"
            android:id="@+id/s5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </Spinner>

    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="1dp">


        <TextView
            android:id="@+id/t6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Environmental Studies"
            android:textSize="17sp"
            android:textStyle="bold"></TextView>

        <Spinner
            android:spinnerMode="dropdown"
            android:id="@+id/s6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </Spinner>

    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="1dp">


        <TextView
            android:id="@+id/t7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Chemistry Laboratory"
            android:textSize="17sp"
            android:textStyle="bold"></TextView>

        <Spinner
            android:spinnerMode="dropdown"
            android:id="@+id/s7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </Spinner>

    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="1dp">

        <TextView
            android:id="@+id/t8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="English Communication skills lab - 1"
            android:textSize="17sp"
            android:textStyle="bold"></TextView>

        <Spinner
            android:spinnerMode="dropdown"
            android:id="@+id/s8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </Spinner>

    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="1dp">


        <TextView

            android:id="@+id/t9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Computer Programming Lab"
            android:textSize="17sp"
            android:textStyle="bold"></TextView>

        <Spinner
            android:spinnerMode="dropdown"
            android:id="@+id/s9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </Spinner>
    </TableRow>
    <TextView
        android:id="@+id/textViewSGPA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="SGPA"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        android:textStyle="bold"
        />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Calculate"
        />

    <Button
        android:id="@+id/button_Save"
        android:enabled="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save my data"
        />

</TableLayout>




public class Main2Activity extends AppCompatActivity {

    Spinner s1, s2, s3, s4, s5, s6, s7, s8, s9;
    Button button, btnsave;
    TextView textView;
    private int a, b, c, d, e, f, g, h, i;
    float total;
    int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0, sum8 = 0, sum9 = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        final String[] strings = {"O", "S", "A", "B", "C", "D", "F"};


        sum1 = 0;
        sum2 = 0;
        sum3 = 0;
        sum4 = 0;
        sum5 = 0;
        sum6 = 0;
        sum7 = 0;
        sum8 = 0;
        sum9 = 0;
        total = 0.0f;

        s1 = findViewById(R.id.s1);
        s2 = findViewById(R.id.s2);
        s3 = findViewById(R.id.s3);
        s4 = findViewById(R.id.s4);
        s5 = findViewById(R.id.s5);
        s6 = findViewById(R.id.s6);
        s7 = findViewById(R.id.s7);
        s8 = findViewById(R.id.s8);
        s9 = findViewById(R.id.s9);

        button = findViewById(R.id.button2);
        textView = findViewById(R.id.textViewSGPA);
        btnsave = findViewById(R.id.button_Save);


        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(Main2Activity.this,R.layout.support_simple_spinner_dropdown_item,strings);
        //final ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.Grades1, android.R.layout.simple_spinner_dropdown_item);
        //arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(stringArrayAdapter);
        s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                a = position;
                String t1 = parent.getItemAtPosition(position).toString();

                if (a == 0) {
                    sum1 = 10;
                }
                if (a == 1) {
                    sum1 = 9;
                }
                if (a == 2) {
                    sum1 = 8;
                }
                if (a == 3) {
                    sum1 = 7;
                }
                if (a == 4) {
                    sum1 = 6;
                }
                if (a == 5) {
                    sum1 = 5;
                }
                if (a == 6) {
                    sum1 = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        s2.setAdapter(stringArrayAdapter);
        s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                b = position;
                String t2 = parent.getItemAtPosition(position).toString();
                if (b == 0) {
                    sum2 = 10;
                }
                if (b == 1) {
                    sum2 = 9;
                }
                if (b == 2) {
                    sum2 = 8;
                }
                if (b == 3) {
                    sum2 = 7;
                }
                if (b == 4) {
                    sum2 = 6;
                }
                if (b == 5) {
                    sum2 = 5;
                }
                if (b == 6) {
                    sum2 = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        s3.setAdapter(stringArrayAdapter);
        s3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                c = position;
                if (c == 0) {
                    sum3 = 10;
                }
                if (c == 1) {
                    sum3 = 9;
                }

                if (c == 2) {
                    sum3 = 8;
                }

                if (c == 3) {
                    sum3 = 7;
                }

                if (c == 4) {
                    sum3 = 6;
                }
                if (c == 5) {
                    sum3 = 5;
                }
                if (c == 6) {
                    sum3 = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        s4.setAdapter(stringArrayAdapter);
        s4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                d = position;
                if (d == 0) {
                    sum4 = 10;
                }
                if (d == 1) {
                    sum4 = 9;
                }
                if (d == 2) {
                    sum4 = 8;
                }
                if (d == 3) {
                    sum4 = 7;
                }
                if (d == 4) {
                    sum4 = 6;
                }
                if (d == 5) {
                    sum4 = 5;
                }
                if (d == 6) {
                    sum4 = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        s5.setAdapter(stringArrayAdapter);
        s5.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                e = position;
                if (e == 0) {
                    sum5 = 10;
                }
                if (e == 1) {
                    sum5 = 9;
                }
                if (e == 2) {
                    sum5 = 8;
                }
                if (e == 3) {
                    sum5 = 7;
                }
                if (e == 4) {
                    sum5 = 6;
                }
                if (e == 5) {
                    sum5 = 5;
                }
                if (e == 6) {
                    sum5 = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        s6.setAdapter(stringArrayAdapter);
        s6.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                f = position;
                if (f == 0) {
                    sum6 = 10;
                }
                if (f == 1) {
                    sum6 = 9;
                }
                if (f == 2) {
                    sum6 = 8;
                }
                if (f == 3) {
                    sum6 = 7;
                }
                if (f == 4) {
                    sum6 = 6;
                }
                if (f == 5) {
                    sum6 = 5;
                }
                if (f == 6) {
                    sum6 = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });



        s7.setAdapter(stringArrayAdapter);
        s7.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                g = position;
                if (g == 0) {
                    sum7 = 10;
                }
                if (g == 1) {
                    sum7 = 9;
                }
                if (g == 2) {
                    sum7 = 8;
                }
                if (g == 3) {
                    sum7 = 7;
                }
                if (g == 4) {
                    sum7 = 6;
                }
                if (g == 5) {
                    sum7 = 5;
                }
                if (g == 6) {
                    sum7 = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        s8.setAdapter(stringArrayAdapter);
        s8.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                h = position;
                if (h == 0) {
                    sum8 = 10;
                }
                if (h == 1) {
                    sum8 = 9;
                }
                if (h == 2) {
                    sum8 = 8;
                }
                if (h == 3) {
                    sum8 = 7;
                }
                if (h == 4) {
                    sum8 = 6;
                }
                if (h == 5) {
                    sum8 = 5;
                }
                if (h == 6) {
                    sum8 = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        s9.setAdapter(stringArrayAdapter);
        s9.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                i = position;
                if (i == 0) {
                    sum9 = 10;
                }

                if (i == 1) {
                    sum9 = 9;
                }
                if (i == 2) {
                    sum9 = 8;
                }
                if (i == 3) {
                    sum9 = 7;
                }
                if (i == 4) {
                    sum9 = 6;
                }
                if (i == 5) {
                    sum9 = 5;
                }
                if (i == 6) {
                    sum9 = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                total = (float) ((sum1 * 3) + (sum2 * 3) + (sum3 * 3) + (sum4 * 3) + (sum5 * 3) + (sum6 * 3) + (sum7 * 2) + (sum8 * 2) + (sum9 * 2)) / 24.0f;
                total = Math.round(total * 100.00f) / 100.00f;
                String str = String.valueOf(total);
                textView.setText(str);
                btnsave.setEnabled(true);
            }
        });

        btnsave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Toast.makeText(Main2Activity.this, "YOUR DATA IS SAVED", Toast.LENGTH_SHORT).show();
            }
        });


    }
}

0 answers