Adding layout views from an xml file to the content view - java

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!

Related

Showing two RelativeLayouts in a LinearLayout not working

I'm trying to display two RelativeLayouts in a LinearLayout, however the best result I'm getting is getting one of the two displayed.
final LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.VERTICAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutAll = (RelativeLayout) inflate.inflate(
R.layout.activity_main, null);
final RelativeLayout menuLayout = (RelativeLayout)inflate.inflate(
R.layout.bottom_menu, null);
menuLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
layoutMain.addView(layoutAll);
layoutMain.addView(menuLayout);
layoutAll is showing, and if I change the order of the addView lines, then menuLayout is showing. So far I've tried different layout parameters (MATCH_PARENT for layoutAll or menuLayout and even both). Any suggestion ? Thanks.

Text View not displaying (Android Studio)

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)

Move AdView to bottom, Cocos2dx Activity

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);

admob banner position / notification bar

I'm working on putting ads in my android game project with libgdx. I saw this code on the web and it worked for me. The problem is it shows the notification bar on the top and the position of the ad is on bottom left part of the screen which obscures the view of the character. The game is on landscape position and I want to put the ad on bottom center part. How can I take off the notification bar and put the ad on a correct position. I'm just new in java and I don't understand this code. Thank you.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
//setContentView(R.layout.main);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams gameViewParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//gameViewParams.bottomMargin = 150;
requestWindowFeature( Window.FEATURE_NO_TITLE );
View gameView = initializeForView(new ZBGame(), cfg);
layout.addView(gameView, gameViewParams);
AdView adView = new AdView(this);
adView.setAdUnitId("YOUR ADMOB AD ID");
//adView.setAdUnitId("app-id");
adView.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, adParams);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
setContentView(layout);
I haven't done this yet, but I would assume you would add another rule to adParams to center the view.
For example:
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

Set the width of Spinner using java not referencing any xml

I am dynamically building a View here is what I have so far:
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
Spinner s = new Spinner(this);
s.setId(1);
List<SpinnerObject> list = this.datasource.getFacilitiesList();
ArrayAdapter<SpinnerObject> adapter = new ArrayAdapter<SpinnerObject>(this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
ll.addView(s);
setContentView(sv);
How do I set the width of my Spinner. I would like to know how to set it to wrap_content and a set value like 200dp
You can change the width of anything in code by adjusting its LayoutParams.
For example:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = 200dp;
getWindow().setAttributes(lp);

Categories