I am writing an Android app. I have an activity in my main project which inherits from an activity in my library project. I have a custom titlebar in the base activity which has a button, which uses the following style:
<style name="TitleButton">
<item name="android:padding">6dp</item>
<item name="android:layout_width">48dip</item>
<item name="android:layout_height">48dip</item>
<item name="android:layout_gravity">right</item>
</style>
Works fine. In my child activity, I want to add a button. I can add the button, and it clicks and works fine, but it LOOKS wrong. I am adding the button as so:
ImageView imgAdd = new ImageView(this, null, R.style.TitleButton);
imgAdd.setImageResource(R.drawable.add);
imgAdd.setClickable(true);
imgAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addGroup();
}
});
FrameLayout tb = (FrameLayout) this.findViewById(R.id.Header);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
tb.findViewById(R.id.TitleClose).getLayoutParams()
);
lp.gravity = Gravity.RIGHT;
lp.width = 48;
lp.height = 48;
lp.setMargins(0, 9, 68, 0);
imgAdd.setLayoutParams(lp);
tb.addView(imgAdd, 1);
Notice how the Add button which I've added through code is too big, and offset too much. I'm figuring it must be due to what was pointed out in the comments on this answer, that setting the width and height so sets them in pixels, where as the XML layout sets them in dip. So, my question is, when setting layout params in code, how can you set the unit, so that I may set my new add button to be measured in dip instead of in pixels so that it will display right?
You can use DisplayMetrics to get current density and use it to set width, height and margins accordingly (reference here: px = dp * (dpi / 160))
Related
Thanks in advance for the help.
I have a checkbox that I am creating in a method call
public int myMethod(Context context, doThing) {
if(doThing){
this.checkBox = new CheckBox(context);
do some stuff...
return 1;
}else{
do something else...
return 0;
}
}
I would like to change the size of the checkbox (the actual box not the box and text combo) with a call like checkBox.setBoxSize(mySize). setBoxSize is not a native android method. Is it possible to do this, and if it is, how might I go about it?
If you want to create your checkBox with predefined width and height you can use this method:
this.checkBox = new CheckBox(context);
this.checkBox.setLayoutParams(new LinearLayout.LayoutParams(width, height)); // or if your parent is RelativeLayout use RelativeLayout.LayoutParams(width, height)
Note, that your width and height are in pixels.
If you want to change already existing layout params, you should do something like this:
private void setBoxSize(CheckBox checkBox) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) checkBox.getLayoutParams(); // or RelativeLayout.LayoutParams
params.height = yourHeightInPixels;
params.width = yourWidthInPixels;
checkBox.setLayoutParams(params);
}
EDIT:
In that case, if you want to change size of the checkbox's button, you could only use one of this methods
checkBox.setButtonDrawable(int resId);
checkBox.setButtonDrawable(Drawable d);
and set the right size of your drawables. Unfortunately, Android doesn't have methods to manipulate button (as you mentioned - "box") of the checkbox.
Example of the drawable, that is using as checkbox's button:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/ic_check_on" /> <!-- checked -->
<item android:state_checked="false"
android:drawable="#drawable/ic_check_off" />
</selector>
You should set the layout Params for the checkbox like this
//width and height are in pixels
checkbox.setLayoutParams(new ViewGroup.LayoutParams(width,height));
I have an Activity with a dialog theme (Theme.Holo.DialogWhenLarge). It appears too narrow, and I'd like it to fill up a larger percentage of the screen. I am trying to accomplish this by overriding the windowMinWidthMinor and windowMinWidthMajor attributes.
The theme used by my Activity looks like this...
<style name="MyTheme" parent="#android:style/Theme.Holo.DialogWhenLarge">
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>
However, it seems like the windowMinWidthMajor and windowMinWidthMinor have no effect. Can anybody explain what I'm doing wrong?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Some code here
setWindowHeight(90);
}
/**
* Set percentage width height
* #param percent percent from current size. From 0 to 100.
*/
private void setWindowHeight(int percent){
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = (int)(screenHeight*percent/100);
this.getWindow().setAttributes(params);
}
It's impossible to set percents for android, BUT there is a way around. What I've done is get the screen width and multiply it by the percent I want my view or item to be (example: if I want something to fill 40% of the width if would be Screen-Width * 0.4)
I have a method that creates several buttons
public Button[] generaBottoniRisposta(int numeroBottoni, Context context){
Button bottoni[]= new Button[numeroBottoni];
/*genero un tot di bottoni in base a numeroBottoni, รจ necessario avere il context*/
for(int i=0; i < bottoni.length;i++){
bottoni[i] = new Button(context);
bottoni[i].setId(i);
bottoni[i].setText(String.valueOf(i+1));
LayoutParams param = new LinearLayout.LayoutParams(50, 50);
bottoni[i].setLayoutParams(param);
}
return bottoni;
}
and then another method that add them to a gridlayout.
I want to set the width of those buttons, but i'm not able to do it.
I tried a lot of stuff, setWidth(), setMaxWidth(), invalidate() etc.
Something weird happens. If I try to make the button bigger than its default size it works, if i try to make the button smaller than its default size it doesn't work!
How should I do? thank you
Try using LayoutParams, something like..
RelativeLayout.LayoutParams rel_bottone = new RelativeLayout.LayoutParams(buttonWidth, buttonHeight);
button.setLayoutParams(rel_bottone);
And the layout depends on the parent layout that contains the buttons..
I think setlayoutparams should do what you want. Like in the answer in this thread.
Set View Width Programmatically
try this:
LinearLayout.LayoutParams params = button.getLayoutParams();
params.width = 100;
button.setLayoutParams(params);
one more thing LinearLayout.LayoutParams is used when parent is a LinearLayout and when parent is RelativeLayout use RelativeLayout.LayoutParams.
Try something like this :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
button.setLayoutParams(params);
My buttons are inside a gridlayout. I tried to use GridLayout.LayoutParams giving the width of the cells, instead of trying to set the width of the button, and now it seems to work.
this is the code I use to add the buttons to the GridLayout
Spec row = GridLayout.spec(numeroRiga, 1);
Spec colspan = GridLayout.spec(numeroColonna, 1);
GridLayout.LayoutParams gridLayoutParam = new GridLayout.LayoutParams(row,colspan);
gridLayoutParam.width=50;
gridLayoutParam.height=50;
gridLayout.addView(button,gridLayoutParam);
But i'm wondering if i can set the width of the buttons in a similar way.
My dialog is using a linearlayout for its root, and its using the following code as its custom theme:
<style name="HuskyBusDialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
Is it possible to set a max width? Its fine on phones but im trying to optimize it for tablets and they are too big.
The width will be dealt with in the XML for the linear layout, not in the style that you apply to it. Use the android:layout_width tag in XML to specify how wide it could possibly be.
An old question, however no replies are suitable, but you can find some hint here: How to customize the width and height when show an Activity as a Dialog
This helps customize the width and height, but not set the max width and height! To achieve that on an activity using a dialog theme, I had to do a couple of things:
1) set a layout listener to know when the dialog layout is set.
2) Adjust the dialog layout if its size was beyond the desired limit, effectively setting max size
Here is the working snippet I'm currently using, called after setContentView():
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener()
{
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout()
{
adjustDialog();
}
});
Now the dialog size adjustment, as a percentage of actual screen size:
private void adjustDialog()
{
Window w = getWindow();
w.setGravity(Gravity.CENTER);
int current_width = w.getDecorView().getWidth();
int current_height = w.getDecorView().getHeight();
WindowManager.LayoutParams lp = w.getAttributes();
DisplayMetrics dm = getResources().getDisplayMetrics();
int max_width = (int) (dm.widthPixels * 0.90);
int max_height = (int) (dm.heightPixels * 0.90);
if (current_width > max_width)
{
lp.width = max_width;
}
if (current_height > max_height)
{
lp.height = max_height;
}
w.setAttributes(lp);
}
yeaa if u wanna do with Widht GO for ur xml
and try to do
android:layout_width="Defined in pixels//20px"
or u can try this also
android:width="Defined in pixels// 30px"
i hope it will be hepful to u
I have an activity with ListView that has:
android:theme="#android:style/Theme.Dialog"
in Manifest.
When I open it and when it has only one line in ListView, the window that opens is very small.
How do I make the window take the whole screen?
Use this in your onCreate method of the Activity to make it full screen.
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.myxml);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.MATCH_PARENT;
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
I have found that setting the window size does work, but you have to do it a bit later. In this example the window width is set to 90% of the display width, and it is done in onStart() rather than onCreate():
#Override
protected void onStart() {
super.onStart();
// In order to not be too narrow, set the window size based on the screen resolution:
final int screen_width = getResources().getDisplayMetrics().widthPixels;
final int new_window_width = screen_width * 90 / 100;
LayoutParams layout = getWindow().getAttributes();
layout.width = Math.max(layout.width, new_window_width);
getWindow().setAttributes(layout);
}
Similar to the answer from PravinCG but it can be done with one line in onCreate()...
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Use the suggested code before setcontentview() call. It will work.
Just a small update. Used MATCH_PARENT instead of the deprecated FILL_PARENT. PravinCG's answer worked great for me.
Yeezz ! I figured it out ! The problem is that the margin sizes are not calculated in the window widht. So If you set the layout margin to 0 and move that part to the padding of the layout the problem will be solved.