How can I create a seekbar that has numbers above it. For example if the SeekBar has Max=10, it will have 0 to 10 above the seekbar.
I tried to do this by creating a custom view that sits above the Seekbar, this view extends linear layout(horizontal), n number textviews (n = seekbar max attribute) are added to the LinearLayout. The problem is that I couldnt get the numbers to sit directly above the 'notches' on the seekbar.
Am I approaching this in the correct way or should I be creating a customview that extends Seekbar and adding the numbers by drawing them onto a canvas? Could someone please give me a few pointers on how this problem can be solved.
Here is the code I have so far, the prpblem is, as stated above, no matter how I try to play around with the padding on both seekbar and my custom view, I cannot get the numbers to align perfectly with the seekbar notches(selected index)
public class SeekBarNumberIndicator extends LinearLayout
{
TextView[] mNumbers;
public SeekBarNumberIndicator(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
public SeekBarNumberIndicator(Context context, AttributeSet attrs)
{
super(context, attrs);
initView(context, attrs);
}
#SuppressLint("NewApi")
public SeekBarNumberIndicator(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
initView(context, attrs);
}
protected void initView(Context context, AttributeSet attrs)
{
super.setOrientation(LinearLayout.HORIZONTAL);
mNumbers = new TextView[11];
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
for(int x = 0; x < 11; x++){
mNumbers[x] = new TextView(context);
mNumbers[x].setLayoutParams(lp);
mNumbers[x].setTextColor(Color.BLUE);
mNumbers[x].setText(String.valueOf(x));
mNumbers[x].setGravity(Gravity.LEFT);
//mNumbers[x].setPadding(10, 0, 0, 0);
super.addView(mNumbers[x]);
}
}
}
UPDATE
I tried the link you posted, the same problem occurs, the sliding text view doesnt appear directly under the thumb, the offset gradually increases. What extra measures could I take to make the textview appear directly under the thumb(if I can solve this then Im sure the solution can be adapted to suite the custom view in my OP). I know I can start adding some logic to the padding that increases by some exponential value but this seems 'hacky' thanks
Related
This question already has answers here:
Android set height and width of Custom view programmatically
(10 answers)
Closed 2 years ago.
I am trying to resize a textbox dynamically using Java code. I want the width to not use wrap content but using a static number in dp. I want this to be done in Java code instead of the XML file. I want it like this is because I want to apply it to each item in the recyclerview. It needs to work with multiple screens sizes. It will work like a min Length for a textfield box size. If you know how to do this would be much appericaiated.
this is called auto sizing and it can be done by adding TextChangedListener which is a listener for Edit Tex. this listener watch the changes of editText and it has three different states. also you can create a component(custom view) and extend it from AppCompatTextView name as you want; in its initialization you can add below code:
public class CustomTextView extends AppCompatTextView {
Context ctx;
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ctx = context;
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
init();
}
public CustomTextView(Context context) {
super(context);
ctx = context;
init();
}
public void init() {
setOnTouchListener(null);
addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (getText().toString().length() > 10){
setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeSmall);
}
else if (getText().toString().length() > 5){
setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeMedium);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
also check these out, there is a tone of documentation for it:
Autosizing TextView Tutorial for Android
Autosizing TextViews
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hey i want to make a banner or ribbon similar to given photo with a textview or any layout in android using xml or java. I cant able to figure it out how to do that.
So please help me out its really needed.
To create a banner like that You need to do these things
Create a Shape drawable of an Inverted right angled triangle by tweaking this code
Set the background of the textView to this shape
Change the textView Gravity to right/end align
Here's a simple imageview with the text banner in the corner which I wrote for my own use. The same structure could be applied to any type of view.
public class BannerImageView extends ImageView {
private Paint mRibbonPaint;
private Paint mTextPaint;
private Paint mBoxPaint;
private float mScale;
private String mBannerText;
public BannerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
initPainters(attrs);
}
public BannerImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPainters(attrs);
}
private void initPainters(AttributeSet attrs) {
TypedArray attributes = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.BannerImageView, defStyleAttr, 0);
mBannerText = attributes.getString(R.styleable.BannerImageView_label);
mBoxPaint = new Paint();
int white = ContextCompat.getColor(getContext(), R.color.white);
mBoxPaint.setColor(white);
mBoxPaint.setAlpha(156);
mRibbonPaint = new Paint();
mRibbonPaint.setColor(ContextCompat.getColor(getContext(), R.color.banner_color));
mRibbonPaint.setAntiAlias(true);
mRibbonPaint.setStyle(Paint.Style.STROKE);
mRibbonPaint.setStrokeCap(Paint.Cap.BUTT);
mScale = getResources().getDisplayMetrics().density;
mRibbonPaint.setStrokeWidth(dp(16));
mTextPaint = new Paint();
mTextPaint.setColor(white);
mTextPaint.setTextSize( dp(12) );
}
/**
* Converts dp to px
* #param dp
* #return
*/
private float dp(float dp) {
return dp * mScale + 0.5f;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if ( !TextUtils.isEmpty(mBannerText) ) {
canvas.drawRect(0, 0, getRight(), getBottom(), mBoxPaint);
canvas.drawLine(-dp(16), dp(64), dp(64), -dp(16), mRibbonPaint);
canvas.save();
canvas.rotate(-45, 0, 0);
canvas.drawText(mBannerText, -dp(24), dp(38), mTextPaint);
canvas.restore();
}
}
Then declare the extra attributes you want to use in values/attrs.xml
<declare-styleable name="BannerImageView">
<attr name="label" format="string"/>
</declare-styleable>
Of course you use in your XML layouts as usual but remember the app namespace for the custom attributes
<com.my.package.BannerImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:label="Sold Out" />
I needed to correlate checkboxes with their position in the list view in the checkboxes' onClickListener. My first solution was to make a very short custom view that extended checkbox but had an extra variable (position) that would be set in getView().
public class ListCheckBox extends CheckBox {
private int position = -1;
public ListCheckBox(Context context)
{super(context);}
public ListCheckBox(Context context, AttributeSet attrs)
{super(context,attrs);}
public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr)
{super(context,attrs,defStyleAttr);}
#TargetApi(21)
public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{super(context,attrs,defStyleAttr,defStyleRes);}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
It worked, except it changed the checkboxes' color to black, and nothing I did (including changing android:buttonTint) could change it. For now my solution is a HashMap with a view key and integer value that keeps track of the checkboxes' and their positions, but if anyone has a less ugly solution or any idea as to why I couldn't change the color of the checkboxes, it would be very much appreciated.
You can use the method of setTag() to past a extra variable to a view,then you can use getTag() to get the extra variable. This does not need to custom CheckBox.
//set tag
checkBox.setTag(position);
//get tag
int position = (int)checkBox.getTag();
I want to have a custom TextView called MyTextView and i want to set a default TextSize in it's constructor only if no TextSize has been set in it's XML definition. How can i detect if a TextSize has been set in it's XML definition?
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
//How can i read TextSize from AttribureSet??
//if no TextSize has been set then SetTextSize(defaultTextSize);
}
}
Can any one help me please?
You can obtain style attributes like
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
String size = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");
}
}
Here's example:
final Resources.Theme theme = context.getTheme();
TypedArray array = theme.obtainStyledAttributes(attrs, R.styleable.YourStylable, R.attr.YourDefStyleAttr, 0);
Try this float size = new TextView(this).getTextSize();
Is there any way to put a line over text in Android? Since underlines and strikethroughs are possible (independent of font), it seems that overlines should be too.
I've tried using the combining overline symbol (pictured below), but it definitely looks funky. Is there a different way to achieve this?
I know this is not a perfect solution. but it will give some Idea...
public class OverLineTextView extends TextView {
private Paint paint;
public OverLineTextView(Context context) {
super(context);
init();
}
public OverLineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public OverLineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float width = getPaint().measureText(getText().toString());
canvas.drawLine(getTotalPaddingLeft(), getTotalPaddingTop() + 1,
getTotalPaddingLeft() + width, getTotalPaddingTop() + 1, paint);
}
}
After doing enough R & D, the only way for doing that is to create a ImageView and set it dynamically to a line image and set its width with respect to the width of the textview by using getWidth() and position above the textview and align left with respect to the textview
If you want a strikethrough on your textview ,you can do using paint flags
TextView textview1=(TextView) findViewById(R.id.textView1);
textview1.setPaintFlags(textview1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);