Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8097433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:45:39+00:00 2026-06-05T21:45:39+00:00

The following code is suppose to be doing this : – Displaying a string

  • 0

The following code is suppose to be doing this :
– Displaying a string using a standard TextView
– Displaying a string using a extended TextView. (This one overrides OnDraw and draws a line across the string, making it look like it was striked out)

The problem is that only the standard one is appearing.

Please check the following code :

ExpenseWatchActivity.java (This is the main activity)

package com.app.expensewatch;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Calendar;

public class ExpenseWatchActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int day,mon,yr;

        Calendar cal = Calendar.getInstance();

        day = cal.get(Calendar.DATE);
        mon = 1 + cal.get(Calendar.MONTH);
        yr = cal.get(Calendar.YEAR);

        String text = getResources().getString(R.string.hello, day , mon , yr);

        TextView tx1 = (TextView)findViewById(R.id.text1);
        tx1.setText(text);

        List1 list = (List1)findViewById(R.id.list1);
        list.setText(text);
    }
}

List1.java (This is the extended TextView)

package com.app.expensewatch;

import android.util.AttributeSet;
import android.widget.TextView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.app.expensewatch.R;

public class List1 extends TextView {

    public List1(Context context) {
        super(context);
    }

    public List1(Context context, AttributeSet attrs) {
        super( context, attrs );
    }

    public List1(Context context, AttributeSet attrs, int defStyle) {
        super( context, attrs, defStyle );
    }

    float ht = (float)getHeight();
    float wd = (float)getWidth();

    @Override
    protected void onDraw(Canvas canvas)
    {
        Paint divider = new Paint();
        divider.setColor(getResources().getColor(R.color.line1));
        canvas.drawLine(0,ht/2,wd,ht/2,divider);
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <com.app.expensewatch.List1
        android:id="@+id/list1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
       />
 </LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="app_name">ExpenseWatch</string>
   <string name="hello">Today is : %1$d / %2$d / %3$d</string>

</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="line1">#6456648f</color>

</resources>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:45:41+00:00Added an answer on June 5, 2026 at 9:45 pm

    When you set your width and height in the extended TextView, the view hasn’t been measured yet. Its width and height are zero. Override onMeasure() in your extended TextView, and set your width and height there.

    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        ht = getHeight();
        wd = getWidth();    
    }
    

    Always avoid setting a private variable that will hold a dimension in a view until after onMeasure() has been called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following code: foreach(string str in someObj.GetMyStrings()) { // do some
Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ...
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
Suppose that one has some lock based code like the following where mutexes are
Suppose I am doing the following: using (OracleConnection conn = new OracleConnection(connStr)) { OracleTransaction
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have the following code function myFunction(param, callback) { ... if (err) {
suppose that,we have following code auto_ptr<T> source() { return auto_ptr<T>( new T(1) ); }
Say suppose I have the following Java code. public class Example { public static
Suppose the following class public class Message { // some code } And a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.