How to implement Ads In AndEngine - android

I am trying to display an advertisement using startApp in AndEngine.
like this,
#Override
protected void onSetContentView()
{
StartAppAd.init(this, "apiID", "appID");
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
BannerStandard startAppBanner = new BannerStandard(this);
relativeLayout.addView(startAppBanner, layoutParams);
this.addContentView(relativeLayout, layoutParams1);
}
but it isn't work, please help me. I almost done other setup, like adding jar and manifest file.

Related

How do I switch from alignParentLeft to alignParentRIght?

I want my chat bubble to move to the right side when a different user has sent a message but for some reason it stays on the same side. This is what I've tried:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.bubble.getLayoutParams();
if (message.getPersonId() == MainActivity.getUser().getUserId()) {
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
} else {
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
}
holder.bubble.setLayoutParams(params);
Is there something I'm doing wrong? I also tried doing params.removeRule(int) but it didn't work either plus I prefer to stay away from that because I want to ensure as much usability as possible in terms of OS.
You can simply add parameters like this way-
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams
(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
------------------------------------
----------------------------------
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText(d.getName());
tv.setTextSize(25);
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// Setting the parameters on the TextView
tv.setLayoutParams(lp);
b.setLayoutParams(lp);
// Adding the TextView to the RelativeLayout as a child
I think, if you follow this way, there will not be any problem.
It seems you are not using adRule() properly. Please change and try again:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.bubble.getLayoutParams();
if (message.getPersonId() == MainActivity.getUser().getUserId()) {
params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
else {
params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
holder.bubble.setLayoutParams(params);

Android: dynamically add buttons to layout

I am trying to dynamically add 2 buttons to my relative layout. I want it to look like this:
but it currently looks like this:
With #Abdallah Alaraby's help:
myButton1 = new Button(this);
myButton1.setBackgroundResource(R.drawable.button);
myButton1.setText("bttn1");
myButton2 = new Button(this);
myButton2.setBackgroundResource(R.drawable.button);
myButton2.setText("bttn2");
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl_dynamic_bttn);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.RIGHT_OF, myButton2.getId());
rl.addView(myButton1, lp1);
rl.addView(myButton2, lp);
...
...
}
I tried all sort of different allignment options but nothing seems to be working. Anyone know how I can make this look like the first picture?
Is it possible that the issue is with my_button2.getId()? Maybe it's not recognized?
lp.addRule(RelativeLayout.RIGHT_OF, myButton2.getId());
rl.addView(myButton1, lp);
rl.addView(myButton2, lp);
You are using the same rule for both buttons, try the following:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myButton1.setId(1)
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.RIGHT_OF, myButton2.getId());
rl.addView(myButton1, lp1);
rl.addView(myButton2, lp);
If myButton2 does not have an assigned Id, the default Id would be -1 and it won't work. you have to use myButton2.setId(int) before using myButton2.getId()

Programmatically added Views not behaving

I've created many custom views and I am trying to add them to my fragment. They get added but I can't seem to get them to go where I want. There should be 2 columns and 3 rows but it ends up as 1 column with all of the custom views stacked on top of one another. Here is my code to add the views and set the layout params to the fragment layout:
RelativeLayout fm = (RelativeLayout) view.findViewById(R.id.fragmentLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
CustomImages cs = new CustomImages(getActivity());
cs.setId(R.id.one);
cs.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.RIGHT_OF, cs.getId());
CustomImages2 cs2 = new CustomImages2(getActivity());
cs2.setId(R.id.two);
cs2.setLayoutParams(params);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params2.addRule(RelativeLayout.BELOW, cs2.getId());
CustomImages3 cs3 = new CustomImages3(getActivity());
cs3.setId(R.id.three);
cs3.setLayoutParams(params);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params3.addRule(RelativeLayout.RIGHT_OF, cs3.getId());
CustomImages4 cs4 = new CustomImages4(getActivity());
cs4.setId(R.id.four);
cs4.setLayoutParams(params);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params4.addRule(RelativeLayout.BELOW, cs4.getId());
CustomImages5 cs5 = new CustomImages5(getActivity());
cs5.setId(R.id.five);
cs5.setLayoutParams(params);
cs3.setLayoutParams(params);
cs4.setLayoutParams(params);
cs5.setLayoutParams(params);
I believe params there should be replaced with params2, params3 and params4 respectively.
UPDATE:
Also, you should specify LAYOUT_BELOW for all views which are not on top, and do it correctly:
params2.addRule(RelativeLayout.BELOW, cs.getId()); // not cs2
params3.addRule(RelativeLayout.BELOW, cs2.getId()); // add this
params4.addRule(RelativeLayout.BELOW, cs3.getId()); // not cs4

android:layout_alignParentBottom by code

How to set android:layout_alignParentBottom="true" for a videoview, by code and not from xml?
I assume you are trying to add your VideoView to a RelativeLayout ?
RelativeLayout relativeLayout = findViewById(R.id.your_relative_layout);
mVideo = new VideoView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // or wrap_content
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(mVideo , layoutParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
YourVideoView.setLayoutParams(params);
get layout params from your view and add rule
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Some References are Here1 and Here2
this is what you have to do:
Create a RelativeLayout.LayoutParams object.
Use addRule(int) or addRule(int, int) to set the rules. The first method is used to add rules that don’t require values.
Set the parameters to the view (in this case, to each button).
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
yourView.setLayoutParams(params);

android TextView setting margin and background using code

In an Android activity....how do I do the following using just code instead of XML?
<TextView android:background="#0000ff" android:layout_margin="2dip"/>
I also need text align right...
stuck on it for a while =( if someone could help
thanks
UPDATE
I tried the code below
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
//250);
lp.setMargins(5,5,5,5);
and i did
row.addView(t1, lp);
row.addView(t2);
row.addView(t3);
row.addView(t4);
I also tried LinearLayout
but t1 for some reason doesn't show at all anymore....
here is how to code a textView:
mTextView = new TextView(this);
mTextView.setGravity(Gravity.CENTER_VERTICAL);
mTextView.setText(R.string.instructions);
mTextView.setTextColor(0xFF000000);
mTextView.setPadding(20, 8, 8, 20);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);
mScroll = new ScrollView(this);
mScroll.setScrollbarFadingEnabled(false);
mTextPane = new RelativeLayout(this);
mTextPane.setVisibility(View.GONE);
//mScroll.setVisibility(View.GONE);
mScroll.addView(mTextView);
mTextPane.addView(mScroll);
Resources res = getResources();
//Drawable drawable = res.getDrawable(R.drawable.text_pane_feather2);
Drawable drawable = res.getDrawable(R.drawable.text);
mTextPane.setBackgroundDrawable(drawable);
//RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 420, 420 );
RelativeLayout.LayoutParams lp =
new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,
//RelativeLayout.LayoutParams.WRAP_CONTENT);
250);
lp.setMargins(0,0,0,30);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL );
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(mTextPane, lp);
Try the TextViews setBackgroundColour() method.
For setting the layout params, use the setMargins(int,int,int,int) from the MarginLayoutParams class.
See the documentation for more details at:
View.setBackgroundColour and
Layout Params

Categories

Resources