-1

I have seen some explanations on how to add two views next to each other in LinearLayout by using XML. How can this be done programatically?

I have the following two methods:

private void createListOfInstalledApplications() {
    installedApplicationNames = this.getApplicationNames();
    installedPackages = this.getPackageNames();

    for(int i = 0; i < installedApplicationNames.size(); i++){
        TextView scrollAppsView = new TextView(this);
        scrollAppsView.setId(i);
        scrollAppsView.setText(installedApplicationNames.get(i) + "\n ");
        layout.addView(scrollAppsView);
    }

}

This method adds the names of installed applications on the Android phone to the LinearLayout. On the right side of these applications, I would like to have a drop down menu where some settings can be chosen. For now the following methods adds the Spinners to the LinearLayout:

private void createDropDownMenuesForApplications() {
    List<String> vibPatternNames = new ArrayList<String>();
    VibrationPatternManager vibPatManager = VibrationPatternManager.getInstance();

    List<VibrationPattern> vibrationPatterns = vibPatManager.getAllAvailablePatterns();

    for(VibrationPattern vibrationPattern : vibrationPatterns){
        vibPatternNames.add(vibrationPattern.getName());
    }

    for(int i = 0; i < installedApplicationNames.size(); i++){
        Spinner dropDownMenu = new Spinner(this);
        dropDownMenu.setId(i);

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, vibPatternNames);
        dropDownMenu.setAdapter(dataAdapter);


        layout.addView(dropDownMenu);
    }
}

The drop-down menus now appear below the application names. However, I would like them to appear to the right. So "ApplicationName1 drop-down-menu1"

instead of

  • ApplicationName1
  • ApplicationName2
  • Drop-down-menu1
  • Drop-down-menu2

How do I do this?

plase
  • 361
  • 1
  • 3
  • 9
Nora
  • 1,825
  • 8
  • 31
  • 47

3 Answers3

1

Just do

layout.setOrientation(Horizontal)
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Amol Jindal
  • 106
  • 4
0

I have done similar task as required by you, please have a look at my answer: https://stackoverflow.com/a/33339420/5479863

in JSON response image URLs were provided so, at runtime I have to add views dynamically adjacent to each other.

Look at the answer to know more...

I hope that will help you...

Happy coding :)

Abdul Aziz
  • 442
  • 5
  • 12
  • why not use a horizontal *layout manager? This way you only need to design 1 ImageView and the layout manager will put them next to next – Zun Jun 18 '18 at 11:39
0

Try the following:

1) MActivity.class--------------

public class MActivity extends AppCompatActivity {

private LinearLayout layout_horizontal;
private LinearLayout layout_vertical;
private LayoutInflater layoutInflater;
private FrameLayout fl;

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

    // Frame layout generic container --- root.
    // Frame layout has as a direct child layout_vertical (Linear layout with orientation vertical).
    // for each item inside installedApplicationNames, re-create a linear layout with orientation horizontal (layout_horizontal),
    // with a TextView and Spinner and then add it as a direct child of the linear layout with orientation vertical.

    fl = (FrameLayout) findViewById(R.id.fl);
    layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.layout_vertical , null);
    fl.addView(view);
    layout_vertical = (LinearLayout) view.findViewById(R.id.ll_v);
    populate();
}

private void populate() {

    layout_vertical.removeAllViews();

    List<String> installedApplicationNames = new ArrayList<String>();
    for (int k = 0; k < 30; k++) {
        installedApplicationNames.add("N" + k);
    }

    for (int i = 0; i < installedApplicationNames.size(); i++) {

        View view1 = layoutInflater.inflate(R.layout.layout_horizontal , null);
        layout_horizontal = (LinearLayout) view1.findViewById(R.id.ll_h);

        TextView scrollAppsView = new TextView(this);
        scrollAppsView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT));
        scrollAppsView.setId(i);
        scrollAppsView.setText(installedApplicationNames.get(i) + "\n ");
        layout_horizontal.addView(scrollAppsView);

        // TODO: use a certain method to get vibPatternNames for the current installedApplicationName
        List<String> vibPatternNames = new ArrayList<String>();
        for (int j = 0; j < 4; j++) {
            vibPatternNames.add("S" + j);
        }

        Spinner dropDownMenu = new Spinner(this);
        dropDownMenu.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dropDownMenu.setId(i);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, vibPatternNames);
        dropDownMenu.setAdapter(dataAdapter);

        layout_horizontal.addView(dropDownMenu);

        layout_vertical.addView(layout_horizontal);
    }

}

}

2) layout.xml-----------

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginTop="5dp"
    android:id="@+id/fl">
</FrameLayout>

</ScrollView>

3) layout_vertical.xml-----------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ll_v"
android:layout_gravity="center"
android:orientation="vertical">


</LinearLayout>

4) layout_horizontal.xml---------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/ll_h"
android:layout_gravity="center"
android:layout_height="match_parent"
android:orientation="horizontal">

</LinearLayout>
Brainnovo
  • 1,749
  • 2
  • 12
  • 17