Change the background of every new button - java

for (int j = 0; j < 2; j++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout underRow = new LinearLayout(this);
underRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int i = 0; i < MAX_BUTTONS; i++) {
// String buttonID = "btn" + j + i;
// int resID = getResources().getIdentifier(buttonID, "id","com.project.beacontreetech.doublecheckversion1");
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
button = (Button) getLayoutInflater().inflate(R.layout.button_layout, buttonsContainer, false);
// TextView textView = (TextView) getLayoutInflater().inflate(R.layout.text_view, buttonsContainer,false);
int id1 , id2 ;
id1 = i;
id2 = j;
StringBuilder append = new StringBuilder();
append.append(id1);
append.append(id2);
int imgArrLength = imageList.length;
for ( t=0; t < imgArrLength; t++){
}
String yo = (String.valueOf(append));
int id = Integer.valueOf(String.valueOf(append));
button.setHeight(buttonSize);
button.setWidth(buttonSize);
button.setOnClickListener(this);
button.setId(id);
if (button.getId() == 01){
button.setBackgroundResource(imageList[1]);
}

For set background image for button which is in drawable folder then use below code:
button.setBackgroundResource(R.drawable.new_todo_image);
For set color background you should use this:
button.setBackgroundColor(getResources().getColor(R.color.Green));
your color.xml look like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="red">#f00</drawable>
<drawable name="green">#0f0</drawable>
<drawable name="gray">#ccc</drawable>
</resources>

if (button.getId() == 01){
//to set background image on button
button.setBackgroundResource(R.drawable.your_image);
//or set background color use
button.setBackgroundColor(getResources().getColor(R.color.your_color));
}

Related

Setting negative margin doesn't work on Android

I'm creating a list of ImageView in my android app with negative margins set for each.
I'm using this to convert dp to pixels:
public int dpToPixel(float dp) {
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
return (int)((dp * displayMetrics.density) + 0.5);
}
And the List is like this:
List<GridLayout> columnNLinesGrid = new ArrayList<GridLayout>();
List<ImageView> columnLines = new ArrayList<ImageView>();
for (int i = 1; i <= rowLayoutsCount; i++) {
columnNLinesGrid.add(new GridLayout(this));
GridLayout.LayoutParams colGridParam = new GridLayout.LayoutParams();
colGridParam.setMargins(0,0,0,0);
colGridParam.topMargin = dpToPixel(-10);
columnNLinesGrid.get(i - 1).setLayoutParams(colGridParam);
columnNLinesGrid.get(i - 1).setColumnCount(columnLayoutsCount);
linearLayouts.get(i - 1).addView(columnNLinesGrid.get(i - 1));
for (int j = 1; j <= columnLayoutsCount; j++) {
columnLines.add(new ImageView(this));
GridLayout.LayoutParams lineParam = new GridLayout.LayoutParams();
lineParam.rowSpec = GridLayout.spec(0);
lineParam.columnSpec = GridLayout.spec(j - 1);
if(j-1>0){
lineParam.leftMargin = (int) dpToPixel(23);
} else{
lineParam.leftMargin = 0;
}
lineParam.width = GridLayout.LayoutParams.WRAP_CONTENT;
lineParam.height = (int) dpToPixel(35);
columnLines.get(colLinesCount).setLayoutParams(lineParam);
columnLines.get(colLinesCount).setImageResource(R.drawable.linevert);
columnNLinesGrid.get(i - 1).addView(columnLines.get(colLinesCount));
colLinesCount++;
}
}
It seems like colGridParam.topMargin = dpToPixel(-10) is not working. I tried negative margins in some other cases and neither of them worked.

Objects added to a tablerow don't show up?

I'm trying to add some EditTexts to a TableRow programmatially, yet they don't show up on the screen. If I add the EditTexts directly to the TableLayout it works fine but I do want to have them in one row afterall...
public void createPlayers(){
EditText[] editTextSpieler = new EditText[anzahl_Spieler];
TableRow tableRow = new TableRow(getActivity());
tableRow.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableLayout.addView(tableRow);
for(int i = 0; i < anzahl_Spieler; i++){
editTextSpieler[i] = new EditText(getActivity());
editTextSpieler[i].setLayoutParams(layoutparams);
tableRow.addView(editTextSpieler[i]);
editTextSpieler[i].getLayoutParams().width = dWidth/(anzahl_Spieler + 1);
editTextSpieler[i].setHint("Name");
}
}
Thanks in advance.
Solution:
layoutparams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
specifying MATCH_PARENT and WRAP_CONTENT with TableRow.LayoutParams did the job
Can you try this change.
editTextSpieler[i].getLayoutParams().width = dWidth/(anzahl_Spieler + 1);
editTextSpieler[i].setHint("Name");
//Move to last line in the loop.
tableRow.addView(editTextSpieler[i]);

How to set Width and Height of a dynamically created Table Layout Column(Cell)

In my project, I'm creating tables dynamically. Each table is a weekly schedule for college students. So, each cell has an String in it. My working code is below;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundColor(Color.BLACK);
// 2) create tableRow params
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(width/9,height/12);
tableRowParams.setMargins(1, 1, 1, 1);
tableRowParams.weight = 1;
for(int m=0;m<major.size();m++) {
for (int i = 0; i <rowCount; i++) {
// 3) create tableRow
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundColor(Color.BLACK);
for (int j = 0; j <columnCount; j++) {
// 4) create textView
TextView textView = new TextView(this);
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
WeeklySchedule ws=major.get(m);
String course;
if (i == 0 && j == 0) {
textView.setText(" ");
} else if (i == 0) {
textView.setText(cv[j-1]);
textView.setBackgroundColor(Color.GREEN);
} else if (j == 0) {
textView.setText(rv[i -1]); textView.setBackgroundColor(Color.GRAY);
} else if(j!=0&&i!=0){
if(ws.table[i-1][j-1].size()==2){
course=ws.table[i-1][j-1].get(0).getCourseCode()+"/"+ws.table[i-1][j-1].get(1).getCourseCode();
textView.setBackgroundColor(Color.RED);
}
else if(ws.table[i-1][j-1].size()==0){
course= " ";
}
else {
course=ws.table[i-1][j-1].get(0).getCourseCode();
}
textView.setText(course);
}
// 5) add textView to tableRow
tableRow.addView(textView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
It is working but the result is like that;
here
How can i fixed column height and get rid of these black lines.
Please help!
Thanks a lot for advice

Is it possible to declare muliple variables with a for loop?

I need to declare multiple ImageViews with a similair name scheme. I was wondering if there's a way of doing this without a separate line of code for every variable. Right now i'm doing it like this:
ImageView letterView1 = (ImageView) this.findViewById(R.id.letter1);
ImageView letterView2 = (ImageView) this.findViewById(R.id.letter2);
ImageView letterView3 = (ImageView) this.findViewById(R.id.letter3);
ImageView letterView4 = (ImageView) this.findViewById(R.id.letter4);
ImageView letterView5 = (ImageView) this.findViewById(R.id.letter5);
ImageView letterView6 = (ImageView) this.findViewById(R.id.letter6);
ImageView letterView7 = (ImageView) this.findViewById(R.id.letter7);
ImageView letterView8 = (ImageView) this.findViewById(R.id.letter8);
ImageView letterView9 = (ImageView) this.findViewById(R.id.letter9);
ImageView letterView10 = (ImageView) this.findViewById(R.id.letter10);
ImageView letterView11 = (ImageView) this.findViewById(R.id.letter11);
ImageView letterView12 = (ImageView) this.findViewById(R.id.letter12);
ImageView letterView13 = (ImageView) this.findViewById(R.id.letter13);
ImageView letterView14 = (ImageView) this.findViewById(R.id.letter14);
Consider writing
ImageView letterViews = new ImageView[14];
for (int n = 0; n < 14; ++n){
letterViews[n] = (ImageView) this.findViewById(R.id.letters[n]);
}
where I have also anticipating a similar refactoring job on the object to which id is a reference: I've collapsed the fields letter1, letter2, etc. into an array called letters using a declaration similar to that for letterViews.
Note that arrays in Java are zero-based: i.e. one of size 14 is indexed with 0 to 13.
It does seem inefficient that you have declared 14 ImageViews on one page, but if you really need that for your purpose, this code will work fine for you:
for (int i = 1; i <= 14; i++) {
String imageViewId = "letter" + i;
int resId = getResources().getIdentifier(imageViewId, "id", getPackageName());
ImageView imageView = findViewById(resId);
// do what you need to do here
}
P.S. There's probably a better way to do what you need to do instead of declaring 14 ImageViews.
If you can pass "R.id.letterX" as a String: easily:
List<ImageView> views = new ArrayList<ImageView>();
for ( int i = 0; i < length; i++){
views.add((ImageView)this.findViewById("R.id.letter" + (i+1));
}
so, just rewrite your findViewById method to take a String as parameter
If you need a key on every ImageView you can also using a map.
Map<Integer, ImageView> map = new HashMap<>();
for ( int i = 0; i < 14; i++){
map.put(i, (ImageView) this.findViewById("R.id.letter" + (i+1)));
}
And you get your ImageView by key:
ImageView imageView = map.get(1);

Overlapping buttons in android

In android, I am trying to add buttons programatically, but all the buttons that are added are overlapping. The code I am using is somewhat like this:
for(int i = (int) 'a'; i <= (int) 'z'; i++)
{
Button button = new Button(this);
char letter = (char)i;
String letterOnButton = Character.toString(letter);
button.setText(letterOnButton);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.dynbuttons);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rl.addView(button,lp);
}
It does not throw a button, but I only get to see the "z" button.
Any idea on how to fix this?
As mentioned above LinearLayout would be the best solution, but if u still want to use RelativeLayout, try setting an id to each button and inflate the subsequent with the parameter RIGHT_OF/BELOW..as suggested above, parameter "layout_alignLeft" will produce the same effect, i.e inflate all buttons in the same position
RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
int id = 0;
for (int i = (int) 'a'; i <= (int) 'z'; i++) {
Button button = new Button(this);
char letter = (char) i;
String letterOnButton = Character.toString(letter);
button.setText(letterOnButton);
button.setId(i);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, id);
rl.addView(button, lp);
id = i;
}
I would go with a LinearLayout instead of a RelativeLayout, something like this:
LinearLayout ll = (LinearLayout)findViewById(R.id.dynbuttons);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
for(int i = (int) 'a'; i <= (int) 'z'; i++) {
Button button = new Button(this);
char letter = (char)i;
String letterOnButton = Character.toString(letter);
button.setText(letterOnButton);
ll.addView(button,lp);
}
If you still want to use a RelativeLayout you have to set the parameter layout_alignLeft for each button.

Categories

Resources