So, I'm trying to make a column of buttons. After googling for a solution, the best I can find is to place it into a LinearLayout or something of the kind, but my problem is that I'm using fragments (specifically navigation type "Tabs + Swipe") and it doesn't use a layout as far as I can see. (well, OK, it does, but not on the pageviewer) To get a single button I modified the generated code to look like
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
final TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
final Button button = (Button) rootView.findViewById(R.id.button1);
button.setText("Btn Text");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
After consulting the guys over at Reddit/LearnProgramming I was informed instead of making an XML for every button that I should programmatically make the buttons and change the layout Params. i was a bit confused and lost their help, so I went back to google. After that, my code now looks like:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
for (int i=0;i<4;i++){
final Button newButton = new Button(getActivity());
newButton.setText("This is some text for"+i);
newButton.setTag(i);
arrList.add(newButton);
// ((ViewGroup) rootView).addView(newButton); // Works for a single button
((ViewGroup) rootView).addView(newButton, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
newButton.setText("I changed it.");
}
});
}
return rootView;
}
What this results in is a default textview number stating what page I'm on and four buttons that overlap. I know it overlaps and that they are all being displayed because I can see the numbers, 1-4 overlapped. I'm not new to programming, but I am fairly new to android programming.
What I would LIKE to happen is for the buttons to be added beneath each other, in a row. Can anyone push me in the right direction? I made this as clear as possible, but if you need any more information I would be happy to add it. Thanks in advance.
Edit: Here's the fragment_main_dummy.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity$DummySectionFragment" >
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/MyLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/section_label"
android:layout_marginTop="16dp"
android:layout_toRightOf="@+id/section_label"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
So it is relative, but I don't see a reference to RelativeLayout anywhere in the code, so I was lead to believe that it wasn't being used? Also, I tried adding a LinearLayout as a child in the xml, but I haven't done anything with it.
Try this:
for (int i=0;i<4;i++){
final Button newButton = new Button(getActivity());
newButton.setText("This is some text for"+i);
newButton.setTag(i);
arrList.add(newButton);
newButton.setId(1000 + i);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, i == 0 ? R.id.MyLayout : newButton.getId() - 1);
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
newButton.setText("I changed it.");
}
});
((RelativeLayout) rootView).addView(newButton, rlp);
}