TableLayout Trouble - java

I am making 10x10 board of buttons, however, whenever I run my program, it is just 1 column with buttons extending to the bottom until I can't see them anymore. Here's my code:
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.game);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(32, 32);
final TableLayout container = (TableLayout) findViewById(R.id.tableLayout5);
Button btn[][] = new Button[10][10];
for(int i = 0; i<10; i++){
for(int j = 0; j<10; j++){
btn [i][j] = new Button(this);
container.addView(btn[i][j],i,lp);
}
}
}
and my XML code
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout5">
<Button
android:id="#+id/newgamebutton"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/newgame" />
<TableLayout
android:id="#+id/tableLayout5"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/newgamebutton"
android:orientation="vertical" >
</TableLayout>
</RelativeLayout>
I'm wondering why the buttons are extending all the way to the bottom and disappearing, I don't know if it has something to do with my layout height or not. I intend it to look like a 10x10 board

Firstly if you need a 10x10 board of buttons you need to create TableRows as well. Currently you have none. Please read the TableLayout documentation to understand how it works.
Secondly if you do need to programmatically set layout parameters on a Table you have to use TableRow.LayoutParams, and not ViewGroup.LayoutParams.
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(32, 32);
I used this instead:
TableRow.LayoutParams lp = new TableRow.LayoutParams(32, 32);

Related

How can I make a dynamically generated tablelayout scrollable?

I have 3 fragments inside an Activity and in two of the fragments I have a hardcoded TableLayout inside a ConstraintLayout, the headers and rows of the tables are dynamically generated with information from a MySQL server. Given its dynamic nature, the table is able to overflow the page and I want it to be scrollable.
There are already multiple questions on StackOverflow where people have wanted a scrollable table, but in the questions, the tables and their data are hardcoded (don't know if that is relevant or not). I have tried the solutions proposed in those questions but they don't work on my table. The solutions are generally to either wrap the TableLayout in a ScrollView or to use android:isScrollContainer="1" neither of which have worked for me.
TableLayout inside ScrollView. The ScrollView is there for testing, normally it is just the TableLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewProductLocFragment">
<!-- Rest of the fragment -->
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:layout_weight="1"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="#+id/menuBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:stretchColumns="*"
app:layout_constraintBottom_toTopOf="#+id/menuBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view">
</TableLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Here is the code that fills the table.
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_view_product_main, container, false);
initializeTableLayout(view);
fillTable(((ViewProductActivity)getActivity()).pallets);
return view;
}
private void fillTable(PalletsResult pallets)
{
Integer count = 0;
for(Pallet pallet : pallets.getPallets()) {
String barcode = pallet.getBarcode();
String location = pallet.getCode();
Integer quantity = pallet.getQuantity();
TableRow tableRow = new TableRow(getContext());
if (count % 2 != 0) {
tableRow.setBackgroundColor(getResources().getColor(R.color.lightGrey));
}
tableRow.setId(View.generateViewId());
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView labelBarcode = new TextView(getContext());
labelBarcode.setId(View.generateViewId());
labelBarcode.setGravity(Gravity.CENTER);
labelBarcode.setTextColor(getResources().getColor(R.color.standardTextColor));
labelBarcode.setTextSize(18);
labelBarcode.setText(barcode);
tableRow.addView(labelBarcode);
TextView labelLocation = new TextView(getContext());
labelLocation.setId(View.generateViewId());
labelLocation.setGravity(Gravity.CENTER);
labelLocation.setTextColor(getResources().getColor(R.color.standardTextColor));
labelLocation.setTextSize(18);
labelLocation.setText(location);
tableRow.addView(labelLocation);
TextView labelQuantity = new TextView(getContext());
labelQuantity.setId(View.generateViewId());
labelQuantity.setGravity(Gravity.CENTER);
labelQuantity.setTextColor(getResources().getColor(R.color.standardTextColor));
labelQuantity.setTextSize(18);
labelQuantity.setText(quantity.toString());
tableRow.addView(labelQuantity);
tableLayout.addView(tableRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
count++;
}
}
private void initializeTableLayout(View view)
{
tableLayout = view.findViewById(R.id.tableLayout);
TableRow tr_head = new TableRow(getContext());
tr_head.setId(View.generateViewId());
tr_head.setBackgroundColor(getResources().getColor(R.color.lightGrey));
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT
));
TextView label_barcode = new TextView(getContext());
label_barcode.setId(View.generateViewId());
label_barcode.setText("BARCODE");
label_barcode.setTextSize(20);
label_barcode.setTextColor(Color.BLACK);
label_barcode.setGravity(Gravity.CENTER);
tr_head.addView(label_barcode);// add the column to the table row here
TextView label_location = new TextView(getContext());
label_location.setId(View.generateViewId());// define id that must be
unique
label_location.setText("LOCATION"); // set the text for the header
label_location.setTextSize(20);
label_location.setTextColor(Color.BLACK); // set the color
label_location.setGravity(Gravity.CENTER);
tr_head.addView(label_location); // add the column to the table row here
TextView label_quantity = new TextView(getContext());
label_quantity.setId(View.generateViewId());// define id that must be
unique
label_quantity.setText("QTY"); // set the text for the header
label_quantity.setTextSize(20);
label_quantity.setTextColor(Color.BLACK); // set the color
label_quantity.setGravity(Gravity.CENTER);
tr_head.addView(label_quantity); // add the column to the table row here
tableLayout.setScrollContainer(true);
tableLayout.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
This creates a table that runs off the end of the page and that can't be scrolled.
There is a fair bit of other code that I didn't post in regards to the Activity containing the fragments and what not. Let me know if you want any of that.
Thanks for any help.
Update: I implemented a similar table elsewhere in my app and it worked just fine with the code posted in my examples. This means that the problem I am having is realted to the table being in a fragment and not an activity. I will post some of my fragment code later.
I have tried implementing your code over an Activity. I am posting my Activity and XML code for your reference, can you see what you are missing in your complete code so that this can be helpful.
MainActivity.java
public class MainActivity extends Activity {
TableLayout tableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeTableLayout();
}
#Override
protected void onResume() {
super.onResume();
fillTable();
}
private void fillTable() {
Integer count = 0;
for (int i = 0; i < 100; i++) {
String barcode = "#0000000000";
String location = "Location";
Integer quantity = 1;
TableRow tableRow = new TableRow(MainActivity.this);
if (count % 2 != 0) {
tableRow.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
tableRow.setId(View.generateViewId());
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView labelBarcode = new TextView(MainActivity.this);
labelBarcode.setId(View.generateViewId());
labelBarcode.setGravity(Gravity.CENTER);
labelBarcode.setTextColor(getResources().getColor(R.color.colorAccent));
labelBarcode.setTextSize(18);
labelBarcode.setText(barcode);
tableRow.addView(labelBarcode);
TextView labelLocation = new TextView(MainActivity.this);
labelLocation.setId(View.generateViewId());
labelLocation.setGravity(Gravity.CENTER);
labelLocation.setTextColor(getResources().getColor(R.color.colorAccent));
labelLocation.setTextSize(18);
labelLocation.setText(location);
tableRow.addView(labelLocation);
TextView labelQuantity = new TextView(MainActivity.this);
labelQuantity.setId(View.generateViewId());
labelQuantity.setGravity(Gravity.CENTER);
labelQuantity.setTextColor(getResources().getColor(R.color.colorAccent));
labelQuantity.setTextSize(18);
labelQuantity.setText(quantity.toString());
tableRow.addView(labelQuantity);
tableLayout.addView(tableRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
count++;
}
}
private void initializeTableLayout() {
tableLayout = this.findViewById(R.id.tableLayout);
TableRow tr_head = new TableRow(MainActivity.this);
tr_head.setId(View.generateViewId());
tr_head.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
tr_head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView label_barcode = new TextView(MainActivity.this);
label_barcode.setId(View.generateViewId());
label_barcode.setText("BARCODE");
label_barcode.setTextSize(20);
label_barcode.setTextColor(Color.BLACK);
label_barcode.setGravity(Gravity.CENTER);
tr_head.addView(label_barcode);// add the column to the table row here
TextView label_location = new TextView(MainActivity.this);
label_location.setId(View.generateViewId());// define id that must be unique
label_location.setText("LOCATION"); // set the text for the header
label_location.setTextSize(20);
label_location.setTextColor(Color.BLACK); // set the color
label_location.setGravity(Gravity.CENTER);
tr_head.addView(label_location); // add the column to the table row here
TextView label_quantity = new TextView(MainActivity.this);
label_quantity.setId(View.generateViewId());// define id that must be unique
label_quantity.setText("QTY"); // set the text for the header
label_quantity.setTextSize(20);
label_quantity.setTextColor(Color.BLACK); // set the color
label_quantity.setGravity(Gravity.CENTER);
tr_head.addView(label_quantity); // add the column to the table row here
tableLayout.setScrollContainer(true);
tableLayout.addView(tr_head, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:layout_weight="1"
android:scrollbars="none">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:stretchColumns="*"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"></TableLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
I have manually added 100 dummy rows to test its scroll behaviour and would like to inform you that it is exactly working as it should. There could be a problem in your Activity or Fragment parent code which is preventing the table from scrolling.
You can use TableView library to make your Table scrollable with dynamic data. The link is given below:-
https://github.com/evrencoskun/TableView

Dynamically adding buttons to scroll view

I would like some help with dynamically adding buttons to a ScrollView. I've got it working with a LinearLayout but obviously I can only add so many buttons before they no longer appear on screen. My code is below with an attached image of its current state.
I tried changing every occurrence of LinearLayout with ScrollView in the code but when I ran it, I got an error that stated something along the lines of ScrollViews can only have 1 direct child.
I'm not sure how to make it work, so if someone could give me some guidance on how to do it, I would be very grateful.
My XML Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#id/imageTextView"
android:layout_centerHorizontal="true">
</LinearLayout>
</RelativeLayout>
My Java Code (where I'm dynamically creating the buttons):
public class Main5Activity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
for (int i = 0; i < 6; i++)
{
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++)
{
final Button btnTag = new Button(this);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
btnTag.setText("" + (j + 1 + (i * 4)));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
}
}
Image of the current layout.
Yes ScrollView can only have 1 child, and often it is the ViewGroup like LinearLayout, RelativeLayout, etc.
You need to wrap your LinearLayout with a ScrollView, like this:
<ScrollView>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#id/imageTextView"
android:layout_centerHorizontal="true"/>
</ScrollView>
Or you can change your topmost RelativeLayout into ScrollView if it has only one child.

Dynamically adding buttons next to each other - RelativeLayout

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);
}
}
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);
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);

How do I make an ImageButton to randomly be positioned within a linear layout? (Android)

I have a linear layout with a certain amount of padding. I want an image button to be placed randomly anywhere within that linear layout without going outside the layout. Here is my xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="45dp"
android:text="Text 1"
android:id="#+id/text1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="30dp"
android:text="Text 2"
android:id="#+id/text2" />
<LinearLayout
android:id="#+id/lay1"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_margin="20dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:src="#drawable/ib"
android:id="#+id/button1"/>
</LinearLayout>
</LinearLayout>
I want the ImageButton button1 to be placed randomly within layout lay1.
I have tried getting the width and height of the layout and feeding them into a random function which I then use to provide a left margin and top margin to the imagebutton, but my app keeps crashing whenever I do that. Here's the Java code:
ImageButton b = (ImageButton) findViewById(R.id.button1);
LinearLayout l = (LinearLayout) findViewById(R.id.lay1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) b.getLayoutParams();
int width = l.getWidth();
int height= l.getHeight();
params.leftMargin = new Random().nextInt(width);
params.topMargin = new Random().nextInt(height);
b.setLayoutParams(params);
The above method did not work and my app always crashes when I open this activity. Can someone please help me out?
Your method looks good, but you also have to take into account the image size.
So I'd put something like :
params.leftMargin = new Random().nextInt(width - b.getWidth());
params.topMargin = new Random().nextInt(height - b.getHeight());
Now you're gonna have to be more specific about the crash you're experiencing.
EDIT : Also SnyersK is right, if you're going through that piece of code before the element got their dimensions, then you'll need to use a ViewTreeObserver.
I've got a partial solution for you.
The following code places a button at a random location BUT it can be put off screen. You should be able to prevent this by playing around with the random generator.
public class MainActivity extends ActionBarActivity {
int width, height;
LinearLayout linearLayout;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.ll);
linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.v(getClass().getSimpleName(), "onGlobalLayout");
width = linearLayout.getWidth();
height = linearLayout.getHeight();
placeButton();
if (Build.VERSION.SDK_INT >= 16) {
linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
button = (Button) findViewById(R.id.button);
}
private void placeButton() {
Log.v(getClass().getSimpleName(), "width: " + width + " Height: " + height);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
Random rand = new Random();
params.leftMargin = rand.nextInt(width);
params.topMargin = rand.nextInt(height);
Log.v(getClass().getSimpleName(), "left: " + params.leftMargin + " top: " + params.topMargin);
button.setLayoutParams(params);
button.invalidate();
}
}
You just wait for your linearlayout to be drawn. Then you get it's width and height and put the button on a random place.
Note
This will move the button around when you rotate the screen. you could prevent this by only adding an OnGlobalLayoutListener if savedInstanceState == null
Just replace your code with this
ImageButton b = (ImageButton) findViewById(R.id.button1);
LinearLayout l = (LinearLayout) findViewById(R.id.lay1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)l.getLayoutParams();
int width = l.getWidth();
int height= l.getHeight();
params.leftMargin = new Random().nextInt(width);
params.topMargin = new Random().nextInt(height);
l.setLayoutParams(params);

Buttons in linearlayout appearing vertically instead of horizontally?

I'm very new to Android and I'm trying to dynamically add buttons in my android app, the problem is that they appear vertically, while this should be horizontally.
What I'm getting:
What I'm expecting (and want):
Code I'm using:
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.main_linearlayout);
for(int x = 1; x <= 5 ; x++)
{
LinearLayout tmpLinearLayout = new LinearLayout(this);
tmpLinearLayout.setOrientation(LinearLayout.VERTICAL);
tmpLinearLayout.setLayoutParams( new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f));
tmpLinearLayout.getLayoutParams().height = 200;
ll.addView(tmpLinearLayout);
for(int i = 0;i<5;i++)
{
Button tmpButton = new Button(this);
tmpButton.setText("nr:" + i +" - " + x);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
tmpLinearLayout.addView(tmpButton, lp);
}
}
}
Layout (activity_main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/main_linearlayout"
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"
tools:context=".MainActivity"
android:background="#drawable/background"
android:orientation="horizontal" >
</LinearLayout>
Can anyone explain why it does this/correct me?
Thank you!
You're programmatically setting vertical orientation.-
Replace this line
tmpLinearLayout.setOrientation(LinearLayout.VERTICAL);
with this one
tmpLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
PS: As far as I know, Horizontal is the default orientation, so you actually could just delete the orientation line.
Change the android:orientation to vertical in your xml layout.

Categories

Resources