ImageView.setImageResource(int id); is showing OutOfMemoryError.
i have a TableLayout in that i am adding TableRow pragmatically, in each row i'm setting a image, that is already in my projects resource folder, i am referencing those image withe the resource_id.
private void addItems() {
table.removeAllViews();
Random rand = new Random();
for (int i = 0; i < 20; i++) {
TableRow row = new TableRow(getActivity());
table.addView(row);
RelativeLayout profile = (RelativeLayout) getLayoutInflater(null).inflate(R.layout.buddyname, null);
TextView name = (TextView) profile.findViewById(R.id.name);
name.setText(randomNames[i]);
ImageView photo = (ImageView) profile.findViewById(R.id.photo);
photo.setImageResource(randomPhoto[new Random().nextInt(randomPhoto.length - 1)]);
row.addView(profile);
}
}
each time i am doing table.removeAllViews(); even then its not working, can i guess is it because the image resource is loading again and again on heap and not clearing the heap, can we reference the previously loaded image, is there any solution.
Buddyname? It seems like it is a contact list. If it is, use the ListView instead. It dynamically loads elements which are visible. 20 Table rows are indeed much.
Related
How to create dynamic view of some data getting from database like in below picture. In below pic, first line consist of 1 view, second line consist of 2 view similarly 3rd line has 1 view and fourth line 2 view. like in below pic.
I know well that how to create a layout using java like this code,
final int N = 10; // total number of textviews to add
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);
// set some properties of rowTextView or something
rowTextView.setText("This is row #" + i);
// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}
but i want to know how each line different to each other.
Step 1 : Along with your TextViews, create a container layout like a LinearLayout for each line programatically.
Step 2 : So each LinearLayout with a horizontal orientation should represent a Line.
Step 3 : Add your views to the LinearLayout.
Then add all the LinearLayouts into a parent view like another LinearLayout with vertical orientation
Example :
final int N = 10; // total number of Lines to add
LinearLayout llContainer = new LinearLayout(this);
llContainer.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < N; i++) {
LinearLayout llLine = new LinearLayout(this);
llLine.setOrientation(LinearLayout.HORIZONTAL);
//Make other configurations for the linear layout
//Since you want 1 and 2 views in each line alternatively you could do this
if(i%2 ==0){
//Add one view for all even lines
//Whatever view you need. Im using a textview.
TextView tv = new TextView(this);
//Make your configurations/setText etc
llLine.addView(tv);
}else{
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
llLine.addView(tv1);
llLine.addView(tv2);
}
llContainer.addView(llLine);
}
Something like this. I have not tested the above code. It is an example and it should work. Hope this helped.
Cheers!
How can I declare some ImageViews programmatically in oncreate method ? I want to have something like this:
for (int i =1 ; i<=10;i++){
ImageView image+i = (ImageView)findViewById(R.id.image+i);
}
I don't know if it is possible something like this, or you have to make it imageview by imageview.
ImageView[] imageViews = new ImageView[10];
for (int i =1 ; i<= 10; i++){
int id = getResources().getIdentifier("image"+i, "id", getPackageName());
imageViews[i - 1] = (ImageView)findViewById(id);
}
getIdentifier returns the resource's id associate to the name, the first parameter, and you could use an array to store the view's reference.
you can use something like this method
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image+i = new ImageView(this);
image+i.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image+i.setMaxHeight(20);
image+i.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
go through this tutorial and this too.
I’ve been trying to create a minesweeper game in android, and so far, all has gone accordingly. However, I’m currently stuck on the part where I have to randomly place the mines within the game board.
I’ve tried a few things that I could think of, but none of which worked, except one. However, it doesn’t give me the results that I want. Here is how I am drawing the game board (using a 2D array of buttons).
final Button currentButton = new Button(this);
final int bombState = R.drawable.bomb_state;
final Button[][] buttonArray = new Button[6][6];
final int mine = R.drawable.bomb;
final Random rand = new Random();
final int number = 36;
int button;
int row;
//create new Linear Layout
RelativeLayout linearLayout = new RelativeLayout(this);
//creating the layout Params
LayoutParams linLayoutParam = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
//set layout background
linearLayout.setBackgroundDrawable(getWallpaper());
//set LinearLayout as a root element of the screen
setContentView(linearLayout, linLayoutParam);
//create a new Table Layout for the game grid
TableLayout mineGrid = new TableLayout(this);
/*
* creates TableRows and Buttons using the for loop
* then add the buttons into the rows and the rows
* into the TableLayout
*/
for(row = 0; row < 6; row++){
//create new Table Row
TableRow currentRow = new TableRow(this);
for(button = 0; button < 6; button++){
//create new Button
for(int id = 0; id < number; id++){
currentButton.setId(id);
}
currentButton.setText(" ");
//storing the buttons into the array of Buttons
buttonArray[row][button] = currentButton;
if(currentButton.isClickable()){
currentButton.setOnClickListener(new OnClickListener() {
/*
* (non-Javadoc)
* #see android.view.View.OnClickListener#onClick(android.view.View)
*/
public void onClick(View v) {
try
{
for(int i = 0; i < 10; i++){
if(rand.nextInt(10) == i){
currentButton.setBackgroundResource(mine);
restart.setBackgroundResource(bombState);
}
}
} catch(Exception e)
{
Toast.makeText(Game.this,e.getMessage() + "Error : ",
Toast.LENGTH_SHORT).show();
}
}
});
}
//store the button into the Table Row
currentRow.addView(currentButton);
}
//add the newly created row into the table
mineGrid.addView(currentRow);
}
linearLayout.addView(score, params3);
linearLayout.addView(mineGrid, params);
}
What the above code gives me, is a 6x6 grid made up of buttons.
And the following is where I’m trying to randomly place n amount of mines within the board.
try
{
for(int i = 0; i < 10; i++){
if(rand.nextInt(10) == i){
currentButton.setBackgroundResource(mine);
restart.setBackgroundResource(bombState);
}
}
}
Unfortunately, this fills the whole board with mines, instead of only placing n amount of mine on the board. I know am missing something when I try to randomly set the mines! Can anyone advise me as to where I’m going wrong and help point me in the right direction?
Please ask me anything for clarification.
Thanks in advance.
You basically, on every click of a button try to place a mine instead placing them when you create buttons. Maybe You could add to a list, id of a buttons which are mines and only check if user has clicked on one of those buttons.
ArrayList<Integer> mines = new ArrayList<Integer>();
.
.
.
currentButton.setText(" ");
if(rand.nextInt(2)==1)
mines.add(currentButton.id);
and in onClick() You check if currentButton.id is in mines list and if it is, display appropriate image.
On my code i need to create some table that each column contain Bitmap and text - and i create this table dynamically.
So i doing this by using this code:
for (int i = 0; i < collection.size(); i++)
{
ObjectITem item = collection.get(i);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView textView = new TextView(this);
textView.setText(item.getText());
linearLayout.addView(textView);
linearLayout.addView( (new ImageButton(this)).setImageBitmap(item.getBitmap());
layout.addView(linearLayout, LayoutParams.WRAP_CONTENT);
}
this code is working good - but because the originaly images are not in same size - i see that the images that appear on the ImageButton are look not good.
How can i make all the images on the ImageButton to look the same ?
Is there some better way to make the layout that i did here ?
1) You can use Bitmap.createScaledBitmap() to scale all the images to a desired size. Like this...
Bitmap buttonBmp = Bitmap.createScaledBitmap(item.getBitmap(), buttonW, buttonH, true);
linearLayout.addView((new ImageButton(this)).setImageBitmap(buttonBmp);
2) You might want to consider using a ListActivity and a ListView. This is more complicated, but may be more efficient if your collection is large and needs to scroll over many pages.
First off I am new to android development and some seemingly easy tasks are frustrating me to no end. This is a follow on question to what was answered here: Changing the checkbox size in Android 2.0
The gist of it is that for some reason there is this extra space that keeps showing up next to my checkboxes (See Below).
Now I have tried to set the width to 1 and it doesn't get any shorter than what is shown. I have tried to set the width bigger and I can make the size bigger, but no shorter.
Please take a look at the code below and let me know what you see.
TableLayout table = (TableLayout) findViewById(R.id.timeEntriesTable);
for (int i = 0; i < timeEntries.getLength(); i++)
{
Element element = (Element) timeEntries.item(i);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
CheckBox box = new CheckBox(this);
box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_off_background));
box.setPadding(0,0,0,0);
box.setWidth(1);
box.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox box = (CheckBox) v;
if (box.isChecked())
{
box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_on_background));
}
else
{
box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_off_background));
}
}
});
TextView dateBox = new TextView(this);
String dateText = element.getAttribute("date");
dateBox.setText(dateText);
dateBox.setTextColor(Color.BLACK);
TextView customerBox = new TextView(this);
String customerName = element.getAttribute("customer");
customerBox.setText(customerName);
customerBox.setTextColor(Color.BLACK);
TextView hoursBox = new TextView(this);
String hours = element.getAttribute("hours");
hoursBox.setText(hours);
hoursBox.setTextColor(Color.BLACK);
TextView test = new TextView(this);
test.setTextColor(Color.RED);
test.setText("Test");
tr.addView(box);
tr.addView(dateBox);
tr.addView(customerBox);
tr.addView(hoursBox);
table.addView(tr, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
Thank you in advance!
That looks suspiciously like the "Time Entries" header is in its own table cell in a header row and the column of checkboxes below it is matching the same width. (See how the right edge of the text lines up perfectly with the date below it?) Since the checkbox's layout_gravity is likely defaulting to TOP|LEFT, it's being positioned in the upper left of the cell.
Try altering either the header row or the layout_gravity of the checkboxes.
So after the input of the two other answerers #adamp and #Phobos. I stumbled across what worked for me.
How to set (Text)View attributes programmatically?
By adding this:
TableRow.LayoutParams lp = new TableRow.LayoutParams();
lp.width = 15;
lp.setMargins(3, 5, 0, 0);
lp.height = 15;
tr.addView(box, lp);
I was able to tweak that cell exactly how I wanted it. It was very strange to me that it wasn't growing the column of the cell it was growing the checkbox itself. This is because in Android there are no actual cells, it uses the views and their sizes to generate the table apparently.
I have came across no tutorials or anything that has you set the LayoutParams programmatically that way. Thank you for your help troubleshooting this!
Table Layouts naturally want to make all the cells the same size. If you want one, or more, to be different sizes then that takes additional parameters.
To shrink all the extra space in your cells add this to your table object
table.setShrinkAllColumns(true);
Or to just shrink the first column where your checkbox is
table.setColumnShrinkable(int columnIndex, boolean isShrinkable)