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

  • SEARCH
  • Home
  • 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 6848175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:50:09+00:00 2026-05-27T00:50:09+00:00

I have three files. The XML, the draw function and the main Activity. I

  • 0

I have three files. The XML, the draw function and the main Activity.
I have some LinearLayout in my XML file.

<LinearLayout android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1">
    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:background="#ef3"
                  android:id="@+id/img01"/>
    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:background="#E8A2B4"
                  android:id="@+id/img02"/>
</LinearLayout>

This is the draw function:

public class getBorder extends TextView {
    public getBorder(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();

        paint.setColor(android.graphics.Color.RED);

        canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
        canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
        canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
            this.getHeight() - 1, paint);
        canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
            this.getHeight() - 1, paint);
    }
}

And this is the main Activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final getBorder getBorder = new getBorder(this);
    final LinearLayout img01 = (LinearLayout) findViewById(R.id.img01);
    img01.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            getBorder.setWidth(100);
            getBorder.setHeight(100);
            img01.addView(getBorder);
        }
    });       
}

The program could draw border but the size doesn’t fit the LinearLayout. And when I click the LinearLayout again, the program crashed.

Also, I want to draw two circles in the center of the LinearLayout, but how could I figure out the center coordinates?

  • 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-05-27T00:50:09+00:00Added an answer on May 27, 2026 at 12:50 am

    Do you really need to do that programmatically?

    Just considering the title: You could use a ShapeDrawable as android:background…

    For example, let’s define res/drawable/my_custom_background.xml as:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
      <corners
          android:radius="2dp"
          android:topRightRadius="0dp"
          android:bottomRightRadius="0dp"
          android:bottomLeftRadius="0dp" />
      <stroke
          android:width="1dp"
          android:color="@android:color/white" />
    </shape>
    

    and define android:background=”@drawable/my_custom_background”.

    I’ve not tested but it should work.

    Update:

    I think that’s better to leverage the xml shape drawable resource power if that fits your needs. With a “from scratch” project (for android-8), define res/layout/main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/border"
        android:padding="10dip" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, SOnich"
            />
        [... more TextView ...]
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, SOnich"
            />
    </LinearLayout>
    

    and a res/drawable/border.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
       <stroke
            android:width="5dip"
            android:color="@android:color/white" />
    </shape>
    

    Reported to work on a gingerbread device. Note that you’ll need to relate android:padding of the LinearLayout to the android:width shape/stroke’s value. Please, do not use @android:color/white in your final application but rather a project defined color.

    You could apply android:background="@drawable/border" android:padding="10dip" to each of the LinearLayout from your provided sample.

    As for your other posts related to display some circles as LinearLayout’s background, I’m playing with Inset/Scale/Layer drawable resources (see Drawable Resources for further information) to get something working to display perfect circles in the background of a LinearLayout but failed at the moment…

    Your problem resides clearly in the use of getBorder.set{Width,Height}(100);. Why do you do that in an onClick method?

    I need further information to not miss the point: why do you do that programmatically? Do you need a dynamic behavior? Your input drawables are png or ShapeDrawable is acceptable? etc.

    To be continued (maybe tomorrow and as soon as you provide more precisions on what you want to achieve)…

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

Sidebar

Related Questions

I have some xml data contained in three files (Database.xml, Participants.xml, and ConditionTokens.xml). I
In Unix, I have got three main files. One of them is a library
I have three xml files of similar structure and I would like to use
I have three projects(C# libraries) namely A,B,C. All the 3 have 3-4 xml files(in
I have an xml file right now which has a TableLayout within a LinearLayout.
I have a XML file template like this that comes from some IC chips,
I have three xml files in my spring hibernate app Spring-Security.xml <security:authentication-manager> <security:authentication-provider user-service-ref=customUserDetailsService>
I have three files of xml <step> <Products> <Product UserTypeID=Country> <Name>Cyprus</Name> <Product UserTypeID=Resort> <Name>Argaka</Name>
I have some .xml files that are encoded in UTF-8 . But whenever I
I have three views main1.xml, main2.xml , main3.xml. In main.xml I'm putting three buttons.

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.