1

Ok, so here's the thing. I'm trying to make an app that resembles a piano for android, also I've never really had much experience with Java or programming for Android so all of this is pretty new to me. I've managed to do this in XML but I want to make it programmaticaly so I can easily add more white and black keys also dependant of screen size. In XML it looks like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
    android:id="@+id/white1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white2"
    android:layout_toRightOf="@+id/white1"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white3"
    android:layout_toRightOf="@+id/white2"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white4"
    android:layout_toRightOf="@+id/white3"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white5"
    android:layout_toRightOf="@+id/white4"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white6"
    android:layout_toRightOf="@+id/white5"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white7"
    android:layout_toRightOf="@+id/white6"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginStart="-10dp"
    android:layout_marginEnd="-6dp"
    android:background="#000"
    android:id="@+id/black1"
    android:layout_toRightOf="@+id/white1"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginLeft="-6dp"
    android:layout_marginRight="-10dp"
    android:background="#000"
    android:id="@+id/black2"
    android:layout_toRightOf="@+id/white2"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginLeft="-10dp"
    android:layout_marginRight="-6dp"
    android:background="#000"
    android:id="@+id/black3"
    android:layout_toRightOf="@+id/white4"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginLeft="-8dp"
    android:layout_marginRight="-8dp"
    android:background="#000"
    android:id="@+id/black4"
    android:layout_toRightOf="@+id/white5"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginLeft="-6dp"
    android:layout_marginRight="-10dp"
    android:background="#000"
    android:id="@+id/black5"
    android:layout_toRightOf="@+id/white6"/>

And now I wanted to recreate it programmaticaly, at first I've tried linear approach but first of all I was unable to make more than 7 keys, and I didn't really knew how to make black keys on top of that. So now I've went with RelativeLayout and all is fine as long as I create two buttons, then it works fine, one is next to another. But when I try to create more than two buttons they kinda make a stack.

I was trying to make some sort of array of buttons so I could easily make a loop to create destined number of buttons. Also I wanted to change the width of buttons, so if I create 8 buttons the would have the width of screen_width/8 but I'm not quite sure if it makes any sense since it's actually not doing anything when uncommented.

I would be grateful for any tips :)

public class MainActivity extends AppCompatActivity {

final int[] whitelist = {R.id.whitebt1,R.id.whitebt2,R.id.whitebt3,R.id.whitebt4,R.id.whitebt5,
        R.id.whitebt6,R.id.whitebt7,R.id.whitebt8};
Button[] whiteKeys = new Button[whitelist.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;



    final RelativeLayout pianoLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams whiteKeyParams1 =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


    whiteKeys[0] = new Button(this);
    whiteKeys[0].setId(View.generateViewId());
    //whiteKeys[0].setHeight(height);
    //whiteKeys[0].setWidth(width/8);
    whiteKeys[0].setLayoutParams(whiteKeyParams1);
    pianoLayout.addView(whiteKeys[0]);


    whiteKeys[1] = new Button(this);
    whiteKeys[1].setId(View.generateViewId());
    //whiteKeys[i].setHeight(height);

    RelativeLayout.LayoutParams whiteKeyParams2 =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    whiteKeyParams2.addRule(RelativeLayout.RIGHT_OF, whiteKeys[0].getId() );
    whiteKeys[1].setLayoutParams(whiteKeyParams2);
    pianoLayout.addView(whiteKeys[1]);

    //HERE'S IS THE MOMENT WHERE I TRY TO ADD THIRD BUTTON AND THE BUTTONS START TO PILE UP
    /*
    whiteKeys[2] = new Button(this);
    whiteKeys[2].setId(View.generateViewId());
    //whiteKeys[i].setHeight(height);

    //RelativeLayout.LayoutParams whiteKeyParams2 =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    whiteKeyParams2.addRule(RelativeLayout.END_OF, whiteKeys[1].getId());
    whiteKeys[2].setLayoutParams(whiteKeyParams2);
    pianoLayout.addView(whiteKeys[2]);*/

    this.setContentView(pianoLayout);
    }
}
Danmcrea
  • 15
  • 6

2 Answers2

0

You can add 8 same size buttons using weightsum and layoutweight with LienarLayout with horizontal orientations.

see below code it may help you to add same size buttons dynamically.

  /* Add a new Linearlayout as a container for the buttons */
            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            //Added Weight Sum 8 in LinearLayout
            linearLayout.setWeightSum(8);
            /* Create a new Buttons in this container, for the status bar */
            //below LayoutParams define with weight 1 for buttons.
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
            Button button1 = new Button(linearLayout.getContext());
            button1.setLayoutParams(param);

            Button button2 = new Button(linearLayout.getContext());
            button2.setLayoutParams(param);

            Button button3 = new Button(linearLayout.getContext());
            button3.setLayoutParams(param);

            Button button4 = new Button(linearLayout.getContext());
            button4.setLayoutParams(param);

            Button button5 = new Button(linearLayout.getContext());
            button5.setLayoutParams(param);

            Button button6 = new Button(linearLayout.getContext());
            button6.setLayoutParams(param);

            Button button7 = new Button(linearLayout.getContext());
            button7.setLayoutParams(param);

            Button button8 = new Button(linearLayout.getContext());
            button8.setLayoutParams(param);
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • That's great :). But I do have a question, how do I add another keys on top of that? I mean like in a piano, is it possible to do it the way I did in XML but with LinearLayout? Or maybe I have to create a relative layout and inside it two linearlayouts one which would be the white keys on the bottom, and second one for black keys on top? – Danmcrea Apr 10 '17 at 14:23
0

With your approach before adding the view to parent layout you will have to add margins for every new key also which will prevent stacking one key over another.

params.setMargins(left, top, right, bottom);

  • It doesn't really work for me. I mean it works as long as there are only two buttons. But when I try to add the third all of them stack up. – Danmcrea Apr 10 '17 at 14:19