Android SDK ImageButton stretches when Gravity.Right - android

I have this code:
ImageButton call = new ImageButton(context);
call.setId(9001+result.index);
call.setBackgroundResource(R.drawable.small_call);
LinearLayout.LayoutParams call_params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
call.setLayoutParams(call_params);
It renders the button the way I want it, but when I do this:
ImageButton call = new ImageButton(context);
call.setId(9001+result.index);
call.setBackgroundResource(R.drawable.small_call);
LinearLayout.LayoutParams call_params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.RIGHT
);
call.setLayoutParams(call_params);
It stretches the image, messing up the aspect ratio, and makes it blurry/pixelated.
So what is the correct way to render the image as in the first code snippet, but place the button where the second code snippet puts it?
Thanks in advance.

Use RelativeLayout or FrameLayout instead of LinearLayout.
See also layout_gravity in LinearLayout.
This is a tutorial: A Visual Guide to Relative Layouts In Android.

Related

Android first ImageView in vertical LinearLayout stretching

I'm creating the above popup, the content of which consists of rows of horizontal LinearLayout views within a main vertical LinearLayout. Each horizontal LinearLayout contains one ImageView and one TextView.
I'm creating this within a PopupWindow, and doing so programmatically so that I can change the ImageView source as required.
As you can see the first icon seems to take up a lot of space, despite having the same code generating it as the other icons.
Below is the code:
LinearLayout verticalLayout = new LinearLayout(context);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams mainLayoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
verticalLayout.setLayoutParams(mainLayoutParams);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams iconParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams textParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//History row
LinearLayout historyLayout = new LinearLayout(context);
historyLayout.setLayoutParams(layoutParams);
historyLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView historyIcon = new ImageView(context);
historyIcon.setImageResource(R.drawable.small_book_grey);
historyIcon.setAdjustViewBounds(true);
historyIcon.setLayoutParams(iconParams);
historyLayout.addView(historyIcon);
TextView historyText = new TextView(context);
historyText.setLayoutParams(textParams);
historyText.setText("History");
historyLayout.addView(historyText);
verticalLayout.addView(historyLayout);
//Exam row...
//... (duplicate of history row)
I've tried playing around with the layout parameters, even creating a mock xml layout that displays the content as I'd like, to match the parameters to.
If anyone can give some advice on making that book icon the same size as the others, I'd be grateful.
Add a scaleType to ImageView of fitCenter
Write this code under historyIcon.setAdjustViewBounds(true);
historyIcon.setWidth()`
And put width according to your layout.
Although I didn't figure out why the first image was scaling differently to the other images, I did find another solution: Using compound left drawables.
historyText.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Drawable img = m_context.getResources().getDrawable(
R.drawable.small_book_grey);
img.setBounds(0, 0, img.getIntrinsicWidth() * historyText.getMeasuredHeight() / img.getIntrinsicHeight(), historyText.getMeasuredHeight());
historyText.setCompoundDrawables(img, null, null, null);
historyText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Manually setting the bounds to match the TextView worked. Seems clunky, but it was the only way I could get it to do what I was aiming for.

Android: Center Text in programmatically added Button

I am trying to add a button programmatically in a custom view implementation. App is API 15+.
Here I am facing issue with button text not getting centered. It sounds trivial, but it is not working. Below is code in question. output is
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
mSubmit = new Button(mContext);
mSubmit.setTextColor(Color.WHITE);
mSubmit.setText("Submit");
mSubmit.setTextSize(getResources().getDimension(R.dimen.headertxt));
addView(mSubmit, params);
Set equal padding for all sides of your button:
for example:
myButton.setPadding(2,2,2,2);
Before:
After adding our padding:
addContentView worked for me!
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
mSubmit = new Button(mContext);
mSubmit.setTextColor(Color.WHITE);
mSubmit.setText("Submit");
mSubmit.setTextSize(getResources().getDimension(R.dimen.headertxt));
addContentView(mSubmit, params);
It's not centered vertically, so try
mSubmit.setGravity(Gravity.CENTER_VERTICAL);
Could also be some custom background for the button that's not correctly set up -- it's not very clear from the image.
And note : The gravity you set on the layout params is a completely different thing than the gravity you set on the button.

Programmatically adding three buttons in line in a LinearLayout

I'm creating a LinearLayout programmatically and I'm adding to this layout three buttons, but they're showed one on top of the other.
How I can show the buttons in line?
Reading around I probably understood that I need to set up a LayoutParams but I didn't figured out how..
I've tried with this but it didn't did the trick..
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
ll.addView(b1, layoutParams);
ll.addView(b2, layoutParams);
ll.addView(b3, layoutParams);
Thanks for any help!
EDIT:
Probably I needed to add more details.
I have also other stuff in the Layout but I does'n matter, I've created an additional layout just for the buttons.
Now the buttons are in line but they have different width.. : /
I've tried with this but it didn't help..
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
1f
);
ll.setOrientation(LinearLayout.HORIZONTAL);
Lets you set buttons in horizontal line allignment.
But to give balanced space to all three buttons. you must set weight property for all the three Button objects to 1.
Edit:
Do this for all buttons.
LinearLayout.LayoutParams params = button.getLayoutParams();
params.weight = 1;
button.setLayoutParams(params);
to set weight for all buttons.
Regards,
Aqif Hamid
Try this:
ll.setOrientation(LinearLayout.HORIZONTAL);
All have MATCH_PARENT should have LayoutParams.WRAP_CONTENT in width or height (in at least one as per LinearLayout orientation )
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
Try it:
LinearLayout gvDivisao = (LinearLayout) findViewById(R.id.gvDivisao);
LayoutInflater inflater = getLayoutInflater();
LayoutParams btDivLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
Button btDivA = (Button)inflater.inflate(R.layout.button_divisao, null);
btDivA.setText("A");
gvDivisao.addView( btDivA, btDivLayoutParams);

LinearLayout not correctly resizing

I have a LinearLayout and dynamically add three LinearLayouts to it. Each of the LinearLayouts contains a Button. After pressing one of the Buttons I wanted to display a CalendarView in the second-level LinearLayout. The View is displayed, however, it is not shown fully. I have attached a screenshot to visualize the problem:
http://i.stack.imgur.com/CE9je.png
Here is the code for adding the layout:
LinearLayout.LayoutParams pLayouts = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
hllCalendar = new LinearLayout(this);
hllCalendar.setLayoutParams(pLayouts);
hllCalendar.setId(hllCalendarID);
hllCalendar.setOrientation(LinearLayout.VERTICAL);
btCalendar = {some other code}
hllCalendar.addView(btCalendar);
hllComponents.addView(hllCalendar);
And here is the code for adding the CalendarView:
LinearLayout.LayoutParams pCalendar = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
CalendarView cal = new CalendarView(this);
cal.setLayoutParams(pCalendar);
cal.setOnDateChangeListener(this);
hllCalendar.addView(cal);
Any suggestions on how I could make the calendar fully visible?
Here is the dynamically created layout for your convenience:
hllComponents pLayouts
-hllCalendar pLayouts
--btCalendar
--CalendarView pCalendar
-hllStartTime pLayouts
--btStartTime
-hllEndTime pLayouts
--btEndTime
The problem is the same as in this post.
Try calling the requestLayout() of the parent layout.
This will post a "relayout" message on the UI's messages loop. This will calculate the new size of the layout, after the calendar was added, and thus paint it fully on the screen.

How i detemine Button (x,y) cords in Layout?

I Have this simple Code:
layout = new RelativeLayout(this);
bottom = new LinearLayout(this);
Button close = new Button(this);
close.setText("Close");
bottom.addView(close);
layout.addView(bottom);
setContentView(layout);
How can i put my button at the bottom of the window i prefer to determine the x,y cords but any solution will be good.
since the absoluteLayout is deprecated i tried using the Relativelayout as the api adviced with no succes.
i'm not using XML at all is that a problem ?
You can set LayoutParametrs (RelativeLayout.LayoutParams) to your elements. And don't forget to set LayoutParams to parent view.
RelativeLayout.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(ALIGN_PARENT_BOTTOM);
button.setLayoutParams(lp)

Categories

Resources