How to make a normal lines numbering in EditText? - java

I am making a text editor for Android and I would like to show the number of every line in theEditText.
My solution was to make a TextView with lines numbers and place an EditText next to TextView. Like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:fillViewport="true">
<TextView
android:id="#+id/lines_numbering_view"
android:layout_width="wrap_content"
android:minWidth="10dp"
android:layout_height="match_parent"
android:textSize="18sp"
android:text="1\n"/>
<EditText
<!--EditText options here-->
/>
</LinearLayout>
This works fine but there is one fact... If the lines count is about 600, when TextView's onMeasure method is called, it makes the app to freeze for a 0.5 seconds.
So, what can I do to improve the performance of TextView or are there any good solutions of numbering lines?

The line number view can be seen and implemented as a ruler. All it needs as input is the line height. From there it can display increasing line numbers at the proper vertical positions. It need not know the number of lines in the content view.

Related

How to make a layout in android that sticks to the bottom of the screen until the scrollview is scrolled to a certain position it gets scroll too?

I want to make a android activity screen where a layout is sticked to bottom until scrollview get scroll to a certain position. like in this video samples links:
https://drive.google.com/open?id=0B14wNBitoI33LWtUNThqcXRIUWc
https://drive.google.com/open?id=0B14wNBitoI33QW1BOGR1di1TcGc
amarjain07's StickyScrollView maybe a solution you would want to look into. His methodology of assigning the "sticky" views as identified attributes in the layout xml is quite a nifty feature IMHO.
Use a RelativeLayout and put the Button-Container inside it.
Pass the container in your_scrollview_activity.xml the following attribute
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1 (reft)" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2 (right)"/>
</LinearLayout>
</RelativeLayout>
NOTE: You need to set a listener in your activity to get the ScrollViews current position / current height. With this current height you can calculate the "left space" to the bottom / end of the ScrollView. If your RelativeLayout reaches the bottom or if it's close to the bottom AND within the visible area, then call your animation.

Baseline aligned TextView clips bottom line of multi-line text

I would like the items in a ListView in my android app to contain a check box on the left, followed by some text, followed by a button. I would like the check box and button to be baseline aligned with the text. To achieve this, I put all three in a LinearLayout which tries to baseline align its items by default. This works great, except that the bottom line of multi-line text is always clipped.
Here is an example of how it looks:
There is a related question on StackOverflow with the proposed solution to be to set layout_gravity = "fill" for the TextView, but that defeats the purpose as it disables the baseline align. Is there any solution to this problem that allows me to preserve the baseline align? This seems like a pretty basic problem that surely many people must encounter.
The only other solution I've found is to add some padding to the bottom of the text view, but this seems hacky and not robust against font sizes. I could set the padding procedurally from code based on the font size, but it seems like it may be tricky to get the correct values.
Here is a sample layout that illustrates the problem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:baselineAligned="true">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginRight="8dp"
android:textSize="16sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Woah, this is a long text line, enough that tit has to wrap to multiple lines, thus demonstrating our problem. In fact, it wraps to multiple lines! So many lines! I hope they all show up correctly!"
android:textSize="16sp"/>
<FrameLayout
android:layout_width="40dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="12dp"
android:src="#drawable/ic_note_check_delete"/>
</FrameLayout>
</LinearLayout>

Android: Fixed size ScrollView not scrolling/scrolling weird

I have a problem with a fixed size scrollview that i'm trying to make. I've read many questions that are similar (but not equal) to mine here in StackOverflow and other sites and none of the answers have helped me, so I decided to ask my own question.
Basically, i want a fixed size scrollview with different controls inside. The basic one would be a textview of a dynamically changing size inside it. When I change the text of the textview and it is bigger than the scrollview, the scrollbars flash quickly, as they should, indicating that I can scroll but no matter how many times I swipe my finger, it doesn't scroll. Then I tried swiping with two or three fingers and sometimes (only a counted number of times) it scrolls.
I have tried many different approaches to this, like changing the textview to an edittext with focusable = false so it doesn't give the user chance to edit the text; or putting the textview alone in the scrollview, or wrapping it in linearlayouts, relative layouts etc. and it still doesnt scroll.
Below is the code as it is today. This scrollview is inside a vertical linearlayout along with other controls that I'm not putting 'cause of the length, but if someone needs it, I'll put it. I would appreciate very much if someone can point to my problem or help me solving this.
XML:
<ScrollView
android:id="#+id/scrollFifthHorizontalLineDetails"
android:layout_width="304dp"
android:layout_height="133dp"
android:layout_gravity="center"
android:layout_marginTop="5dp" >
<LinearLayout
android:id="#+id/rrrr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtlblPlaceDetails"
android:layout_width="302dp"
android:layout_height="wrap_content"
android:background="#drawable/placedescriptionbg"
android:text="#string/null_text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</ScrollView>
the line in which I populate the TextView:
((TextView) findViewById(R.id.txtlblPlaceDetails)).setText(mJsonOb.getString("placeDescription"));
I had a similar problem with an Android app I was developing a few months back. I got around the scrolling problem by increasing the layout width for the scroll view. In the editor you can actually drag the RHS of the scroll view to exceed the visible area. I think I set the android:layout_width to about 1024.

android:gravity="right" not working on all devices for Arabic and other RTL languages

I've got an Arabic Android application, and here is the XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/greygradientbackground">
<ImageView android:id="#+id/logo"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:layout_margin="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" />
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:layout_gravity="center_vertical|right"
android:gravity="right"
android:layout_toLeftOf="#id/logo"/>
</RelativeLayout>
The problem is that android:gravity works on some Android models and on others don't.
To be more specific, I've tested the app on many 2.3.3 android devices and the Arabic text is aligned right. However on other 2.3.3 devices the Arabic text is aligned left (which is wrong).
When I changed android:gravity="right" to android:gravity="left" the problem shifted from the second group of devices to the first.
So my question is how can I solve this issue especially that as far as I know there aren't a way to localize layouts based on a device model number.
Thanks in advance for any guidance because am totally lost. :(
UPDATE:
I searched about "How to align Arabic correctly on all Android versions?" but found nothing working on all my testing devices. Any suggestions please? I am sure there is a best practice approach for aligning Arabic text on Android.
UPDATE 2:
I tried to use a WebView instead of a TextView to align Arabic correctly using CSS styles. However, the Arabic text is showing in the WebView as strange characters.
Here is the Code:
mWebView.loadData("<html dir=\"rtl\">الأسم<body></body></html>", "text/html", "UTF-8");
The strange thing is that Arabic websites text is displayed correctly. So, what's the problem? :(
This is an expected behaviour. You may not use gravity="right", but gravity="end" instead, the same idea you apply to gravity="left" which you may use gravity="start", as well as layout_marginStart instead of layout_marginLeft
This way, Android will put the text to the "start" orientation, depending on the location. (for us, americans, we start to write from the left and we end on the right, but arabians start from the right and end on the left).
You can get the layout/text directions with the method getLayoutDirectionFromLocale(null), this will return View.LAYOUT_DIRECTION_LTR (left-to-right) or View.LAYOUT_DIRECTION_RTL (right-to-left, like arabic).
More info you can check here
change your textveiw in the layout to this....
<TextView android:id="#+id/title"
change your layout_width to "fill_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:layout_gravity="center_vertical|right"
android:gravity="right"
android:layout_toLeftOf="#id/logo"
/>
You can change the thing in another way also....You can make changes in the activity class..as follows...let your TextView with id title is mapped in the activity class with a TextView object named "tv" the you can make it right aligned by writing this.
tv.setGravity(Gravity.RIGHT);
but in both the cases the width layout width should be "fill_parent"..
If you do not do this and keep it "wrap_content" then there is no space for the text to move... all the alignments (center,left,right...)remain the same.....
If both of these donot work out... then I would prefer you to use table layout and keep the text view in it....
try putting your textview inside a separate LinearLayout ,and set that layouts properties likethis:
android:layout_gravity="center_vertical" and android:orientation="vertical"
android:gravity="right"
note: dont specify layout_gravity of the textview.
hope this helps! it worked fine for me.
also you can try the solution provided here: Android Arabic text aligment
Another solution to achieve such behavior:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="ltr"
android:layout_weight="1"
android:gravity="end"
android:padding="16dp"
android:text="#string/locale_switch_btn" />
</LinearLayout>
Note that, for correct gravity="end" alignment, in rtl locales you should set property:
android:layoutDirection="ltr"

How to set height for the listview that created dynamically through java code?

I 'm trying to set the height of the listview which has been created through java code dynamically. I'm using 3 listviews(one below another) in a scrollview(to avoid hiding of lists). If i use scroll view, i cannot see all the list items of the listviews.Is there any idea to set height for those 3 list views through java coding in android? Any help is really appreciated and thanks in advance...
Don't put ListViews in ScrollViews! In Detail Information from the developers of Listview is available in the talk World of ListViews
It is possible to make the three ListViews share the space on the Screen by using the layoutWeight parameter.
Make a layout like this (you can also use code for that):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/listView1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></ListView>
<ListView android:id="#+id/listView2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></ListView>
<ListView android:id="#+id/listView3" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></ListView>
</LinearLayout>
Here each ListView takes a third of the available space (all weights set to 1).
To set the layoutWeight in code use this line as example:
listview1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));

Categories

Resources