There is TextView inside RecyclerView(Screen A) and inside a FrameLayout(Screen B), but both have maxLines=10.
In RecyclerView, the textview renders only 10 lines and additional text is ignored.
But the textview in LinearLayout, shows 10 lines but allows the user to scroll.
How to disable scroll and to just show maximum lines set using maxLines.
I have already tried,
android:isScrollContainer="false" - it doesn't work
android:enabled="false" - it disables the hyperlink clicks in the textview
Update:
I don't want textview in RecyclerView to scroll, I want to disable scrolling in LinearLayout only.
Tried the below line?
recyclerView.setNestedScrollingEnabled(false);
create a NoScrollTextView extends TextView like this :
public class NoScrollTextView extends TextView {
public NoScrollTextView(Context context) {
super(context);
}
public NoScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public void scrollTo(int x, int y) {
//do nothing
}
}
Then use this in xml as :
<com.example.NoScrollTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="10"/>
Just set in class
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setMovementMethod(null);
and in layout
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:maxLines="8"
android:text="Lorem Ipsum is simply dummy text of the dummy text of t printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy."
android:textSize="14sp" />
Related
How can i change those font attributes of a SwitchPreference?
I already tried to use app:layout attribute with the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#android:id/title"
style="#android:style/TextAppearance.Widget.TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="MY-FONT"
android:text="Title" />
<TextView
android:id="#android:id/summary"
style="#android:style/TextAppearance.Widget.TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="MY-FONT"
android:text="Summary" />
</LinearLayout>
That doesn't work well, because the switch was missing and everything looks a bit messy.
What is an easy (and working) way to customize the fontfamily and textSize?
You need to create a Custom Switch Preference by extending its main class
public class CustomSwitchPreference extends SwitchPreference {
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context);
}
#Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
TextView title1= (TextView) holder.findViewById(android.R.id.title);
title1.setTypeface(ResourcesCompat.getFont(getContext(),R.font.noto_sans_regular));
TextView title2= (TextView) holder.findViewById(android.R.id.summary);
title2.setTypeface(ResourcesCompat.getFont(getContext(),R.font.noto_sans_regular));
}
}
and use this is in your preferences xml
from
<SwitchPreference
.....
/>
to
<com.example.myapp.CustomSwitchPreference
....
/>
This Worked for me
To change the font family in android first you need to have the font in your app something like this
Your font goes in the font directory under res
Now to use it, your going in the right way
<Button
...
android:fontFamily="#font/nameOfFont"
... />
Let me now if it works for you
I'm facing this weird problem where I have a custom view (with custom drawing) overriding ToggleButton, and for some reason overriding the onDraw and draw methods do not prevent the parent class from drawing a part of it, which makes my view look like it's glitched.
This bug seems to be happening only with API level 25, on a physical device or on the emulator.
Using the following code for my custom toggle Button:
public class CustomToggleButton extends ToggleButton {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomToggleButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomToggleButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomToggleButton(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
//nope, not drawing anything
}
#Override
public void draw(Canvas canvas) {
//nope, not drawing anything
}
}
And the following simple XML layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:text="Bottom left: buggy custom ToggleButton.\nBottom right: regular ToggleButton." />
<test.com.togglebuttonbug.CustomToggleButton
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="bottom|left"
android:textOff="B"
android:textOn="A" />
<ToggleButton
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="bottom|right"
android:textOff="B"
android:textOn="A" />
</FrameLayout>
The result is the following:
If you look carefully at the screenshot, you can see that for the phone on the right, at the bottom left, instead of having nothing like the empty draw methods should do, you can see a piece of gray drawing.
Would anybody have an idea of what could be causing this? Is that a bug on Android N?
Hi I have created custom component for TextInputLayout. I want to set hint text to TextInputLayout. I am adding custom class which extends TextInputLayout into my root layout. I tried it in following way:
<android.support.design.widget.TextInputLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/gray"
android:theme="#style/editTextSelectedTheme"
android:layout_marginTop="#dimen/margin_10"
>
<EditText
android:id="#+id/form_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
public class FormEditText extends TextInputLayout
{
TextInputLayout view;
public FormEditText(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (TextInputLayout)inflater.inflate(R.layout.form_edit_text, this, true);
initUI();
}
public FormEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FormEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
And finally in my fragment:
FormEditText gadgetName = new FormEditText(getContext());
gadgetName.setHint(getResources().getString(R.string.gadget_name_hint));
formContainer.addView(gadgetName);
Above code adds new TextInputLayout in my view but not adding hint text for it. Am I doing anything wrong? Need Some help. Thank you.
I did few changes as per comment below.
public class FormEditText extends LinearLayout
view = inflater.inflate(R.layout.form_edit_text, this, true);
Now it si showing hint text as per required.But now it is not showing my edit text value.It is not taking any input value.
I have a slightly modified NumberPicker that I use and when the activity starts, the default value is already selected. This is annoying because it brings up the keyboard or brings up the dialog to cut/copy/paste. I did instruct the keyboard to stay down but it is still selected and will bring up the cut/copy/paste dialog. Once I scroll through the values the number is no longer selected and it acts normally. Does anyone know how to set the NumberPicker to not have anything selected at startup?
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class NumPicker extends NumberPicker
{
public NumPicker(Context context)
{
super(context);
}
public NumPicker(Context context, AttributeSet attrs)
{
super(context, attrs);
processAttributeSet(attrs);
}
public NumPicker(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
processAttributeSet(attrs);
}
/**
* Reads the xml, sets the properties
*/
private void processAttributeSet(AttributeSet attrs)
{
setMinValue(attrs.getAttributeIntValue(null, "min", 0));
setMaxValue(attrs.getAttributeIntValue(null, "max", 0));
setValue(4);
}
}
This is how it is used in the xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginRight="50dp"
android:layout_marginEnd="50dp"
android:layout_marginTop="40dp"
style="#style/CustomText"
max="100"
min="2"
The reason this happens is that the NumberPicker is the first (or only?) focusable element in your activity. You can trap the focus to your main layout view by adding this attribute to the root view:
android:focusableInTouchMode="true"
custom view inside of a custom viewGroup not visible, how can I get it to show up?
or is there a better way to do this that would work?
no compile or runtime errors but the view does not show up in the viewGroup, it is supposed to fill the area with color like the other views but it is white and the color for the view is not showing up inside of the CustomLayout
xml code, the first 2 views show up with no problems but the 3rd view that is nested inside of the CustomLayout does not show up, just white color area, the view inside not visible
CustomViewOne is a separate class file, CustomViewTwo and CustomViewThree are both nested inside the MainActivity class as static inner classes, and CustomLayout is a separate file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<com.example.customviewexample.CustomViewOne
android:layout_width="100dp"
android:layout_height="50dp" />
<view
class="com.example.customviewexample.MainActivity$CustomViewTwo"
android:layout_width="100dp"
android:layout_height="50dp" />
<com.example.customviewexample.CustomLayout
android:layout_width="100dp"
android:layout_height="50dp">
<view
class="com.example.customviewexample.MainActivity$CustomViewThree"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.example.customviewexample.CustomLayout>
</LinearLayout>
here is the code for the CustomViewThree, vary simple like the other custom Views it just fills the area with color, it is nested inside of the MainActivity so you have to use MainActivity$CustomViewThree to access it.
public static class CustomViewThree extends View {
public CustomViewThree(Context context) {
super(context);
}
public CustomViewThree(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomViewThree(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.GREEN);
}
}
and here is the code for the CustomLayout class
public class CustomLayout extends FrameLayout {
public CustomLayout(Context context) {
super(context);
init(context);
}
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public void init(Context context) {
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
custom view inside of a custom viewGroup not visible, how can I get it
to show up?
Your parent CustomLayout which wraps the child has a empty onLayout() method which makes the child to not appear. This method is important in a ViewGroup because it's used by the widget to place its children in it. So, you need to provide an implementation for this method to place the children(by calling the layout() method on each of them with the proper positions). As CustomLayout extends FrameLayout you could just call the super method to use FrameLayout's implementation or even better remove the overridden method(is there a reason for implementing it?).