Custom components or regular composition - android

I'm up to creating a piece of reusable interface, namely a year-picker(just year). Essentially, this component should have a couple of buttons and a textview, sliding feature would be great as well.
As I've said I strive to make it reusable, but I'm not sure whether Android provides any means for achieving it. I've started reading the "Custom Components" article - is it what I really need ? Or should I make something like a regular composing class that has references to view objects, etc ?
Thanks.
Update: it was simple - here's an example if someone gets in the same situation
YearPicker.class
public class YearPicker extends LinearLayout {
protected int _minValue = 1980;
protected int _currentValue;
protected int _maxValue;
protected int _decrementValueView = R.id.decrement_value;
protected int _previousValueView = R.id.previous_value;
protected int _currentValueView = R.id.current_value;
protected int _nextValueView = R.id.next_value;
public YearPicker(Context context) {
super(context);
}
public YearPicker(Context context, AttributeSet attrs) {
super(context, attrs);
Calendar calendar = Calendar.getInstance();
_maxValue = calendar.get(Calendar.YEAR);
_currentValue = _maxValue - ((_maxValue - _minValue) / 2);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
TextView curView = (TextView) findViewById(_currentValueView);
curView.setText(Integer.toString(_currentValue));
findViewById(_decrementValueView).setOnClickListener(
_onDecrementValueClick);
}
protected OnClickListener _onDecrementValueClick = new OnClickListener() {
#Override
public void onClick(View v) {
_currentValue--;
TextView curView = (TextView) findViewById(_currentValueView);
curView.setText(Integer.toString(_currentValue));
}
};
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<include
layout="#layout/year_picker"/>
</LinearLayout>
year_picker.xml
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="localhost.yearpicker.YearPicker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/decrement_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/previous_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/current_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/next_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/increment_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</view>

In your case, I would just extend a preferable ViewGroup, inflate an XML - for styling and positioning Views - into it, grab an access to those View, add all relevant set/get methods, and that's it.
If relevant, you can also define attributes for your component to adjust its custom functionalities while using it in XMLs.

You might not need a custom view at all. I prefer to keep separate XML layouts that I can inflate as and when needed, then control them with custom OnClickListeners (or OnTouchListeners)
I'd only resort to a custom view if you need to override a base view's method (such as the onLayout or draw() methods)

Related

DialogPreference in Full Screen Width

I created a custom dialog preference in my Android application, but I can not figure out how to get the dialog which is displayed to span the complete width of the display.
image of dialog with too much space on left and right side
I found many proposed solutions to get a normal Dialog in full screen mode
Android get full width for custom Dialog
https://gist.github.com/koocbor/88db64192638bff09aa4
http://blog.jimbaca.com/force-dialog-to-take-up-full-screen-width/
But setting the attributes via getWindow does not work:
#Override
public Dialog getDialog() {
Dialog dialog = super.getDialog();
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// or
// dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
return dialog;
}
And applying a full screen theme to my dialogs root element didn't do the job neither:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
[...]
android:theme="#style/FullscreenTheme">
Moreover I'm not able to access the onCreate Method (at least I don't know how) of the Dialog, to set the style there.
Did anyone had the same problem and figured out a solution for this very specific issue?
My layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:padding="0dp"
android:paddingTop="#dimen/preferences_dialog_def_padding"
android:paddingBottom="#dimen/preferences_dialog_def_padding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="-2dp"
android:background="#color/expandable_preference_divider"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/preferences_expandable_margin_top_bottom"
android:layout_marginTop="#dimen/preferences_expandable_margin_top_bottom">
<RelativeLayout
android:id="#+id/icon_wrapper_choose"
android:layout_width="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_height="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_marginBottom="0dp"
android:layout_marginEnd="#dimen/preference_expandable_icon_margin"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:layout_marginTop="0dp"
android:gravity="center">
<ImageView
android:layout_width="#dimen/preferences_expandable_icon_size"
android:layout_height="#dimen/preferences_expandable_icon_size"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:cropToPadding="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_settings_white_36dp"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/icon_wrapper_choose"
android:paddingBottom="#dimen/preferences_expandable_text_padding_top_bottom"
android:paddingTop="#dimen/preferences_expandable_text_padding_top_bottom"
android:text="#string/pref_wheel_circumference_choose"
android:textColor="#color/colorAccent"
android:textSize="#dimen/text_size_medium"
android:textStyle="bold"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_marginEnd="#dimen/preference_expandable_icon_margin"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:layout_height="wrap_content"
android:text="#string/etrto_hint"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingEnd="?android:attr/scrollbarSize"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:weightSum="3"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/etrto"/>
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingEnd="?android:attr/scrollbarSize"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:weightSum="3"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/manufacturer"/>
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="-2dp"
android:background="#color/expandable_preference_divider"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/preference_category_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/preferences_expandable_margin_top_bottom"
android:layout_marginTop="#dimen/preferences_expandable_margin_top_bottom">
<RelativeLayout
android:id="#+id/icon_wrapper_manual"
android:layout_width="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_height="#dimen/preferences_expandable_icon_wrapper_size"
android:layout_marginBottom="0dp"
android:layout_marginEnd="#dimen/preference_expandable_icon_margin"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:layout_marginTop="0dp"
android:gravity="center">
<ImageView
android:id="#+android:id/icon"
android:layout_width="#dimen/preferences_expandable_icon_size"
android:layout_height="#dimen/preferences_expandable_icon_size"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:cropToPadding="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_edit_white_36dp"/>
</RelativeLayout>
<TextView
android:id="#+android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/icon_wrapper_manual"
android:paddingBottom="#dimen/preferences_expandable_text_padding_top_bottom"
android:paddingTop="#dimen/preferences_expandable_text_padding_top_bottom"
android:text="#string/pref_wheel_circumference_manually"
android:textColor="#color/colorAccent"
android:textSize="#dimen/text_size_medium"
android:textStyle="bold"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingEnd="?android:attr/scrollbarSize"
android:layout_marginStart="#dimen/preference_expandable_icon_margin"
android:weightSum="2.5"
>
<EditText
android:id="#+id/pref_dialog_wheelcircumference_et"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:textColor="#color/colorFont"
android:textSize="#dimen/text_size_small"
android:inputType="number"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textAlignment="center"
android:text="#string/wheel_circumference_unit"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My custom preference class
public class WheelCircumferencePreference extends android.preference.DialogPreference {
private static String TAG = "CustomSwitchPreference";
private int mWheelCircumference;
public static int WHEEL_CIRCUMFERENCE_DEFAULT = 2125;
private int mDialogLayoutResId = R.layout.pref_dialog_wheelcircumference;
public WheelCircumferencePreference(Context context) {
this(context, null);
}
public WheelCircumferencePreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.dialogPreferenceStyle);
}
public WheelCircumferencePreference(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
setLayoutResource(R.layout.custom_preference);
setDialogLayoutResource(mDialogLayoutResId);
setPositiveButtonText(getContext().getString(R.string.dialog_save));
setNegativeButtonText(getContext().getString(R.string.dialog_cancel));
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
// Default value from attribute. Fallback value is set to WHEEL_CIRCUMFERENCE_DEFAULT.
return a.getInteger(index, WHEEL_CIRCUMFERENCE_DEFAULT);
}
#Override
protected void onSetInitialValue(boolean restorePersistedValue,
Object defaultValue) {
would load value from shared preferences
if (restorePersistedValue) {
mWheelCircumference = getPersistedInt(WHEEL_CIRCUMFERENCE_DEFAULT);
} else {
mWheelCircumference = (Integer) defaultValue;
persistInt(mWheelCircumference);
}
}
private EditText mWheelCircumferenceEt;
#Override
protected void onBindDialogView(View view) {
mWheelCircumferenceEt = view.findViewById(R.id.pref_dialog_wheelcircumference_et);
if (mWheelCircumferenceEt == null) {
throw new IllegalStateException("preference dialog view must contain" +
" a EditText with id 'pref_dialog_wheelcircumference_et'");
}
mWheelCircumferenceEt.setText(Integer.toString(mWheelCircumference));
super.onBindDialogView(view);
}
#Override
public Dialog getDialog() {
//Dialog dialog = super.getDialog();
// WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
//p.height = LinearLayout.LayoutParams.WRAP_CONTENT;
//dialog.getWindow().setAttributes(p);
return dialog;
}
#Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
String circumferenceText = mWheelCircumferenceEt.getText().toString();
try {
mWheelCircumference = Integer.parseInt(circumferenceText);
} catch (Exception e) {
NLog.e(TAG, "onDialogClosed - ", e);
mWheelCircumference = WheelCircumferencePreference.WHEEL_CIRCUMFERENCE_DEFAULT;
}
persistInt(mWheelCircumference);
}
}
Edit:
Actually I only want the dialog to span over the full width of the screen, not the height. If I would use a additional PreferenceFragment (as the DialogPreference is already embedded in a PreferenceFragment ) the "Dialog" (aka Fragment) would take the complete width and height (i guess).
I already implemented a solution without a DialogPrefrence, that works but is not exactly elegant
using just a normal EditTextPreference
adding an onPreferenceClickListener to this preference in my SettingsFragment Code
the ClickListener displays a simple Dialog
Example:
Preference preference = findPreference(EXAMPLE_PREFRENCE);
if (preference != null) {
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
// showDialog();
}
});
But as I have a lot of preferences which will display dialogs the code for the dialog creation and display bloads the SettingsFragment and makes it nearly unreadable. Therefore I thought it would be a nice solution to put the responsibility of displaying the dialog and handling the preference values to the Preference and the XML layout.
Unfortunately I got stuck with the "full width issue" mentioned above.
Note: fixed the code of getDialog as I tested different versions (also in combination with the xml theme set)
Finally I did find a solution for this problem:
Fetch the AlertDialog of the Preference in showDialog method
#Override
protected void showDialog(Bundle state) {
super.showDialog(state);
CustomDialogPreference.makeDialogFullScreen((AlertDialog) getDialog());
}
make it span the complete width:
public static void makeDialogFullScreen(AlertDialog d) {
NLog.d(TAG, "makeDialogFullScreen enter ");
if (d != null) {
ViewGroup.LayoutParams params = d.getWindow().getAttributes();
if (params != null) {
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
d.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
}
Try this in the onResume of your dialog.
// Store access variables for window and blank point
Window window = getDialog().getWindow();
Point size = new Point();
// Store dimensions of the screen in `size`
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
// Set the width of the dialog proportional to 75% of the screen width and height
window.setLayout((int) (size.x * 0.75), (int) (size.y * 0.75));
window.setGravity(Gravity.CENTER);
// Call super onResume after sizing
Adjust accordingly for 100%. It works great for a dialogFragment. Haven't tried it for your case though.
Wait, you're not looking for the bog-standard 'Pref settings user options appear in a dialog' thing are you? That's almost definitely already done in AndroidStudio's add activity...> Settings Activity in boiler plate, check it out, or look for sample settings apps
Anyway, I do actually have a fullscreen dialog in my app, although it purposely doesn't fill the full screen, and I actually use an activity with some fragments now instead.
Personally I think this is what your problem is, I remember having this exact issue when I first needed a dialog like this. You should just use activities and have up navigation (if you want a full screen "popup" type thing you could use the Navigation pattern that makes the home/up button an 'X' instead of a '<');
Or anything else, you don't need to have a dialog explicitly, and if you do then extend activity or dialog and get what you want.
Here's my activity stuff in case it's any use
my theme:
<style name="AppTheme.FullScreenDialog"
parent="#style/Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>
</style>
my onCreate gist:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
...
requestWindowFeature(Window.FEATURE_NO_TITLE);
...
super.onCreate(savedInstanceState);
setContentView(getConcreteContentView());
ButterKnife.bind(this);
setUpUIComponents();
...
}
my general layout gist:
<CoordinatorLayout>
<AppBarLayout>
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="#+id/container_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:paddingTop="6dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:id="#+id/container_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_security_word"
android:paddingEnd="18dp"
android:paddingStart="18dp" />
<RelativeLayout
android:id="#+id/container_security"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/container_recycler"
android:minHeight="150dp"
android:paddingEnd="18dp"
android:paddingStart="18dp"
android:visibility="visible" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/security_container"
android:layout_centerHorizontal="true"
android:contentDescription="#string/app_name"
android:minHeight="50dp"
android:scaleType="centerInside" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Bon Chance!

Distorted App screen - Android

Our app looks like this on Huawei 8510, and android version is 2.3.5.
This screenshot came from our customer. We disabled screenshot on app, so our customer used another phone to take screenshot.
How is it possible?
this is layout code that i think there is no problem.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<com.asdasd.asdasd.controls.CMScrollView
android:id="#+id/scvAgreement"
style="#style/pm_ContentStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:fadingEdge="none"
android:paddingLeft="0dp"
android:paddingRight="0dp">
<TextView
android:id="#+id/tvAgreement"
style="#style/pm_TextStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="#dimen/pm_content_padding_left"
android:layout_marginRight="#dimen/pm_content_padding_right"
android:gravity="center"
android:text=""
android:typeface="monospace" />
</com.asdasd.asdasd.controls.CMScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
style="#style/pm_ButtonStyle_Standart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_btn_black"
android:gravity="center"
android:onClick="onBtnNegativeClicked"
android:text="#string/pgPrivacy_btnNotAccept"
android:textSize="#dimen/pm_middle_text_size"
android:singleLine="false"/>
<Button
android:id="#+id/btnSendAgreement"
style="#style/pm_ButtonStyle_Standart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/pm_button_margin_left"
android:layout_weight="1"
android:background="#drawable/selector_btn_black"
android:enabled="false"
android:gravity="center"
android:onClick="onBtnSendAgreement"
android:text="#string/pgPrivacy_btnSendAgreement"
android:textSize="#dimen/pm_middle_text_size"
android:singleLine="false"/>
<Button
android:id="#+id/btnPositive"
style="#style/pm_ButtonStyle_Standart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/pm_button_margin_left"
android:layout_weight="1"
android:background="#drawable/selector_btn_orange"
android:enabled="false"
android:gravity="center"
android:onClick="onBtnPositiveClicked"
android:text="#string/pgPrivacy_btnAccept"
android:textSize="#dimen/pm_middle_text_size"
android:singleLine="false"/>
</LinearLayout>
And also, our custom listView
public class CMScrollView extends ScrollView {
public static final String LOG_TAG = "com.asdasd.asdasd.controls.CMScrollView";
private OnScrollViewReachedToEnd onScrollViewReachedToEnd;
public CMScrollView(Context context, AttributeSet st) {
super(context, st);
}
public OnScrollViewReachedToEnd getOnScrollViewReachedToEnd() {
return onScrollViewReachedToEnd;
}
public void setOnScrollViewReachedToEnd(OnScrollViewReachedToEnd onScrollViewReachedToEnd) {
this.onScrollViewReachedToEnd = onScrollViewReachedToEnd;
}
#Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = (View) getChildAt(getChildCount() - 1);
int diff = (view.getBottom() - (getHeight() + getScrollY() + view.getTop()));// Calculate the scrolldiff
if (diff == 0) { // if diff is zero, then the bottom has been reached
if (ApplicationController.DEBUGMODE)
Log.d(CMScrollView.LOG_TAG, "MyScrollView: Bottom has been reached");
if (onScrollViewReachedToEnd != null) {
onScrollViewReachedToEnd.onReachedToEnd(this);
}
}
super.onScrollChanged(l, t, oldl, oldt);
}
public interface OnScrollViewReachedToEnd {
void onReachedToEnd(IngScrollView scrollView);
}}
on AndroidManifest GL Version:
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
One of my friend found a promising Answer after a lot of time & search.
The same kind of issue (Flickering in GUI) is solved by simply Turn OFF the Developer Options
Because if you enable hardware overlay , GPU will be takes care of UI & sometimes there will be some conflict in GUI rendering.

Fixed buttons between scrollable content

i'm struggling with scrollview.
I want to display content (1) then buttons (2) and finally content (3).
When i'm scrolling, i want the buttons to be fixed at the top of my activity.
I can't post image, so it would be :
1
2
3
I scroll
2 (fixed at top)
3 (this part start to scroll when the 1 disapear at top)
I scroll
2 (fixed at top)
3 (still scrollable).
I m curently using this :
<ScrollView
android:id="#+id/sv_entete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_bar"
android:background="#color/gris_blanc" >
<RelativeLayout
android:id="#+id/layout_conteneur"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
// Some image etc.
</RelativeLayout>
</ScrollView>
<Button
android:id="#+id/btn_presentation_has_to_stay_on_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/sv_entete" />
<Button
android:id="#+id/btn_analyze_has_to_stay_on_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/sv_entete" />
<ScrollView
android:id="#+id/sv_presentation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btn_presentation"
android:background="#color/gris_blanc" >
<RelativeLayout
android:id="#+id/layout_presentation"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
// Some image etc
</RelativeLayout>
</ScrollView>
But only the second part scroll (3), the first part (1) is (obviously) not linked with the second scrollview, but i can't figured out by myself.
Any help plz ?
Looks like there's no way to implement it right out of the box. So, You need to create custom components.
Unfortunately, there's not much details in the question, so I'm not able to suggest which way is better for your case.
However, below two variants I can think of:
Have two button panels in the same layout. Initially only one panel should be visible (one inside scroll view). During scrolling, it first panel reaches top, You'll need to make second one visible. And make it invisible, when scroll up (and first one become visible inside ScrollView). It would increase complexity of layout and coe little bit (e.g. You'll need to listen two sets of buttons instead of one). In simple test application I haven't observed any critical issues / UI artifacts using this implementation,
For that you need to create separate layout for buttons panel, e.g. buttons_panel.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_presentation_has_to_stay_on_top"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="left button"/>
<Button
android:id="#+id/btn_analyze_has_to_stay_on_top"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="right button"/>
</LinearLayout>
Then main activity layout might look like the following (activity_main.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.alexstarc.testapp.ObservableScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/sv_entete">
<RelativeLayout
android:id="#+id/layout_conteneur"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/image_1"
android:layout_alignParentTop="true"
android:background="#android:color/holo_blue_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/image_1_1"
android:layout_centerHorizontal="true"
android:layout_below="#id/image_1"
android:src="#drawable/img1"/>
<include
layout="#layout/buttons_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/middle_button_panel"
android:layout_below="#id/image_1_1" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/image_2"
android:layout_below="#id/middle_button_panel"
android:background="#android:color/holo_green_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 2"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="350dp"
android:id="#+id/image_3"
android:layout_below="#id/image_2"
android:src="#drawable/img3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/image_4"
android:layout_below="#id/image_3"
android:background="#android:color/holo_orange_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 4 (1)"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/image_5"
android:layout_below="#id/image_4"
android:background="#android:color/holo_purple"
android:gravity="center"
android:textSize="30sp"
android:text="Image 5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="350dp"
android:id="#+id/image_6"
android:layout_below="#id/image_5"
android:background="#android:color/holo_blue_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 6"/>
<TextView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/image_7"
android:layout_below="#id/image_6"
android:background="#drawable/img1"
android:gravity="center"
android:textSize="30sp"
android:text="Image 7"/>
<TextView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/image_8"
android:layout_below="#id/image_7"
android:background="#android:color/holo_orange_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/image_9"
android:layout_below="#id/image_8"
android:background="#android:color/holo_purple"
android:gravity="center"
android:textSize="30sp"
android:text="Image 9"/>
<TextView
android:layout_width="match_parent"
android:layout_height="350dp"
android:id="#+id/image_10"
android:layout_below="#id/image_9"
android:background="#android:color/holo_blue_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 10"/>
<TextView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/image_11"
android:layout_below="#id/image_10"
android:background="#drawable/img1"
android:gravity="center"
android:textSize="30sp"
android:text="Image 11"/>
</RelativeLayout>
</com.alexstarc.testapp.ObservableScrollView>
<include
layout="#layout/buttons_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/top_button_panel"
android:layout_alignParentTop="true"
android:visibility="gone" />
</RelativeLayout>
There's special custom ViewGroup - ObservableScrollView. It's needed, because ScrollView doesn't have any listeners for observing scroll position, but we need to detect then buttons panel inside it become visible. Here's its draft code (please note, I haven't done any optimization or full testing):
public class ObservableScrollView extends ScrollView {
private int mTrackViewId = 0;
private View mTrackView = null;
private int mTrackViewTop = 0;
private int mTrackViewDown = 0;
private OnViewReachTop mTopReachListener = null;
/* Helper interface to notify listener about */
public interface OnViewReachTop {
/**
* Called then View riches top
*/
void onReachTop();
/**
* Called then view leaves top position
*/
void onLeaveTop();
}
public ObservableScrollView(final Context context) {
super(context);
}
public ObservableScrollView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public ObservableScrollView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}
/**
* Sets view id to be tracked and corresponding callback
*
* #param viewId id of view to be trackked
* #param listener {#link com.alexstarc.testapp.ObservableScrollView.OnViewReachTop} to be called
*/
public void setViewTopTracking(final int viewId, final OnViewReachTop listener) {
mTrackView = findViewById(viewId);
if (mTrackView == null) {
throw new IllegalArgumentException("Passed view id should be from child of this scroll view");
}
mTrackViewId = viewId;
mTopReachListener = listener;
}
#Override
protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
if (mTrackViewDown == 0 && mTrackViewTop == 0) {
mTrackViewDown = mTrackView.getBottom();
mTrackViewTop = mTrackView.getTop();
}
super.onScrollChanged(l, t, oldl, oldt);
if (getScrollY() >= mTrackViewTop) {
mTopReachListener.onReachTop();
} else if (getScrollY() <= mTrackViewDown) {
mTopReachListener.onLeaveTop();
}
}
}
And activity code, which will observe current scroll position and show / hide second buttons panel:
public class MyActivity extends Activity implements ObservableScrollView.OnViewReachTop {
private static final String TAG = "MyActivity";
private View mButtonsPanel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonsPanel = findViewById(R.id.top_button_panel);
((ObservableScrollView)findViewById(R.id.sv_entete)).setViewTopTracking(R.id.middle_button_panel, this);
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.action_bar, menu);
return true;
}
#Override
public void onReachTop() {
mButtonsPanel.setVisibility(View.VISIBLE);
}
#Override
public void onLeaveTop() {
mButtonsPanel.setVisibility(View.GONE);
}
}
Second way: take ScrollView code, analyze it and make possible to fix some view on the top. This way is more complex, but possible to implement. Please, investigate it (if first one is not applicable to your code / layout) and ask additional questions if face some particular problems with it.

I need to create layout from an image

I have the following picture and I want create layout like it, and I need in place of the rectangles to put EditText views. But I can't figure out which is the best way I can achieve this layout, should I make background of the lines or I make relative layout and try align things?
So if someone is experienced with such layouts, please tell how can I do this.
Thanks in advance.
Looking at that image, it looks like you are trying to cram a lot of EditText's into a single screen. That's a pretty unusual layout... unless your application has a very, very specific use case in mind where another layout is inadmissible, I'd advise you to try using one of the more "standard" layouts in Android, as that will likely enhance usability.
However, if you decide that that's really what you need, then I'd take a look at GridLayout, where the rightmost component is expandable and contains a LinearLayout or something like that.
That layout is relatively simple to make, it will be a lot more work to make it work across the large number of Android devices. For example:
<?xml version="1.0" encoding="utf-8"?>
<com.luksprog.ds.views.RelativeLayoutExtension xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/et10"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="X" />
<EditText
android:id="#+id/et9"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="#id/et10"
android:layout_marginBottom="25dp"
android:layout_toLeftOf="#id/et10"
android:text="9" />
<EditText
android:id="#+id/et8"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_below="#id/et10"
android:layout_marginTop="25dp"
android:layout_toLeftOf="#id/et10"
android:text="8" />
<EditText
android:id="#+id/et7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"
android:layout_toLeftOf="#id/et9"
android:text="7" />
<LinearLayout
android:id="#+id/et65wrapper"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/et7"
android:orientation="vertical"
android:paddingLeft="10dp" >
<EditText
android:id="#+id/et6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="6" />
<EditText
android:id="#+id/et5"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="5" />
</LinearLayout>
<LinearLayout
android:id="#+id/et43wrapper"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/et65wrapper"
android:orientation="vertical"
android:paddingLeft="10dp" >
<EditText
android:id="#+id/et4"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="4" />
<EditText
android:id="#+id/et3"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="3" />
</LinearLayout>
<LinearLayout
android:id="#+id/et21wrapper"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/et43wrapper"
android:orientation="vertical"
android:paddingLeft="10dp" >
<EditText
android:id="#+id/et2"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="2" />
<EditText
android:id="#+id/et1"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="1" />
</LinearLayout>
</com.luksprog.ds.views.RelativeLayoutExtension>
Where the RelativeLayoutExtension is a class extending the RelativeLayout class like this:
public class RelativeLayoutExtension extends RelativeLayout {
private LinearLayout mFirstLinear;
private LinearLayout mSecondLinear;
private LinearLayout mLastLinear;
private EditText mUpperEditText;
private EditText mLowerEditText;
private Paint mPaint;
public RelativeLayoutExtension(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(2.0f);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
mFirstLinear = (LinearLayout) findViewById(R.id.et21wrapper);
mSecondLinear = (LinearLayout) findViewById(R.id.et43wrapper);
mLastLinear = (LinearLayout) findViewById(R.id.et65wrapper);
mUpperEditText = (EditText) findViewById(R.id.et9);
mLowerEditText = (EditText) findViewById(R.id.et8);
}
#Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
final int leftFirst = mFirstLinear.getLeft();
final int topFirst = mFirstLinear.getTop();
final int middleFirst = (mFirstLinear.getBottom() - mFirstLinear
.getTop()) / 2;
final int lastRight = mLastLinear.getRight();
canvas.drawLine(leftFirst, topFirst + middleFirst, lastRight, topFirst
+ middleFirst, mPaint);
final int rightFirst = mFirstLinear.getRight();
final int bottomFirst = mFirstLinear.getBottom();
canvas.drawLine(rightFirst, topFirst, rightFirst, bottomFirst, mPaint);
final int rightSecond = mSecondLinear.getRight();
canvas.drawLine(rightSecond, topFirst, rightSecond, bottomFirst, mPaint);
final int leftUpperEdit = mUpperEditText.getLeft();
final int topUpperEdit = mUpperEditText.getTop();
final int middleUpperEdit = (mUpperEditText.getBottom() - mUpperEditText
.getTop()) / 2;
canvas.drawLine(lastRight, topFirst + middleFirst, leftUpperEdit,
topUpperEdit + middleUpperEdit, mPaint);
final int leftLowerEdit = mLowerEditText.getLeft();
final int topLowerEdit = mLowerEditText.getTop();
final int middleLowerEdit = (mLowerEditText.getBottom() - mLowerEditText
.getTop()) / 2;
canvas.drawLine(lastRight, topFirst + middleFirst, leftLowerEdit, topLowerEdit
+ middleLowerEdit, mPaint);
}
}
The lines aren't quite centered, it was just an example.
If you plan to use this layout on a single device for which you know its dimensions the layout will work as you only need to calculate the dimensions once outside of the code. If you plan this as a general layout things will get pretty ugly because you'll need to make a lot of calculations to properly position the views and draw the lines so you may want to rethink your approach. Also I hope that you want to use this layout on large screen devices, because cramming six EditTexts along with spaces on a portrait smartphone will not work pretty well.
You can create it with standard layouts.
The main layout is a FrameLayout.
The background is a image with some lines (or shapes to display lines).
The active layout is a RelativeLayout to show all rectangles.

Android: Last line of textview cut off

I have a horizontal LinearLayout containing a TextView followed by a Spinner next to it. This LinearLayout is dynamically inflated multiple times in a fixed vertical LinearLayout contained within a RelativeLayout.
The problem is that since I switched from Theme.light to Theme.holo.light, the last line of the TextView gets cut in half. This happens when the dynamic text is long and spans more than one row.
I have been able to fix this by adding bottom padding to the horizontal LinearLayout containing the TextView and Spinner.
This does not feel like a fix, but more of a hack. Can someone please give me some advice on how to properly fix this?
I have also read some other questions, but none seem to help.
Horizontal Linear layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="TextView"/>
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Relative layout where above layout is dynamically inflated at Linear Layout with id ll2_7:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/relLayoutButtonNext"
android:layout_below="#id/textView1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="30dp"
android:text="2.7" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#id/textView10"
android:text="#string/question2_7" />
<LinearLayout
android:id="#+id/ll2_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView11"
android:layout_below="#+id/textView11"
android:orientation="vertical" android:layout_marginBottom="20dp">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
EDIT:
Here is the complete layout xml for above:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
style="#style/question_section_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/question2_header" />
<RelativeLayout
android:id="#+id/relLayoutButtonNext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/bottomBar"
android:paddingBottom="3dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="3dp" >
<Button
android:id="#+id/buttonNext"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="nextStep"
android:text="Next Section"
android:textSize="20sp" />
<Button
android:id="#+id/buttonPrevious"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="previousStep"
android:text="Previous Section"
android:textSize="20sp" />
</RelativeLayout>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/relLayoutButtonNext"
android:layout_below="#id/textView1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="30dp"
android:text="2.7" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#id/textView10"
android:text="#string/question2_7" />
<LinearLayout
android:id="#+id/ll2_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView11"
android:layout_below="#+id/textView11"
android:orientation="vertical" android:layout_marginBottom="20dp">
</LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView10"
android:layout_below="#+id/ll2_7"
android:text="2.8" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll2_7"
android:layout_toRightOf="#+id/textView10"
android:text="#string/question2_8" android:layout_marginBottom="10dp"/>
<LinearLayout
android:id="#+id/ll2_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:layout_marginBottom="20dp"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/ll2_8"
android:text="2.9" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll2_8"
android:layout_toRightOf="#+id/textView10"
android:text="#string/question2_9" android:layout_marginBottom="10dp"/>
<LinearLayout
android:id="#+id/ll2_9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_toRightOf="#+id/textView10"
android:orientation="vertical" android:layout_marginBottom="20dp">
</LinearLayout>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/ll2_9"
android:text="2.10" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll2_9"
android:layout_toRightOf="#+id/textView10"
android:text="#string/question2_10" android:layout_marginBottom="10dp"/>
<LinearLayout
android:id="#+id/ll2_10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView7"
android:layout_marginBottom="20dp"
android:layout_toRightOf="#+id/textView10"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/ll2_10"
android:text="2.11" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll2_10"
android:layout_toRightOf="#+id/textView10"
android:text="#string/quesiton2_11" android:layout_marginBottom="10dp"/>
<LinearLayout
android:id="#+id/ll2_11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView9"
android:layout_below="#+id/textView9"
android:orientation="vertical" android:layout_marginBottom="20dp">
</LinearLayout>
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_below="#+id/ll2_11"
android:text="2.11.1" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll2_11"
android:layout_toRightOf="#+id/textView10"
android:text="#string/question2_11_1" android:layout_marginBottom="10dp"/>
<LinearLayout
android:id="#+id/ll2_11_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView13"
android:layout_toRightOf="#+id/textView10"
android:orientation="vertical" android:layout_marginBottom="20dp">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I applied a LayoutGravity to the TextView item:
android:layout_gravity="fill"
I've encountered the same cut-off issue as shown at the screenshot. It is caused by the baseline alignment in the horizontal LinearLayout. TextView and Spinner have different baselines due to font size difference. To fix the issue it is needed to disable baseline alignment for the layout by setting:
android:baselineAligned="false"
or in the code:
layout.setBaselineAligned(false);
I had the same problem, and found that simply adding
android:includeFontPadding="false"
the final line of text no longer had its descenders clipped.
I added some dummy space after text by adding
textView.setText(firstString+"\n");
I tried all other solution.But this was the only solution worked for me
I found a different solution by extending TextView and adding a custom Class like this:
public class AdaptingTextView extends TextView {
public AdaptingTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public AdaptingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AdaptingTextView(Context context) {
super(context);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// set fitting lines to prevent cut text
int fittingLines = h / this.getLineHeight();
if (fittingLines > 0) {
this.setLines(fittingLines);
}
}
}
Put the problematic textview inside a framelayout. I think the text view is not calculated correctly because of the sibling view, Spinner.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout
android:layout_width="150dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="TextView"/>
</FrameLayout>
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
When this occurs, you should ensure that the TextView is not growing larger than it's container -
If a TextView is set to wrap_content and it's container (or an ancestor container) doesn't leave room for the TextView to grow into it can be occluded.
If that's not the case, it's also possible the onMeasure() of the TextView sometimes doesn't correctly measure the tails of letters, non-latin characters or the effects from text being italic. You can correct for this by setting a global style for your TextView so it will be picked up without needed to change your entire code base:
Ensure that you're application/activities use a custom theme like so:
<style name="Custom" parent="#android:style/Theme.Light">
<item name="android:textViewStyle">#style/Custom.Widget.TextView</item>
</style>
<style name="Custom.Widget.TextView" parent="#android:style/Widget.TextView">
<item name="android:gravity">fill</item>
<item name="android:padding">1sp</item>
</style>
The answer by #Rynadt was really helpful in getting to the above stage. Setting the gravity of the Text inside the View ensures on some devices that occlusion never takes place (The text is correctly fitted inside the view), on others a helping hand with padding of an sp value, ensures that the tails et al are accounted for with a TextSize specific value.
My solution was close to the accepted one, but I had to change it to
android:layout_gravity="fill_vertical"
instead. Otherwise the other rows would have been stretch as well with added line breaks at random places. For example, the biggest row had 4 lines, so another row was changed from
this is a testphrase
to
thi
s is
a testph
rase
try with removing android:paddingBottom="20dp"
from
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
getViewTreeObserver().addOnGlobalLayoutListener does not work in a recycler view. If you're using a recycler, use View.addOnLayoutChangeListener:
I found that the ellipsizing I defined for textView in xml was not always reflected so I programmatically set it before reassigning the text property. This worked for me.
textView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
textView.removeOnLayoutChangeListener(this);
float lineHeight = textView.getLineHeight();
int maxLines = (int) (textView.getHeight() / lineHeight);
if (textView.getLineCount() != maxLines) {
textView.setLines(maxLines);
textView.setEllipsize(TextUtils.TruncateAt.END);
// Re-assign text to ensure ellipsize is performed correctly.
textView.setText(model.getText());
}
}
});
If you have this problem and your TextView is inside a RelativeLayout, try switching the RelativeLayout for a LinearLayout.
That fixed the problem for me
You can use a global layout listener for a TextView in any type of ViewGroup.
final TextView dSTextView = (TextView)findViewById(R.id.annoyingTextView);
dSTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
dSTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
float lineHeight = dSTextView.getLineHeight();
int maxLines = (int) (dSTextView.getHeight() / lineHeight);
if (dSTextView.getLineCount() != maxLines) {
dSTextView.setLines(maxLines);
}
}
});
You can read more about it here
I know it's so late, but this is work like charm for me.
add this code to your textview
android:ellipsize="marquee"
android:layout_weight="1"
I think there is very little you can do to get this working by altering the layouts. As I have found that some methods work only in some cases. I think it depends on the entire layout hierarchy and is not a one-size-fits-all solution. I have also noticed that it happens especially when you have a different font that you want to set to the TextView.
A sure shot method that I have experimented and tested is that you can set the font attributes in code after the view is inflated. I am assuming that you have a font in the assets/fonts folder that you want to you.
For eg in a Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_view, container, false);
TextView tv = (TextView)view.findViewById(R.id.text_view);
tv.setText("Insert text that needs to be displayed");
AssetManager assetManager = getContext().getAssets();
Typeface typeFace = Typeface.createFromAsset(assetManager, "Fonts/OpenSans-Light.ttf");
tv.setTypeface(typeFace , 0); // 0 is normal font
tv.setPadding(10, 0, 10, 0); // This is not mandatory
}
And in an Activity:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(Resource.Layout.main_activity);
TextView tv = (TextView)this.findViewById(R.id.text_view);
tv.setText("Insert text that needs to be displayed");
AssetManager assetManager = getContext().getAssets();
Typeface typeFace = Typeface.createFromAsset(assetManager, "Fonts/OpenSans-Light.ttf");
tv.setTypeface(typeFace , 0); // 0 is normal font
tv.setPadding(10, 0, 10, 0); // This is not mandatory
}
I have this same problem, and its very annoying.
It only happens with Arabic text.
If you make the label multi-line and adding a \n at the end of your string, it would fix it, but the problem is that there would be a big gap between this label and the object below it, due to the fact that this field now has a new empty line below it.
A custom control can be done to get around that. But overall, this is an annoying bug.
Best workaround for this is to add a dummy View of desired height (i.e. this will add padding itself) at the bottom of your view.
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<View
android:layout_width="match_parent"
android:layout_height="30dp"/>
</TableRow>
Like in my case I added one more table row at the bottom of the view. Hope this could help someone.
Add padding to the bottom of the text view:
android:paddingBottom="24dp"
I had the same problem and found a handy solution. I get the number of lines of the TextView after rendering and set the height according to the number of lines. Here is the code.
TextView textView = (TextView) layout.findViewById(R.id.textView);
textView.setText(this.text);
textView.post(new Runnable() {
#Override
public void run() {
int linesCount = textView.getLineCount();
textView.setLines(linesCount);
}
});
For me, this solution worked like a charm.
The height and width of my outermost layout was set dynamically, so the TextView contained within got it's text cut even if I set android:maxLines in my xml (for different devices it was behaving differently).
After trying out different methods, finally I got a solution that fixed my issue.
Textview:
public class CustomTextView extends androidx.appcompat.widget.AppCompatTextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextView(Context context) {
super(context);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// set fitting lines to prevent cut text
int fittingLines = h / this.getLineHeight();
if (fittingLines > 0) {
this.setLines(fittingLines);
this.setEllipsize(TextUtils.TruncateAt.END);
}
}
}
xml:
<com.myproject.android.customviews.CustomTextView
android:id="#+id/tv_partner_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:textColor="#color/white"
android:textSize="#dimen/text_small_medium" />
create theme as for particular language like style-ar which cut-off textview:
<style name="EnnodaCustomTextView" parent="Widget.AppCompat.TextView">
<item name="android:paddingTop">1dp</item>
<item name="android:paddingBottom">1dp</item>
</style>
Apply it in you AppTheme to reflect in overall app for padding bottom, as :
<item name="android:textViewStyle">#style/EnnodaCustomTextView</item>
Note : create same style name in default styles.xml with no item tags for padding..(where no need of extra padding )
I finally fixed it!
I try to add String to the TextView in Service and then call scrollTo(), the last line be cut off!
The scrollTo() should be call in "Runnable", like:
private ScrollView mScrollView;
public void scrollToBottom()
{
mScrollView = (ScrollView) findViewById(R.id.debug_textview_scrollview);
mScrollView.post(new Runnable()
{
public void run()
{
mScrollView.fullScroll(View.FOCUS_DOWN);
}
});
}
I think it because in the monent of call scrollTo() in service, the update of TextView is not ready.

Categories

Resources