How to hardcode TextView aligment distance from start? - android

I have a layout with white background and big textViews with darkblue background and white text.
I want to give some space to the text from the start of the darkblue background rectangle.
I already tried but only can set start, center, end, top, etc; and I want to specify it numerically.
How I can meet that goal?

I think what you are looking for can be achieved by specifying the padding value on the TextView. Look for android:padding attribute. If you want to give some space from the start, you can use android:paddingStart attribute on the TextView.
Hope this helps.

Probably setting margin to your layout might help
TextView tv = (TextView)findViewById(R.id.my_text_view);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
params.setMargins(10, 0, 0, 0); //move 10 px to right (increase left margin)substitute parameters for left, top, right, bottom
tv.setLayoutParams(params);//tv.setMargins(left, top, right, bottom);
im not sure if this is excatly what you are looking for

Related

How to add space between textviews which is in TableRow?

I Displayed my program's output via TableRow in TableLayout using array of Textview.Actually the output is of 2d array.
I displayed every element of 2d array without any problems.
But what the problem is I cant have space between TextViews(Columns).
Below is the image which explains this clearly.I want to do this programmatically
Please help me to solve this
Remember that almost any property that can be set on your XML layout, can also be set programmatically!
The way to do it is:
view.setPadding(0,padding,0,0);
This will set the top padding to padding-pixels. If you want to set it in dp instead, you can do a conversion:
float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (sizeInDp*scale + 0.5f);
I go this from here .
You can set padding programmatically on the textview by using this method:
txt.setPadding(left, top, right, bottom);
set integer value for left,top,right,bottom according to your condition.Hope it will help...

Can a View be focused and invisible at the same time?

I want to have an EditText in focus so it is edited, but also invisible. In other words, all the user will see is the keyboard.
Is that possible?
AFAIK, an invisibility Edittext cannot have a view( I have never tried) so the workaround for you is
You can set a transaparent background for edittext to achieve this.
youredittext.setBackgroundResource(android.R.color.transparent);
or you can directly set the attribute in XML like this
android:background="#null"
I know I might be late, but recently I had to face a similar problem.
The solution could also be this one (use in styles.xml or in the layout):
android:layout_width = 0dp
android:layout_height = 0dp
That will also let your EditText be still focusable.
My two alternate approaches:
Approach 1: Fade it out
view.animate().alpha(0.0f).setDuration(0);
and fade it in when you want it back
view.animate().alpha(1.0f).setDuration(0);
Setting duration to 0 ensures it happens instantaneously.
Approach 2: Set the view outside the screen
LayoutParams params = view.getLayoutParameters();
int offset = -80; // offset from the screen border, choose your own
params.setMargins(0, 0, offset, 0); // left, top, right, bottom
view.setLayoutParams(params);
and do
params.setMargins(0, 0, 0, 0); // left, top, right, bottom
view.setLayoutParams(params);
when you want it back.

Can android padding be made to be not clickable?

EDIT: Answer found through Emil's comments. Code ended up like this:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
view.setLayoutParams(params);
Thanks all :D
I'm currently writing an Android application and I have a dynamically created tree which displays ImageView elements within it. However, to arrange these images I've used padding left to line them up correctly, dynamically changing how the padding is calculated so that everything turns out evenly. However when I do this, the padding covers up some of the images to the left, and even though you can see the images behind this padding, when you click on that image, it will evaluate the image who's padding is being used (the furthest left element). I'm basically wondering if there is a way to programatically set something that does what padding does to images but isn't clickable? Margins would make sense, but you cannot set margins on ImageViews.
The tree set up is like this:
-----A-----
--B--C--D--
-E-F----G--
Imagine every letter is an ImageView, dynamically placed with padding. But if I click on B or C, D's code will be evaluated because it's left padding covers B and C. The same goes for E or F, click those, and G will be evaluated. I cannot figure out a different way to place these ImageViews. Any help is greatly appreciated.
Consider replacing your padding with layout_margin property. that way the area that separates the padded view is not considered as part of the view but ratter as part of the layout it sits in.
So use
android:layout_margin="10dp"
or:
android:layout_marginLeft="10dp"
for your needs.

Android: Placing Views as required

I have an application where I have to place a set of ImageViews in different parts of the screen, each ImageView placed at a position by specifying the co-ordinates(x,y) Is this possible in android? If so, pls let me know how to do it.
It is possible but not recommended since it doesn't work well across resolutions/densities/aspect-ratios.
This is possible by setting the positions in their xml layout in an absolutelayout. Though, if you want to change the position of the imageview in your code dynamically you can only change the positions by setting their margins and padding to move them left/right and up/down. The following receives the layout params of the imageviews so that you can change the views' margins.
ImageView image;
MarginLayoutParams params = (MarginLayoutParams)image.getLayoutParams();
param.setMargins(int left, int top, int right, int bottom);

Android padding problem

:) I'm having the following problem: I have a view and i want to add borders to it. What I'm currently trying to do is to set padding to the view (padding from all the sides) and set background color to it which will fill the padding. The thing is that it seems to me that it's possible to set padding either only from top and left or from bottom and right but not from all of them together. I.e if i write
view.setPadding(border,border,border,border)
this will set padding only from top and left. In order to set padding from bottom and right I have to write:
view.setPadding(-border,-border,0,0)
which won't leave left and top padding and so on. If I try to use margin it moves the whole block(the view + the padding area), but not only the view, so this doesn't seem to work either. Any ideas on how to do it without having to use a wrapping layout? Thanks!
What exactly happens when you use the first example?
The four int parameters for setPadding() are for left, top, right, and bottom, respectively. So, calling setPadding(4, 5, 6, 7) should give you 4 pixels of space for the left edge, 5 for the top, 6 for the right, and 7 for the bottom. What result are you getting when you do this? Can you show a screenshot?
What is the content of your view? If it's an image or something similar, perhaps it's not being centered or scaled properly. Try calling setGravity(CENTER);.

Categories

Resources