I need a button with foreground transparent image and background color. So have use this code. The background color is going outside of the image. I need the button with the same size of the image.
Depending on the user interaction i have to change the foreground image and background color. I want add the image and background color separately so that i can change one of them at minimum cost. I have to use a lot of button in this UI so it will be done in java code.
layout = new TableLayout(this);
layout.setLayoutParams(new TableLayout.LayoutParams(8,7));
TableRow row2 = new TableRow(this);
buttonPlayer1 = new ImageButton(this);
buttonPlayer1.setImageDrawable(getResources().getDrawable(R.drawable.blankc4));
buttonPlayer1.setBackgroundColor(Color.GREEN);
row2.addView(buttonPlayer1);
layout.addView(row2);
If your only problem is that button background color is going outsize image and you need button size to be same as image size then get image Height and Width using getHeight() & getWidth() methods on image object and use these values to make the Button size same as Image size using setHeight() & setWidth() methods respectively on button object.
Sample Code with LinearLayout:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout btnLO = new LinearLayout(this);
btnLO.setOrientation(LinearLayout.VERTICAL);
ImageButton i1 = new ImageButton(this);
i1.setBackgroundColor(color.background_dark);
i1.setImageDrawable(getResources().getDrawable(R.drawable.rana));
btnLO.addView(i1, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// btnLO.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);
this.addContentView(btnLO, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
Related
I'm writing a chess app and I've been able to resize my pieces to match the square size, that itself depends on screen size.
Problem is, it relies on XML. And I need to be able to do it dynamically (adding or removing pieces on the screen after each move)
This is working :
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(sq_size, sq_size);
ImageView klt60 = (ImageView)findViewById(R.id.klt60);
klt60.setImageResource(R.drawable.klt60); // setting the image in the layout
klt60.setLayoutParams(layoutParams); // allows resizing
But this is not : if I declare my imageView without using XML, it just won't resize, and also it won't move when I play a move with that piece.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(sq_size, sq_size);
ImageView klt60 = new ImageView(this); // Create a new image (several pawns to display, for example)
klt60.setImageResource(R.drawable.klt60); // setting the image in the layout
klt60.setLayoutParams(layoutParams); // allows resizing
My view just extends extends AppCompatActivity. Thanks for help
I have an activity window (Activity pic), when I click the second button, popup window appears at the bottom (it's good, I need that) of screen (PopUp pic), when i click on the edit text field, brings up the keyboard, but it cover the popup window edit text fields (Keyboard 3).
Where is my error that the popup window does not up when the keyboard appears? Do you have any ideas?
Screen shoots
Activity
PopUp
Keyboard
Pop-Up Window class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width),(int)(height*.4));
WindowManager.LayoutParams params = getWindow().getAttributes();
params.gravity = Gravity.BOTTOM;
getWindow().setAttributes(params);
PopupWindow popup = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
/** ... **/
popup.showAtLocation(this, Gravity.CENTER, 0, 0);
Then the only thing you need is a Listener for the Keyboard (or more general: For Window Height changes). This was actually easier than I thought - and it didn't require any special access like an Activity-object or similar. Even in my independent View-class which only knows the Context (which I didn't want to cast), I was able to accomplish that. Everything you need is only one View-object which has already been added to the layout.
final View root = this.getRootView();
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
Rect r = new Rect();
root.getWindowVisibleDisplayFrame(r);
// Calculate the difference between the original height and the new height
int heightDiff = r.height() - root.getHeight();
// Now update the Popup's position
// The first value is the x-axis, which stays the same.
// Second value is the y-axis. We still want it centered, so move it up by 50% of the height
// change
// The thir`enter code here`d and the fourth values are default values to keep the width/height
popup.update(0, heightDiff / 2, -1, -1);
}
});
I am trying to make a game with multiple levels. My plan is to print 15 levels on 1 page. I have attached a photo of the simple design I am currently using.
As you can see in the picture the right border of a button is not showing. For the parent I am using this java code:
LinearLayout horl = new LinearLayout(this);
horl.setId(worldVar);
horl.setOrientation(LinearLayout.HORIZONTAL);
horl.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
And to set 3 buttons horizontally in this LinearLayout I use this code for the buttons. This code just has a simple for loop around to put 3 buttons in this LinearLayout:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout
.LayoutParams(getMarginInDp(100), getMarginInDp(100));
params.setMargins(getMarginInDp(1), getMarginInDp(25), getMarginInDp(1), 0);
button.setLayoutParams(params);
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);horl.addView(button);
The function getMarginInDp looks like this:
public int getMarginInDp(int dp){
return (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
Does anybody have any idea how to make the border appear because I want to keep the buttons approximately this size and I may even slightly increase the margins?
You can set the weight to 0.33f for each button:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getMarginInDp(100), getMarginInDp(100), 0.33f);
so they are distributed equally in the width of the layout.
I am trying to make dynamic buttons of fixed dimensions. I am able to alter the height but unable to change the width. The width of the button seems to MATCH_PARENT (it takes up whole screen width. If there are two buttons, each button width is half screen width and when there is only one button, button width is screen width).
TableLayout table = (TableLayout)findViewById(r.id.table);
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(30,30));
tableRow.addView(button);
Could anybody point out where I am going wrong.
Only This You have to do to change Button Dynamically
First I get Button
Button btn = (Button) findViewById(R.id.button1);
Then Button Click event I put This Code Only
TableRow.LayoutParams lp = new TableRow.LayoutParams(70, 70);
btn.setLayoutParams(lp);
And Works Fine. Try It. Will solve your Problem
Instated "button.setLayoutParams(new TableRow.LayoutParams(30,30));" change to following code change the button Height and Width.
LayoutParams lp = new LayoutParams(40, 40);
table.addView(button, lp);
I am creating some buttons in a tablelayout. I am creating the buttons programmatically.
For example the following code is in the "onresume" method...
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0,android.widget.TableRow.LayoutParams.MATCH_PARENT,3f);
TableLayout tableLayout = (TableLayout) findViewById(R.id.sometablelayoutivealreadydefinedinxml);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(tableParams);
Button btnOne = new Button(this);
btnOne.setId(100);
btnOne.setLayoutParams(rowParams);
btnTwo.setId(200);
btnTwo.setLayoutParams(rowParams);
tableRow.addView(btnOne);
tableRow.addView(btnTwo);
tableLayout.addView(tableRow);
I want to set the height of the button to be the SAME as the width. The width is determined by the size of the screen. The problem as I understand it,is that until the buttons are actually drawn to the screen they don't exist so any width returned will be zero. So I tried the following code.
Button yourBtn = (Button)findViewById(100);
int btnSize;
btnSize = yourBtn.getWidth();
yourBtn.setLayoutParams(new TableRow.LayoutParams(btnSize, btnSize));
Now the problem is that if I run this code immediately after creating the buttons the width of the button is returned as 0 as the button hasn't been drawn yet. However, if I assign the code to a test button then it resize the button as I want it to. However, I don't want the user to have to press a button after the screen has loaded to resize the buttons properly.
So, the question is when is the appropriate time to call this to resize the buttons properly when the activity is created but after the buttons have been drawn and how do I do this?