Tutorial on creating custom layouts for Android?

Paul Blaine picture Paul Blaine · Jun 17, 2011 · Viewed 7.4k times · Source

I'm at my wits end trying to figure this out and hope there is someone out there who has a solution.

I want to create a custom layout for an Android App. This should theoretically be straight forward enough, but I can find no useful information on how to do so.

I'm looking for: 1) A simple, step-by-step, tutorial on how to subclass the ViewGroup to create custom layouts. 2) A basic understanding of the inner mechanics of, for example, how to access child views, how and when to ask the child views to draw themselves, how to define the xml for a custom layout, how to access custom xml attributes etc.

I've looked all around the internet and I can only find small tidbits on it, and haven't even been able to get started creating a subclass.

I have found 1) A talk given at http://parleys.com/#id=2191&sl=3&st=5 but it cuts off after a few minutes asking for a subscription. 2) The API reference at http://developer.android.com/reference/android/view/ViewGroup.html but this doesn't give any clue as to how to realistically implement a subclass.

Answer

Nikola Lukic picture Nikola Lukic · Oct 19, 2016

I found this . It is complicated for nothing .

Tutorial

There is no more absolute layout in android support. I just wanna setX method . FrameLayout is best option . Look also : stackoverflow answer

FrameLayout allows you to setup position and dimensions similar to absolute . If you wanna go deeper you will need to use next methods :

// For x and y you can use onCreate

// Best way for setup Width AND Height - use post // post method means that UI is ready

  VEIW_MAIN = (ViewGroup) findViewById(R.id.activity_main);

   VEIW_MAIN.post(new Runnable() {
            @Override
            public void run() {

                LayoutParams paraBigText = BigText.getLayoutParams();
                Double d24 = 200.0;
                paraBigText.width = d24.intValue();
                BigText.setLayoutParams(paraBigText);
                BigText.setGravity( Gravity.CENTER_HORIZONTAL);

           }
       });

...

ViewTreeObserver vto = SOME_ELEMENT.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()       
{
    public boolean onPreDraw() {

         // changes here
        return true;
    }
}); 

...

If you need some fix use this :

 if (EKRAN.WIDTH() <= 480)
            {
                FIX_FOR_480_WIDTH();
            }
            else if (EKRAN.WIDTH() == 540) {
                FIX_FOR_540_WIDTH();
            }
            else if (EKRAN.WIDTH() == 720) {
                FIX_FOR_720_WIDTH();
            }
            else if (EKRAN.WIDTH() == 1080) {
                FIX_FOR_1080_WIDTH();
            }

...

 void FIX_FOR_540_WIDTH()
    {
        System.out.println("FIX FUNCTION FOR 540 low width !!!!!!!");
        SOMEELEMENT.setY( 0.865f * EKRAN.HEIGHT() );
    }

...

Copy SCREEN class from : Set position by percent - Android DisplayMetrics

In other way if you really wanna full custom order then you can use canvas2d and create own uniq UI ...