Android Custom Controls with textviews inside them - android

I am making a Custom control and I want to add TextViews inside it. How Can I do that ?
Any ideas?
The custom controls will be used to display electronic programming guide(EPG). My application is for google tv and will be used for channels listing and playback and EPG, in EPG screen i will show the time intervals and programs in each intervals , I want to use Textviews for Program names and to give them custom fonts and style.
Any kind of help will be appreciated,
Many thanks,
Here is screen shot of my custo control ? to add the texts styles i want to use textviews. I hope its clear now?

Try to build further upon this example code:
public class CustomView extends LinearLayout {
public CustomView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public CustomView(Context context) {
super(context);
}
public void addTextView(String text) {
TextView tv = new TextView(getContext());
tv.setText(text);
this.addView(tv);
this.invalidate(); //I think this is called implicitly, but just in case.
}
}
However, you have to do thing like margins, scrolling, layout and so on yourself, as your context does not give many clues.
EDIT: with the addition of some context, I suggest you use a TableLayout. Still, you can build that with the given example code.

Related

How to set an Android-X custom preference height?

I am trying to create a custom preference used in a fragment. That preference will have an icon, a long text and a hyperlink to link somewhere.
For that I create a class that extends androidx.preference.Preference
The constructor is this:
public MyCustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWidgetLayoutResource(R.layout.my_custom_preference_layout);
}
I get it to work correctly and all that, but the problem is that its height is way too short. The preference size doesn't fit its content. It seems like it is always the same size. About two lines of text only.
I have tried with
android:singleLine="false"
android:minLines="50"
but nothing. The preference is displayed way too short.
Is there any limitation in the height of custom preferences?
I use a RelativeLayout for the layout and add the controls I need to it.
that Relative Layout have these values:
android:layout_width="match_parent"
android:layout_height="wrap_content"
I have tried setting match_parent to both, even setting a fixed value, for example 500dp, but it is always displayed in small height.

Can't display custom View in Android Studio Layout Editor

I am struggling to get my Custom View to be displayed in Android Studio's Layout Editor.
My view is a simple subclass of RecyclerView with custom parameters. I use View.isInEditMode() to use the given attrs only when actually running the app:
public class CustomRecyclerView extends RecyclerView {
public CustomRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
if (isInEditMode()) {
return;
}
final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomRecyclerView, 0, 0);
/* ... */
}
However, previewing a layout containing this View results in an empty Preview pane (not even the phone's frame):
If I replace my View by a RecyclerView, the Preview works as expected:
I don't see what I should do here: if the RecyclerView is correctly displayed and all I do in my subclass's constructor is call super() and return if isInEditMode is true, then what could explain my View not being displayed?
What can I do to get my custom View displayed in Android Studio's Layout Editor?
As of today, opening the same project with Android Studio 2.1.2 does successfully display the UI in the Preview Pane. I can only guess this was a bug in the IDE and has been fixed since I asked this question.
For solving this problem use lower
API levels like 26
then rebuild the project.

WebView inside ScrollView seems to work fine, but warnings to not use - alternative?

Everything I've read (AFTER already making my app :( ) says something like "you should never use a WebView inside a ScrollView!". This is understandable because you could theoretically have 2 scrolling things which would make for odd usability.
But - so far, I've seen no adverse effects. Then again, each WebView I'm using doesn't require scrolling - maybe that makes it acceptable-use even though it's technically wrong?
Are there adverse effects that I'm just not noticing to due lack of testing on a specific version(s)?
If I can't use a WebView inside a ScrollView, how would I get the below layout (my current app):
LinearLayout
ScrollView
LinearLayout
TextView //Title of article
TextView //Subtitle of article
RelativeLayout
ImageView //Large Image (clickable to gallery)
ImageView //"more photos icon"
WebView // a small horizontal ad
TextView // actual article text
WebView //embedded HTML code ranging from iframe to video...etc
WebView //embedded HTML code ranging from iframe to video...etc
WebView // a small horizontal ad
LinearLayout
TextView //DB-driven "similar articles" list
WebView //disqus comments
Note: I realize it's "wrong" - but so are using <center> tags in HTML, and people still use them all the time effectively. The difference seems to be that there's an easy-and-better alternative to <center> - is there something similar for this scenario in Android? A somewhat-simple way to get the above?
This is understandable because you could theoretically have 2 scrolling things which would make for odd usability.
More specifically, ScrollView is as dumb as a box of rocks, and assumes it has full control over scrolling.
Then again, each WebView I'm using doesn't require scrolling
More accurately, it doesn't require scrolling on the devices that you have tested, and for the content that you have tested.
For example, DISQUS comment threads typically require scrolling on a desktop browser. One imagines that there will be comment threads that will be long enough to require scrolling on a mobile device, unless there is a scroll-free DISQUS embed you can use.
If I can't use a WebView inside a ScrollView, how would I get the below layout (my current app):
Get rid of everything and have a single WebView, with generated HTML content that contains all the stuff in your current structure. IOW, do what you would do in a Web site.
Don't use WebView.
Ads you can attach to application or use AdMob.
To video is VideoView.
Alternative to comments webview is make api (using php [POST] and sql).
Using webview isn't comlulsory.
I hope I helped
Placing the WebView in a ScrollView will work as long as the WebView isn't internally scrollable in the same direction as the ScrollView. Making the WebView not scroll internally can be achieved in a couple of ways:
the WebView's height (assuming the ScrollView is scrollable vertically) is WRAP_CONTENT,
the WebView has a fixed size and the content has reactive layout which will never be larger than the viewport (alternatively "overflow: hidden" could be used).
I don't think the above approaches will mix well with pinch-zooming so you'd best not enable that.
Make a Java class and extends with WebView
package com.mypackage.common.custom.android.widgets
public class TouchyWebView extends WebView {
public TouchyWebView(Context context) {
super(context);
}
public TouchyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onTouchEvent(MotionEvent event){
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(event);
}
}
and in layout
<com.mypackage.common.custom.android.widgets
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I hope this will solve your problem

Compound Controls forwarding State

Does anyone know what the appropriate mechanism is when creating a custom compound control to apply state changes from the container down to all the children? It would seem to me there should be a straightforward way to set up a ViewGroup to forward all state changes (pressed, enabled, etc.) to each child.
For example, if I create a custom widget along the lines of:
public class MyWidget extends RelativeLayout {
private TextView mTitleView, mValueView;
private ImageView mImageView;
public ValueButton(Context context) {
this(context, null);
}
public ValueButton(Context context, AttributeSet attrs) {
super(context, attrs);
//View setup, etc.
}
}
In many cases, there are drawable or color state lists attached to the children that I want to toggle when changes apply to the overall widget as a whole. What do I need to add to a widget such as this so that when, for example, I call MyWidget.setEnabled() or when MyWidget is pressed those state changes filter down the hierarchy?
Thanks!
Add android:duplicateParentState="true" to each child (perhaps iterating through the childs using getChildAt(int index))
http://developer.android.com/reference/android/view/View.html#attr_android:duplicateParentState
Edit: you will need to set it up in the xml
Note: in the current implementation, setting this property to true after the view was added to a ViewGroup might have no effect at all. This property should always be used from XML or set to true before adding this view to a ViewGroup.
Not sure if I'm going to be much help here, but it's an interesting problem for me to think about because I think I'm going to want to do something quite similar.
In terms of cascading state information down to child Views, could one possible approach be to only contain that state information in the parent ViewGroup, and in the child Views make use of getParent() to access that information?

Adding Buttons to FingerPaint Android API Sample1

Im kind of new to Android.
I am playing around with the Android FingerPaint API Sample. I have figured out how to add buttons and functions to the Menu, but how do I get buttons on the actual screen?
Is it possible to place buttons over the drawing surface? Or would I need a linear (Vertical) layout on the left, and place buttons in there. Either would be fine.
Help? Thanks.
The Android FingerPaint sample code does not use a layout; it instead just has a subclass of View called MyView, and then the Activity sets its content view to be an instance of MyView.
If you want to have more than one View on the screen, you'll need to use some sort of layout. You could use a LinearLayout if you want the MyView for painting to be above, below, or to the side of the buttons; if you want the buttons to be on top of the MyView, take a look at using a FrameLayout or RelativeLayout.
You can either then define the layout in XML or create it manually in code. The former is more flexible and maintainable, but there will be a few hiccups.
First, create a layout XML showing how you want your components to be laid out. For this example, we'll call it finger_paint.xml. Make sure you have a MyView in there somewhere, something like:
<view class="com.example.android.apis.graphics.FingerPaint$MyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Then, replace the line that looks like
setContentView(new MyView(this));
with
setContentView(R.layout.finger_paint);
Note that because MyView does not (yet) have the proper constructor for being instantiated by the LayoutInflater, this will not work yet, so let's fix that. Add an additional import near the top of the file:
import android.util.AttributeSet;
and then add an AttributeSet parameter to the constructor of MyView:
public MyView(Context c, AttributeSet as) {
super(c, as);
// rest of constructor is same as in the sample
}
You will also have to change MyView to be a static inner class of FingerPaint.
You may find the Building Custom Components document and the NotePad sample useful as you figure this out.
Good luck!
Are you trying to dynamically position your buttons?
If so, you can use setLayoutParams to set the layout properties of the button.

Categories

Resources