I want to add some switches in a linear layout (declared as a view, not as a LinearLayout). I tried this, but it gets me an error:
numberDevices = 3; //This is going to be used after
Switch[] switches = new Switch[numberDevices];
for (int i = 0; i < numberDevices; i++) {
switches[i].setTextOn("ON");
switches[i].setTextOff("OFF");
switches[i].setId(i);
((LinearLayout) linearLayout).addView(switches[i]);
}
Any idea?
You haven't created any Switches, you've just made an empty array. You need to create the switches first:
for (int i = 0; i < numberDevices; i++) {
switches[i] = new Switch(linearLayout.getContext());
...
}
Related
I have an activity that should display X RelativeLayouts, where X is the length of a JSON array. How can I do this?
So far, I have tried this:
RelativeLayout[] interestLayouts = new RelativeLayout[jarr.length()];
for (int interest = 0; interest < jarr.length(); interest++) {
interestLayouts[interest] = (RelativeLayout) view.findViewById(R.id.X); // I don't know how to determine X
}
You can add the Relative Layout dynamically.
Linear LAYOUT; // suppose this is the layout where you want to add relativelayout
for (int i =0;i < jsonarray.length; i++){
Relative rLayout = new RelativeLayout(this);
LAYOUT.addView(rLayout);
}
something like this
I want to create a bunch of switches in Android.
for (int i = 0; i < 12; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 1; j++) {
Switch switchTag = new Switch(this);
switchTag.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
switchTag.setText("");
switchTag.setId(j + 1 + i);
row.addView(switchTag);
});
}
layout.addView(row);
But I have not been able to find a way to call the switches so I can do things with them. Usually, if I have defined a switch, I can make statements like
myswitch.setOnClickListener(this);
But that doesn't work if the switch's name is an integer
1.setOnClickListener(this);
My Question
Is .setId() the most efficient way to create a bunch of objects in Android programmatically?
If it is, how do I call the switches so that I can do things to them? Calling them by integer doesn't work.
I am creating UI layout dynamically inside the RecyclerView which causes out of memory issue when the application is used for long time.Ui being re created on scrolling up and down. How can i prevent the creation of views on scrolling? my code looks as follows.
LinearLayout container = (LinearLayout) getBaseView().findViewById(R.id.container);
container.removeAllViews();
int position = 0;
for (int i= 0; i<content.size()/2;i ++) {
LinearLayout ln = new LinearLayout(getBaseView().getContext());
ln.setWeightSum(2);
ln.setGravity(Gravity.CENTER);
for (int j = 0; j<COLUMN_COUNT; j++) {
VIOPlayListItemView tile = new VIOPlayListItemView(getBaseView().getContext());
tile.setTag(position);
tile.setGravity(Gravity.CENTER);
ln.addView(tile);
if (position <content.size()) {
tile.setData(content.get(position));
}
position ++;
}
container.addView(ln);
}
}
I recently started making a chess-like game for android. The first thing I needed was to make the 8 by 8 board. I figured adding 64 buttons and organizing them in the XML wouldn't be much efficient, so I found a way to create them programmatically using a simple 8x8 matrix of buttons. Until this point, everything worked as intended, and I had this:
The next thing I tried was to change the colors of the buttons to match a chessboard. On the internet I found some ways of doing it, but pretty much all of them just made my buttons invisible, and did not change their color.
Here's the onCreate method (the only thing I modified so far):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
//GETTING SCREEN DIMENSIONS
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
//SETTING THE BOARD
int TILESIZE = width/8;
LinearLayout back = (LinearLayout) findViewById(R.id.back);
LinearLayout[] rows = new LinearLayout[8];
Button[][] tiles = new Button[8][8];
for(int i = 0; i < 8; i++){
rows[i] = new LinearLayout(this);
back.addView(rows[i]);
for(int j = 0; j < 8; j++){
tiles[i][j] = new Button(this);
tiles[i][j].setWidth(TILESIZE);
tiles[i][j].setHeight(tiles[i][j].getWidth());
if((i + j) % 2 == 0){
tiles[i][j].setBackgroundColor(0xFFFFFFFF);
tiles[i][j].invalidate();
}
else{
//TODO: Make tiles black
}
rows[i].addView(tiles[i][j]);
}
}
}
The XML file contains a single vertical linear layout called back.
My question is how can I make the buttons change color, and where am I doing something wrong. I would also gladly accept alternative (or better) ways to make the board.
Change your loop like this and try
for(int i = 0; i < 8; i++){
rows[i] = new LinearLayout(this);
for(int j = 0; j < 8; j++){
tiles[i][j] = new Button(this);
tiles[i][j].setWidth(TILESIZE);
tiles[i][j].setHeight(tiles[i][j].getWidth());
if((i + j) % 2 == 0){
tiles[i][j].setBackgroundColor(0xFFFFFFFF);
tiles[i][j].invalidate();
}
else{
//TODO: Make tiles black
}
rows[i].addView(tiles[i][j]);
}
back.addView(rows[i]);
}
Change your if loop like this:
if((i + j) % 2 == 0)
tiles[i][j].setBackgroundColor(android.R.color.holo_blue_dark);
else
tiles[i][j].setBackgroundColor(android.R.color.holo_red_dark);
You can define black and white colors in your color.xml file and add them instead using tiles[i][j].setBackgroundColor(getResources().getColor(R.color.white)); and similarly for black.
But make sure, you use a different background so they are clearly visible.
In my onCreateView i have the following codes to populate custom view into my LinearLayout whereby StageViewer extends View.
layout = (LinearLayout)rootView.findViewById(R.id.lo1);
for (int i = 0 ; i < 6 ; i ++)
{
StageViewer viewer = new StageViewer(i,callingActivity,this, 0, Global.STAGE_VIEW);
layout.addView(viewer);
stageList.add(viewer);
}
In my application the user have a option to set all view to default mode
public void resetToDefault()
{
for (int i = 0 ; i < 6 ; i ++)
{
StageViewer viewer = new StageViewer(i,callingActivity,this, 0, Global.STAGE_VIEW);
layout.addView(viewer);
stageList.add(viewer);
}
}
However upon reset the new view are NOT viewable (an empty layout is shown).
I tried
viewer.invalidate();
layout.refreshDrawableState();
Did I miss out something ?
public void resetToDefault()
{
layout.removeAllViews();
stageList.clear();
for (int i = 0 ; i < 6 ; i ++)
{
StageViewer viewer = new StageViewer(i,callingActivity,this, 0, Global.STAGE_VIEW);
layout.addView(viewer);
stageList.add(viewer);
}
}
Maybe layout params are missing?
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
viewer.setLayoutParams(layoutParams);
layout.addView(viewer);