Adding a LeadBolt App Ad - how to add a Runnable - android

The doc says I should add something like this to my code:
// create a new layout
LinearLayout layout = new LinearLayout(this);
/*
Add any elements to your view
*/
// make the view visible
this.setContentView(layout);
final Activity act = this;
// now add the banner or overlay to the app
layout.post(new Runnable() { //what about this line?
public void run() {
myController = new AdController(act, MY_LB_SECTION_ID);
myController.loadAd();
}
});
My layout is in xml and I have already defined setContentView(R.layout.config); I mean the layout.post(new Runnable() { line. How should I modify the code?

For anyone who is interested I have found the obvious solution:
RelativeLayout rellaymain = (RelativeLayout)findViewById(R.id.rellaymain);
final Activity act = this;
rellaymain.post(new Runnable() {
public void run() {
myController = new AdController(act, MY_LB_SECTION_ID2);
myController.setAsynchTask(true);
myController.loadAd();
}
//});
});
rellaymain is the layout of the .xml file.

Related

On Click Listener Inside a Handler

I have a Handler in my Loading Activity that delays my Intent to next Activity (which works perfectly).
What I want to do is, after the delay ends I want to have an on Click Listener that covers all the screen, but it's not working!
I've tried public void OnClick and View.onClickListener none of them work inside the Handler
How can i fix this?
My Handler code:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
overridePendingTransition(R.anim.animin, R.anim.animout);
final Intent mainIntent = new Intent(LoadingActivity.this, StartActivity.class);
LoadingActivity.this.startActivity(mainIntent);
LoadingActivity.this.finish();
}
}, 6000);
}
That what you want is to start the activity in the click of the screen but just after th delay??
Then you should set the listener in the handler :
setContentView(R.layout.mylayout);
final View mScreen = findViewById(R.id.whole_layout);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
overridePendingTransition(R.anim.animin, R.anim.animout);
final Intent mainIntent = new Intent(LoadingActivity.this, StartActivity.class);
LoadingActivity.this.startActivity(mainIntent);
LoadingActivity.this.finish();
}
});
}
}, 6000);
mylayout.xml:
<LinearLayout
android:id="#+id/whole_layout"
android:clickeable="true"
...>
<!-- your content -->
</LinearLayout>
You can use the layout's type that you wish
Create an empty view that matches the layout of the screen and then setup logic to show/hide the view when appropiate?

Showing webview in AndEngine GLES 2 while the game is running on Android

I am working on a game while the game is running I need to show a webview with some instructions from web is that possible?
These instructions will be shown again and again and are dynamic as they are coming from a server.
Can you please help me with that? I cannot use LayoutBaseGameActivity as I don't want to show them always.
I want to pause the game and show a webview which will be clickable. Please help.
You can add to your game activity to show WebView and call openWebViewURL or openWebViewHTML when you need
#Override
protected void onSetContentView() {
final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT);
mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine, this);
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
final FrameLayout.LayoutParams webViewLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, 100,
Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
webView = new WebView(this);
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(webView, webViewLayoutParams);
this.setContentView(frameLayout, frameLayoutLayoutParams);
webView.setVisibility(webView.INVISIBLE);
}
private void openWebViewURL(String url) {
webView.loadUrl(url);
this.runOnUiThread(new Runnable() {
#Override
public void run() {
webView.setVisibility(webView.VISIBLE);
}
});
}
private void openWebViewHTML(String html) {
webView.loadData(html,"text/html", "en_US");
this.runOnUiThread(new Runnable() {
#Override
public void run() {
webView.setVisibility(webView.VISIBLE);
}
});
}
I suggest you Start a new Activity to show the webpage.
the user can close the activity when they finish. And there is nothing change in the game. :D
Hopte it can help you :D

Programmatically create ImageView in Android

I followed this to create a simple animation.
The example creates manually an ImageView in the main layout, having a resource like this android:src="#anim/anim_android
The problem is, when I make a dynamic ImageView, and I set the resource like this myImageView.setImageResource(R.anim.anim_android);, the animation doesn't work, just views the first image of the sequence.
Any help?
Thanks.
you sould start the animation from you imageView :
myImageView.setImageResource(R.anim.anim_android);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myImageViewn.getDrawable();
myImageView.post(
new Runnable(){
#Override
public void run() {
myAnimationDrawable.start();
}
});
}
EDIT : i think you didn't add the ImageView to your Layout ( contentView of your Activity ) ; try this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rootLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rootLayout.setLayoutParams(params);
ImageView myAnimation = new ImageView(this);
RelativeLayout.LayoutParams paramsImage = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsImage.addRule(RelativeLayout.CENTER_IN_PARENT);
myAnimation.setLayoutParams(paramsImage);
myAnimation.setImageDrawable(getResources().getDrawable(R.anim.anim_android));
rootLayout.addView(myAnimation);
setContentView(rootLayout);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
myAnimation.post(new Runnable() {
#Override
public void run() {
myAnimationDrawable.start();
}
});
}

Android: Adding many children to a layout in a separate thread

I have an activity with a Button and a GridLayout with many children. If I add all these children in onCreate() my activity appears on a screen with a lag:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(this);
button.setText("test");
main.addView(button);
GridLayout testGrid = new GridLayout(this);
testGrid.setColumnCount(5);
for (int i = 0; i < 100; i++)
testGrid.addView(new Button(this));
main.addView(testGrid);
setContentView(main);
}
But I want at least my Buttton to appear immediately, so I try to add the children to the grid in a thread. After experiments I came to this solution:
final GridLayout testGrid = new GridLayout(this);
testGrid.setColumnCount(5);
main.addView(testGrid);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 100; i++)
MyActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
testGrid.addView(new Button(testGrid.getContext()));
}
});
}
}).start();
}
}, 1);
But I'm not sure it's a good idea, because it looks kinda complicated and may be it won't work well on some devices. Any better suggestions?
When you have to do something like this, it is a clear indication that you do something wrong. If you really need 100 buttons in a grid, maybe you should consider using GridView instead of GridLayout and loading buttons into view via a simple adapter.

Add text view dynamically inside a tab (Android)

I am trying to add a textview inside a tab dynamically. using this code
Oncreate()
{
OA.loaderShow(this); //Loader display
new Thread(new Runnable(){
public void run()
{
Looper.prepare();
fetchDocs();
OA.loaderHide(); //Loader Hide
Looper.loop();
}
}).start();
}
fetchDocs()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
TextView text = new TextView(this);
text.setText(mytext);
layout.addView(text);
}
I am getting this error "Only the original thread that created a view hierarchy can touch its view".
Please Help.
Put above inside the following block
runOnUiThread(new Runnable() {#Overridepublic void run() {//your code here}}
Try using a handler like this:
EDIT:
protected static final int SET_TEXTVIEW = 0;
Oncreate()
{
OA.loaderShow(this); //Loader display
new Thread(new Runnable(){
public void run()
{
Looper.prepare();
handler.sendMessage(handler.obtainMessage(SET_TEXTVIEW));
OA.loaderHide(); //Loader Hide
Looper.loop();
}
}).start();
}
public Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SET_TEXTVIEW :
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
TextView text = new TextView(this);
text.setText(mytext);
layout.addView(text);
}
}
};
Add this in the onCreate method rather than adding from else where.

Categories

Resources