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 7905883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:40:24+00:00 2026-06-03T10:40:24+00:00

I am trying to create my custom view through xml, but the screen does

  • 0

I am trying to create my custom view through xml, but the screen does not show my view, s it is not inflating. My custom view xml is as:

 <?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/profileSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imgAdvertise"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@android:drawable/btn_minus" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/txtAdvertise"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="asdas" />
    </LinearLayout>

</ViewSwitcher>

I have my custom View class as:

public class MyCustomView extends View{

    TextView textAdvertise;
    ImageView imageAdvertise;
    View view;
    ViewSwitcher switcher ;

    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.main, null);

        }

        initialize();
    }

    public void initialize() {
        // TODO Auto-generated method stub

        textAdvertise = (TextView) view.findViewById(R.id.txtAdvertise);
        imageAdvertise = (ImageView) view.findViewById(R.id.imgAdvertise);
        switcher = (ViewSwitcher) view.findViewById(R.id.profileSwitcher);
        startAnimation();

    }

    public void startAnimation() {
        // TODO Auto-generated method stub

        new Thread() {
            public void run() {

                for (;;) {
                    try {

                        Thread.sleep(2000);
                        hRefresh.sendEmptyMessage(5);
                    } catch (Exception e) {
                    }
                }
            }
        }.start();
    }

    Handler hRefresh = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 5:
                switcher.showNext();
                // To go back to the first view, use switcher.showPrevious()
                break;
            default:
                break;
            }
        }
    };

}

and in my base xml layout where I have to show the View, is one button and and my View as:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:text="Button" 
    android:layout_weight="1"/>

<com.example.viewswitcher.MyCustomView 
    android:id="@+id/MyView"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"

    />

Can anyone please tell me what it is going wrong?

  • 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-03T10:40:25+00:00Added an answer on June 3, 2026 at 10:40 am

    You will see nothing because MyCustomView doesn’t have any content as you wrote it. First of all I would use one of the ViewGroups children instead of View(like LinearLayout, RelativeLayout etc):

    public class MyCustomView extends LinearLayout {
    
        TextView textAdvertise;
        ImageView imageAdvertise;
        View view;
        ViewSwitcher switcher ;
    
        public MyCustomView(Context context, AttributeSet attrs) {
            super(context);
        }
    
        public MyCustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (layoutInflater != null) {
                view = layoutInflater.inflate(R.layout.main, this, true);
            }
            initialize();
        }
    
         public MyCustomView(Context context, AttributeSet attrs, int theme) {
             super(context, attrs, theme);
         }
    // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a watermark text to my custom view, but the
I'm trying to create custom layout for android. It normally draws on screen, but
I'm trying to create a CGRect inside of a custom view (rectView) that will
Im trying to create a custom version of the RequiredAttribute to replace the built
Ok, I am trying to create a custom view called CheckedRelativeLayout . It's purpose
I'm trying to create a DialogFragment using a custom view in an AlertDialog .
Trying to create a custom component that gets it's layout from an XML file
I'm trying to create custom view that shows some info about it: Name, Id,
I'm trying to create a custom view for a spotify playlist. right now I
I am trying to create a custom view where a bitmap moves around with

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.