Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a XML layout that contain all my buttons and images and i want a moving cloud on the top of my layout. so i created a view and made my cloud move, however i couldnt link the view with layout. here is my view code

public class CloudView extends View {

Bitmap cloud;
int ChangingX;


public CloudView(Context context) {
    // TODO Auto-generated constructor stub
    super(context);
    cloud = BitmapFactory.decodeResource(getResources(), R.drawable.cloud);
    ChangingX = 50;

}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    canvas.drawBitmap(cloud, ChangingX , 50, null);
    if (ChangingX < canvas.getWidth())
        ChangingX += 2;
    else
        ChangingX = 50;
    invalidate();
}

}

and here is my MainActivity

public class MainActivity extends Activity {

CloudView myView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myView = new CloudView(this);
    setContentView(myView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

Im new at animation in android can u explain in details how i can link a View with layout. and if it wont work what other classes besides View that i can use.

Thanks for your time and consideration. and sorry for my bad English.

share|improve this question
Use layout inflater to add new view. – URAndroid Jun 8 at 5:21
can you please show in code how to use inflater, i have never used it before – Osama Espil Jun 8 at 5:27
see the answer below. – URAndroid Jun 8 at 5:40

2 Answers

Here is Android Developer Link might me useful for you.

Define Custom Attributes

How to define attribute :

To define custom attributes, add resources to your project. It's customary to put these resources into a res/values/attrs.xml file. Here's an example of an attrs.xml file:

<resources>
   <declare-styleable name="PieChart">
       <attr name="showText" format="boolean" />
       <attr name="labelPosition" format="enum">
           <enum name="left" value="0"/>
           <enum name="right" value="1"/>
       </attr>
   </declare-styleable>
</resources>

How to use in XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
 <com.example.customviews.charting.PieChart
     custom:showText="true"
     custom:labelPosition="left" />
</LinearLayout>

Read for more details do follow.

share|improve this answer

Use this:

View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.activity_my_vault, null,false);
        this.addContentView(viewToLoad);
share|improve this answer
i need to add this on MainActivity or View activity?! – Osama Espil Jun 8 at 5:47
where is your View Activity man. You have to add this in Main Activity. – URAndroid Jun 8 at 5:51
it gives me PointerNullException. and my MainActivity extends Activity shouldnt i have another class to extend View to do the animation and import onDraw. as i mentioned im new at this please be patient with me and i meant CloudView when i said my View activity – Osama Espil Jun 8 at 6:09
man change the name of the layout to which you want to inflate. Don't use the same name. – URAndroid Jun 8 at 6:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.