I am trying to solve this conundrum: "How to get rest of the buttons in a list go under the first button."
In order to solve that issue, I have tried to set a boolean to detect the first button and apply the rule on anything below that, but button1 seems to go on top of button0 every time.
I listed the MainActivity below. The layout xml only has a Relative layout in it.
package com.code.p2_project.finalevaluationapp;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import java.util.ArrayList;
public class MainActivity extends Activity implements View.OnClickListener {
private ArrayList<Category> categories = new ArrayList<Category>();
private ArrayList<Button> buttons = new ArrayList<Button>();
private boolean firstButton = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.mainLayout);
categories.add(new Category("Mobility",CategoryTypes.MOBILITY));
categories.add(new Category("Flexibility",CategoryTypes.FLEXIBILITY));
categories.add(new Category("Mobility",CategoryTypes.MOBILITY));
categories.add(new Category("Flexibility",CategoryTypes.FLEXIBILITY));
categories.add(new Category("Mobility",CategoryTypes.MOBILITY));
categories.add(new Category("Mobility",CategoryTypes.MOBILITY));
for (int i = 0; i < 6; i++)
{
makeButton(this, "Buttons"+i,i);
}
for (int i = 0; i < buttons.size(); i++)
{
addButtonToLayout(buttons.get(i),relLayout,i);
System.out.println(buttons.get(i).getLayoutParams());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//This method constructs a button object, sets its text, id and an OnClickListener. Finally adds the buttons to buttons arraylist
void makeButton (Context context, String buttonText, int id)
{
Button button = new Button(context);
button.setText(buttonText);
button.setId(id);
button.setOnClickListener(this);
buttons.add(button);
System.out.println(button.getId());
}
//Method to add button to layout. The LayoutParams are used to add the rule that the buttons should be underneath each other.
void addButtonToLayout (Button button, RelativeLayout layout, int index)
{
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams paramsForSecondButton = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (firstButton)
{
firstButton = false;
System.out.println(buttons.get(index).getText());
//TODO: [BUG] Doesn't add the first button for some reason.
}
else if (!firstButton)
{
params.addRule(RelativeLayout.BELOW, buttons.get(index-1).getId());
}
//paramsForSecondButton.addRule(RelativeLayout.BELOW, buttons.get(0).getId());
//params.addRule(RelativeLayout.BELOW, buttons.get(index).getId());
button.setLayoutParams(params);
// buttons.get(1).setLayoutParams(paramsForSecondButton);
layout.addView(button);
}
@Override
public void onClick(View view) {
}
Aucun commentaire:
Enregistrer un commentaire