Help me please to move ads to bottom of the screen
Cocos2dxActivity.this.adView = new AdView(Cocos2dxActivity.this, AdSize.SMART_BANNER, mediationId());
#SuppressWarnings("deprecation")
ViewGroup.LayoutParams ad_layout_params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Cocos2dxActivity.this.adView.setLayoutParams(ad_layout_params);
AdRequest adRequest = new AdRequest();
Note : this is in regard to cocos2d-x 3.1.1, though earlier versions should be similar in regard to this.
Cocos2dxActivity uses a FrameLayout as its top most layout. In order to achieve what you want, this has be changed to a RelativeLayout - in Cocos2dxActivity class, there is an init() method which you should edit. There is also a class variable called mFrameLayout - just change its type to RelativeLayout and name accordingly, and your IDE should tell you where else to change.
As to the banner itself you should give it this layout parameters when adding it to the RelativeLayout :
RelativeLayout.LayoutParams relParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
relParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Related
I have a relative layout which I am creating programmatically:
RelativeLayout layout = new RelativeLayout( this );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Now I have two TextViews which I want to add in this relative layout. But the problem is both TextViews are being shown on the left of the RelativeLayout overlapping on each other.
textViewContainer.addView(textView1);
textViewContainer.addView(textView2);
Now I want to know how can I programmatically set the the
`android:layout_alignParentRight="true"`
or
`android:layout_toLeftOf="#id/textView"`
attribute of TextViews as we do in the xml?
You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using
RelativeLayout.LayoutParams#addRule(int verb) and
RelativeLayout.LayoutParams#addRule(int verb, int anchor)
You can get to it via code:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)textView.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
textView.setLayoutParams(params); //causes layout updat
Use the method addrule
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
You need to align LayoutParams to each view:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Then add appropriate rule to each view:
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
to the first one and
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
to the second one.
then apply it to the view:
view.setLayoutParam(params)
I wanted to try using a database on android so I made a small app that allows you to add contacts to the database and then it displays them.
I have been able to display contacts (from the database) if I create the layout on xml and then edit the text fiels in code. But I wanted to build the layouts in code so I can add as many contacts as I want.
The following code is the method I use to create the layout and the app crashes whenever I make it run this code. My guess is that there is a problem with the params. If only type LayoutParams.MatchContent it asks me to import and it gives me many options, that0s why it says LinearLayout.LayoutParams...;
I add the resulting layout to a LinearLayout.
Any help is much apreciated.
private LinearLayout createContactView (Contact contact) {
LinearLayout contactInfoWrapper = new LinearLayout(this);
contactInfoWrapper.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
contactInfoWrapper.setOrientation(LinearLayout.VERTICAL);
TextView nameView = new TextView(this);
nameView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
nameView.setText(contact.getName());
contactInfoWrapper.addView(nameView);
TextView numberView = new TextView(this);
numberView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
numberView.setText(contact.getPhoneNumber());
contactInfoWrapper.addView(numberView);
return contactInfoWrapper;
}
Instead of using setLayoutParams, instead use the LayoutParams in the addView(View v, LayoutParams params) method of your LinearLayout:
TextView nameView = new TextView(this);
nameView.setText(contact.getName());
contactInfoWrapper.addView(nameView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
can you change the line
numberView.setText(contact.getPhoneNumber());
to this
numberView.setText(String.valueOf(contact.getPhoneNumber()));
So I'm having trouble getting a TextView to appear programmatically with Java. Here's the code:
LinearLayout layout=new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
TextView tx = new TextView(this);
tx.setText("Hello World");
layout.addView(tx);
Are you ever setting the content view for the current activity? The LinearLayout you're dynamically creating doesn't appear to ever be displayed.
Do something like this after you create it (or create the linear layout in your XML first, then dynamically add the text box):
setContentView(layout)
As a bit of background on this - I created a layout for a user interface that sat ontop of a GLSurfaceview. As I progressed I wanted to add adverts from admob on top of the GLSurfaceview. After some reading it seemed like the only way to do this was to create a new Layout view then add each dynamically created view to this. Then set the new Layout view as the content view. This means that all the views in my UI are dynamically created in java code.
My question is:
Is there any way you can use a view from an xml layout that is not set as the content view and somehow add it to the content view just like you would when dynamically creating a view?
I did try this but got null pointer exceptions from which ever view I tried to findViewById.
Any sort of write up explaining this would be helpful
EDIT added code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("******************************").build();
adView.loadAd(adRequest);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
final TextView Score = new TextView(this);
Score.setText(" 0");
RelativeLayout.LayoutParams scoreParams = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Score.setLayoutParams(scoreParams);
Typeface tf = Typeface.createFromAsset(getAssets(),"Fonts/BOYCOTT_.ttf");
Score.setTextSize(getResources().getDimension(R.dimen.textsize));
Score.setTypeface(tf);
Score.setTextColor(0xFFFFFFFF);
// Check if the system supports OpenGL ES 2.0.
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
if (supportsEs2)
{
// Request an OpenGL ES 2.0 compatible context.
mGLSurfaceView = new RBGLSurfaceView(this); new GLSurfaceView(this);
mGLSurfaceView.setEGLContextClientVersion(2);
final DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
mGoogleApiClient = getApiClient();
mGLSurfaceView.setRenderer(new RBRenderer(this, Score, mGoogleApiClient),displayMetrics);
layout.addView(mGLSurfaceView);
layout.addView(Score, scoreParams);
layout.addView(adView, adParams);
//Set main renderer
setContentView(layout);
}
else
{
// This is where you could create an OpenGL ES 1.x compatible
// renderer if you wanted to support both ES 1 and ES 2.
return;
}
}
Ok so the above code is creating an adview, textview and surface view and adding them to the layout which is then getting set as the contentview - works great.
But is it possible to add the text view from an xml layout even if this xml layout is not set as the contentview?
Doing this doesnt work
final TextView test= (TextView) findViewById(R.id.test);
// set properties here etc
layout.adView(test);
because 'test' is null because the layout in which the view 'test' belongs to is not the set as the content view.
is it possible to add the text view from an xml layout even if this xml layout is not set as the contentview?
Yes, it's totally possible! Try this:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View textView = inflater.inflate(R.layout.text_view, null);
layout.addView(textView);
Hope this helps!
There very well may be a duplicate question, but I have yet to find it. I am doing thing all programmatically, not using the xml. Basically what I am trying to do is to have an EditText appear below an image. I am using RelativeLayout with an ImageView and and EditText.
These are the parameters that I am setting up for the ImageView and EditText:
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
editTextParams.width=500;
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageParams.addRule(RelativeLayout.ABOVE, editText.getId());
Which I have verified correctly places the EditText in the bottom right corner and the image above. What I run into is if the picture it "too tall" then it covers the EditText. I also tried using
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
and it stretches it over the EditText as well.
The full code that I am using is this
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView imageView = new ImageView(this);
EditText editText = new EditText(this);
RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
editTextParams.width=500;
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageParams.addRule(RelativeLayout.ABOVE, editText.getId());
imageView.setLayoutParams(imageParams);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
editText.setLayoutParams(editTextParams);
Bitmap image = getImage();
imageView.setImageBitmap(image);
layout.addView(editText);
layout.addView(imageView);
Thanks. Any suggestions would be greatly appreciated.
Create a LinearLayout and then add the two components in it (ImageVIew and EditText)
Here is what you should do;
Set the orientation to vertical for the horizontal layout and width to whatever you need
Set the with of both components to 0 and weights to 1 each
After that, you should have the two items one above the other;
I hope this helps
One thing you can do is fix the imageview width and height. This way you can control the maximum size without the imaging going over the edit text.
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(256, 256);
Hopefully this helps.
Your editText has a vertical MATCH_PARENT. Shouldn't it be WRAP_CONTENT?
(I am not sure if this helps, but it might help, as the MATCH_PARENT contradicts your planned layout, and your screenshot indicates that the editText is vertically centered.)