I'm writing a custom Android view which contains an EditText. I want to be able to center the text vertically through Java code but it's not working.
As you can see from the image above, the text is not centering vertically. I set the EditText background to a light red so you can see the layout bounds of it. I have an EditText on the left of it rendered through XML code and it it working correctly.
How do I get the text to show correctly?
.
Here is my code for the middle EditText:
txtSearch = new EditText(context);
txtSearch.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1f));
txtSearch.setGravity(Gravity.CENTER_VERTICAL);
txtSearch.setBackgroundColor(context.getResources().getColor(R.color.nowl_red_transparent_12));//Color.TRANSPARENT);
txtSearch.setHint("Search");
txtSearch.setHintTextColor(context.getResources().getColor(R.color.text_hint));
txtSearch.setInputType(InputType.TYPE_CLASS_TEXT);
txtSearch.setMaxLines(1);
txtSearch.setHorizontalScrollBarEnabled(true);
txtSearch.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
txtSearch.setTextSize(16f);
txtSearch.setEllipsize(TextUtils.TruncateAt.END);
txtSearch.setText("Y u no center");
container.addView(txtSearch);
and here is the code for the EditText on the left which renders correctly:
<EditText
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#color/transparent"
android:hint="test" />
I tried practically everything. I used setGravity(Gravity.CENTER_VERTICAL), removed/added properties, and more. Nothing of which worked. Is this a bug with Android or is there something I'm missing here? It looks like this in the UI Preview and in actual Android devices as well.
From the looks of it, it does look aligned to some degree. If it was top aligned, it would be on the far top with maybe 1dp of margin. If it was bottom aligned, it would be so high either.
I drew a green box on top of your original image to show what I think is what it is aligned relative to.
Given you have replaced the original background with a custom color, It might be possible that Android is pulling some unexpected default settings either from your context object or the system defaults and adds a padding (although I don't remember Android having any padding by default). Have you tried txtSearch.setPadding(0, 0, 0, 0) just in case?
It works well on my side with your code. The only difference is I don't know what is your contain like.
My advice is :
Try to remove the white background image which must be a 9 patch image
Open your device's developer options: Settings->Developer Options->Show layout bounds to debug your view. In this way, it'll be much easier for you to debug.
Add android:gravity="top" attribute to your EditText in the xml file.
How do I place the text inside an EditText widget at the top?
Related
I've inherited an Android Studio project with a challenging goal. Basically, the idea is that the app displays an image which is the map of a building (museum). When a user clicks on a room, the app displays the name of that room, and then they have some other options to do things. This works fine as long as we design the app for use one a single device and don't ever plan to use it on anything else, which of course is silly. I have been able to get the map image to be responsive to different size screens just fine. The problem I have is with the buttons, which were drawn over portions of the map during design.
I have used ConstraintLayout, but it isn't doing what I need. For one, the buttons don't really move the way I would expect. For example, when I run the app on a tablet, the image resizes fine, but the buttons are all clustered in one quadrant; they didn't really move with the image resize the way I expected.
The second issue is the button size. Even if the buttons did move when on a larger screen (let's just assume they did), the buttons themselves don't resize. I have tried WRAP_CONTENT, but then it sizes it larger than I want it even in design. Setting it to 0dp just makes a 1 pixel button (not sure why). Is there some way to get the button size to grow in proportion to the image growth when responding to different screen sizes? In other words, in the layout design, I want to be able to specify the button dimensions so that they cover the exact portion of the map image I want, but then when the image grows, I want the button to grow a proportional amount, so that it mostly stays lined up with the room to which it corresponds.
I have read about using an overlay image with colored areas as a workaround, but this won't be practical in some room images where there will be in excess of 20 clickable areas.
I hope that explanation is clear enough. Thanks in advance for any help.
The buttons (there are many of them) are defined in the layout like this:
<Button
android:id="#+id/LateGothicHallBtn"
android:layout_width="46dp"
android:layout_height="102dp"
android:layout_marginEnd="80dp"
android:layout_marginBottom="221dp"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="#+id/FirstFloorMap"
app:layout_constraintEnd_toEndOf="#+id/FirstFloorMap" />
And then there is a listener for when the button is clicked:
Button LateGothicHallBtn = findViewById(R.id.LateGothicHallBtn);
LateGothicHallBtn.setOnClickListener(this);
If you need it the ImageView code is this:
<ImageView
android:id="#+id/FirstFloorMap"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:contentDescription="#string/first_floor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/gui2" />
Is that what you need?
In your position, I would advise you to look at libraries that have the functionality you need, for example, AndroidImageMap. If necessary, you can download them, insert them into your project as a module and change the code. Also check these questions, I think it will make it clearer: clickable area of image, How to make certain areas in an image clickable in android
That's not that hard to achieve, you need to override onMeasure in the parent ViewGroup that has the image in the background, and knowing its original size you get the scale to the new dimension you wanna fit then you multiply all coordinates by it: the background image and all buttons l,t,r,b in a similar way AbsoluteLayout works
I myself also prefer to override onDraw and onTouchEvent and draw/click the buttons in the same component, but you can make it work with the android.Button
I can provide more help if you add your code
I have created an overlay menu that is a rectangle.
It displays on top of everything else while active. All other apps, the home screen, etc.
If you click anywhere outside of this rectangle your click hits
whatever is visible.
If you click the red part it allows you to drag the menu around the
screen. Clicking the green part performs some function.
I have created this rectangle from inflating layout xml. The rectangle itself is a relativeLayout and the green and red parts are both imageView children.
Since I'm unable to move the relativeView off screen and since the background needs to be clickable my only solution was to calculate margins and apply them to the relative view to imitate the desired effect.
However, the code to calculate these margins is almost 50 lines and fairly complex and hard to understand at a glance if someone else needs to refactor. I can post the code but I don't believe it's relevant to the actual problem's context.
Is there a much simpler way to do this that I'm missing? Is my approach wrong?
I hope this simple project may help you Android Floating Widget
My application has a TextView, used for showing lots of data for the user. The data would be best represented in a structured, "semi-spreadsheet way", ie. some data would need to be always shown on topmost line of the view, some data on bottom-left corner, some on bottom-right corner, and the "middle" of my view would need to be reserved for other stuff.
As a minimum, I would thus need to reserve:
topmost row for certain data
bottom left corner for some
bottom right corner for some
the rest of my view to a rapidly changing data stream
Is there any UI component which would allow me to position my txt freely around the component, to achieve my goal? I need to keep this view (currently TextView) as one, since I need the view to be clickable for other purposes, and dividing the area into millions of small TextViews really is not the answer I'm looking for.
You can use RelativeLayout creatively to achieve what you need. There is no specific layout that will satisfy your needs. But RelativeLayout is something i can see that would serve your purpose if used creatively.
You can use this in your xml file for alinment.
android:gravity="left"
OR in java you can do
TextView textView = (TextView)findViewById(R.id.mytextviewid);
textView.setGravity(Gravity.RIGHT);
For aligning view inside relative layout you can do any of this
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
You have to use Relative layout in your layout xml file and position different textviews according to your need using align properties as described above.
Note: Android does not provide facility to write text in textview at particular position, what at most android provide is align text within textView to left/right/middle position using layout_gravity parameter, but this applies to whole text in textview.
I'm a beginner Android developer and I'm making my first app. I'd like to make a simple view that is overlayed on the map with a semi-transparent background. This is what it would hopefully look like:
As you can see, one of these dialogs/windows has a simple integer displayed and the other will have a rendered graph/chart.
What would be the best way to go about making this? A dialog? The problem with that might be that I would for the user to be able to work with the mapview below while this is displayed. And I'm not sure if a simple transparent rectangular canvas is the right way to get this done.
Any suggestions/ideas would be much appreciated. Thanks so much.
two ways to go
-Make it a FrameLayout so add MapFragment first then add a Linearlayout with orientation horizontal and make gravity top and translucent background,so it will be displayed on top of map, and it will also involve the elements inside,
-Instead of FrameLayout make it a RelativeLayout and the same process as above.
-Make the overlay as seperate Activity(which is the worst case scenario)
Sample code for overlay can be like this
<LinearLayout
....
android:divider=".." // some drawable or color whatever you want
android:showDividers="middle"
android:orientation="horizontal"
android:background="a000">
<TextView
....
android:value="58"/>
<com.example.custom.view
..../>
</LinearLayout>
I think you can use a RelativeLayout on the top of the MapFragment, with a black background and some opacity (for that you can use a drawable).
Then, on the RelativeLayout you can add other controls like TextView and ImageView
I don't recommand you to use a Dialog, since it sounds like it's not corresponding to your needs.
I have a text view on my android app which I want to place in a specific place on the screen. This is because there is a background on the screen and the text view has to show in line with the space I have made for it in the background.
I'm moving the text around using the margins and entering for example 20dp for top margin 10dp for right margin.
Is there any other method I can use in order to ensure that the text view will be displayed in the right place on all devices? As other dvices mas have a higher or lower res.
use relativelayout and you can control the position of a view in a much better way.
android:layoutalign_parentright=true tag makes the view to be the right always.
android:align_right="#id/someViewId" makes the right edge of this view aligned with the right edge of someViewId view.
It packs a lot of tags that enables you to do such stuff.