2

Spinners need to be Right aligned on the Screen

I tried to use everything like setGravity and other, Nothing worked.

I tried to search also a lot for this Right alignment, everywhere solution is available in XML format but NOT in Java format. Can someone Please help :)

Spinner needs to be Right side of the screen

Code

        final LinearLayout lm = (LinearLayout) findViewById(R.id.linearLayout1);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);

        final Spinner spinner = new Spinner(this);
        spinner.setOnItemSelectedListener(this);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
        spinner.setEnabled(false);
        ll.addView(spinner);
        lm.addView(ll); 

Abir : Now its showing this.

enter image description here

Bharat
  • 750
  • 1
  • 9
  • 20

3 Answers3

1

I think this should solve your problem:

    final LinearLayout lm = (LinearLayout) findViewById(R.id.linearLayout1);
    LinearLayout ll = new LinearLayout(this);
    ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    ll.setGravity(Gravity.RIGHT);
    ll.setOrientation(LinearLayout.HORIZONTAL);

    final Spinner spinner = new Spinner(this);
    spinner.setOnItemSelectedListener(this);
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, categories);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);
    spinner.setEnabled(false);
    ll.addView(spinner);
    lm.addView(ll);    

You should set your dynamic linearLayout's width and height. and then set its gravity to RIGHT. Then your spinner will be aligned on the right side of the view.

Abir Hasan Shawon
  • 6,069
  • 5
  • 17
  • 28
1

I hope this will work for you. It's a bit complex, cause it has multiple nested views. But this will solve the issue and i think you will get the view you want.

    final LinearLayout lm = (LinearLayout) findViewById(R.id.linearLayout1);
    LinearLayout ll = new LinearLayout(this);
    ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    ll.setWeightSum(1);
    ll.setOrientation(LinearLayout.HORIZONTAL);


    final CheckBox box = new CheckBox(this);
    box.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, .1f));

    final TextView tv = new TextView(this);
    tv.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, .3f));
    tv.setText("STD_MOBILE");

    LinearLayout l2 = new LinearLayout(this);
    l2.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, .6f));
    l2.setGravity(Gravity.RIGHT);
    l2.setOrientation(LinearLayout.HORIZONTAL);


    final Spinner spinner = new Spinner(this);
    spinner.setOnItemSelectedListener(this);
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, categories);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(dataAdapter);
    spinner.setEnabled(false);


    l2.addView(spinner);

    ll.addView(box);
    ll.addView(tv);
    ll.addView(l2);
    lm.addView(ll);    

I have used weight to specify the exact space for your childViews. Then made the Spinner's parent layout's gravity to right.

Abir Hasan Shawon
  • 6,069
  • 5
  • 17
  • 28
0

You have to use a custom view here to give alignment to the Spinner values. Create a CustomView like this answer has created and add android:gravity to the TextView as right.

Set the CustomView to your adapter using

adapter.setDropDownViewResource(android.R.layout.your_custom_created_view);
Community
  • 1
  • 1
Mohith P
  • 585
  • 5
  • 14