Android floating textview - java

The title may not be accurate but here is what I want to achieve:
I have a layout header.xml which looks like this:
Now I want to control the display of a textview below the icon programatically. Below is how it looks when the textview is set to view.VISBILE:
I include this header in my Activity layout. I have a text view in my activity which has a top margin of 150dp. So, when I set the visibility of the icon's textview dynamically, the text in my activity is coming down. See below two images for comparision:
But even when the icon's textview is visible, I want the "Hello World" text to stay at 150dp distance w.r.t header when the icon's textview is set to view.GONE. So, I am looking for a kind of a floating view or something similar which will not take up space in the layout dimensions.
Note: This requirement is similar to a tooltip but I don't want to use any tooltip library.

Related

Programmatically set TextView with Scrollbar on the top

I have a problem with TextView that has ScrollBar. I know that in order to create scrollbar on the textview, I need to enable this on the xml:
android:scrollbar="vertical"
and in the code.java, I set like this:
tvChapterDesc.setMovementMethod(new ScrollingMovementMethod())
Then, now user could scroll the TextView, if it is more than the defined number of lines. (say maxlines = 5).
However, I have a Button where if it is clicked, it is supposed to scrolled the textview to the top, can I do this? How? For example that the textview has 1000 lines, and there is a button "top". if user click button "top", it will be scrolled to the top immediately, so user can start reading to the beginning of the textview again. How to do that?
You should be able to use the scrollTo function on the textView.

How to select word from textview and then change the font of that particular word at runtime

I am working on an android app in which a user can select a picture and after selecting picture user can add text over it as he wish. I am adding these textView as "I am taking a relative view and then adding a textView as a subview of this relative view". The relative view does contain the touch and pinch zoom listener to scale and rotate an image. Now the new requirements the user can select the select the text from textview and can change the selected word font and color . How can I do this?
Please help.

Relativelayout circual dependancy

I'm trying to create my android app for a lot of different devices so I'm trying to avoid using fixed heights and width and instead using the property WRAP_CONTENT.
Now I need to create a textview on top of a button and align that to the bottom. However the documentation states that you can't use WRAP_CONTENT in combination with ALIGN_PARENT_BOTTOM (which is obvious). Is there another way to achieve this?
The structure is something like this. A RelativeLayout which wraps a button and a textview.
RelativeLayout fl = new RelativeLayout(this);
fl.setLayoutParams(relativeWrapContentParams);
fl.addView(filterBtn);
fl.addView(filterCaption);
The buttons are also created dynamically so theres no xml. Instead the buttons are created in java code.
Also is this a good way of programming for multiple resolutions? Or is it ok to use fixed heights because then the problem is easy to fix and I can just give the relativelayout a fixed height and align its children with ALIGN_PARENT_BOTTOM
See this link this article is the bible for the newbies in android.
Now coming to your question you don't need to use relative layout just for this purpose
you can use linearlayout with vertical orientation place text and then button.
and you need to place this linearlayout inside relative layout with property alignparentbottom=true.
in such way you can have this layout of text and button at the bottom of the screen

How to add clickable widgets in EditText at current position

I guess the title of the question is clear enough. What I want to do is add clickable widgets like Lable or Button or TextView in an EditText which already has some text in it. These widgets should get appended to the text.
As I don't know the exact number of the widgets to be added at design time, it won't be possible to do it in xml. I must do it in Java.
Thanks in advance.
Views just can be added to ViewGroups and EditText is not a ViewGroup, so you can't do that with default EditText widget, and you can write your own custom View to implement that.
this is the android documentation about custom views, and taking a look at this can be useful too.

How do I create an ImageView in java code, within an existing Layout?

I'm looking for an easy way for the user to see how many drinks they've had for a BAC calculator.
Picture of the app:
On button press, I would like an image to be added to the screen, directly under the spinner and with left alignment. When I press the button again, I want another image to be added to the screen.
So if I pressed the add beer button, a drawable of a beer would appear below the spinner. If I pressed the add beer button again, I want there to be TWO drawables of beers under the spinner, preferably with them being added from the right.
(Also, having them reach their width limit, wrapping around, and starting again on the left, but below a full line, would be AWESOME)
I can't figure out how to do this. I assume adding a ImageView in code to a relative layout (because it needs to be positioned to the right) would be the best route, but if it's possible in xml I'd be more than happy to use that. Any help?
In the button's click callback, create an ImageView object, set the bottle image, and position it. For example (I'm assuming the names of your elements):
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.beerbottle);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout01);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
layoutParams.addRule(RelativeLayout.BELOW, R.id.ButtonRecalculate);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativeLayout.addView(imageView, layoutParams);
I haven't tested this, mind you, but it should give you a good start. You'll probably need to add other parameters to the ImageView and possibly to the LayoutParams to make it look good, plus tracking how many bottles are displayed, etc.

Categories

Resources