Set the width of Spinner using java not referencing any xml - java

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

Related

LinearLayout - adding a TableLayout after a TextView

I'm having a problem with LinearLayout where I want to display a TableLayout after a TextView. I've tried modifying the LayoutParams however I can't seem to make it wrap (which I'm assuming I want it to do).
This is simplified version:
ScrollView sv = (ScrollView) findViewById(R.id.sv);
sv.removeAllViews();
LinearLayout ll = new LinearLayout(this);
sv.addView(ll);
//This is displayed
TextView tv = new TextView(this);
tv.setText("Hello World");
ll.addView(tv);
//This is not displayed - but if I remove the above view it will display...
TableLayout tl = new TableLayout(this);
ll.addView(tl, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
By specifiying Orientation as Vertical it worked for me:
ScrollView sv = (ScrollView) findViewById(R.id.sv);
sv.removeAllViews();
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Hello World");
ll.addView(tv);
TableLayout tl = new TableLayout(this);
ll.addView(tl);

Adding layout views from an xml file to the content view

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!

How to add EditText programmatically?

How to add EditText programmatically?
Here's what I wrote:
View RV = inflater.inflate(R.layout.fragment__dummy,container, false);
LinearLayout LL = new LinearLayout(getActivity());
EditText ET = new EditText(getActivity());
ET.setId(5);
ET.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
LL.addView(ET);
return RV;
Thank You!
LinearLayout layout=(LinearLayout)view.findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
EditText edttext= new EditText(this);
edttext.setId("edittext");
edttext.setLayoutParams(params);
layout.addView(edttext);
You are creating the LinearLayout LL dynamically too.
You need to add it to RV like
((ViewGroup)RV).addView(LL);
Also you should set the LayoutParams for LL too

AddView error, Just show the first element

Let's say I have a LinearLayout, and I want to add two View to it. The first one contain editText, and the other one contain listview. I have been try code in java follows:
EditText inputViaText;
ListView historyInput;
protected static LinearLayout askTextLayout = null;
askTextLayout = new LinearLayout(this);
askTextLayout.setVisibility(LinearLayout.VISIBLE);
askTextLayout.setOrientation(LinearLayout.HORIZONTAL);
inputViaText = new EditText(this);
LinearLayout.LayoutParams askTextParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
historyInput = new ListView(this);
LinearLayout.LayoutParams historyInputParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,70);
askTextLayout.addView(historyInput,historyInputParams);
askTextLayout.addView(inputViaText,askTextParams);
FrameLayout.LayoutParams frameAskTextParams = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
setContentView(R.layout.activity_main);
addContentView(askTextLayout, frameAskTextParams);
But, It just show the first one that i add. So when i code like follows:
askTextLayout.addView(historyInput,historyInputParams);
askTextLayout.addView(inputViaText,askTextParams);
It just show listView. When i code like follows:
askTextLayout.addView(inputViaText,askTextParams);
askTextLayout.addView(historyInput,historyInputParams);
It just show edittext. Anyone can help me?
Try this code in oncreate Method of your activity
context = this;
setContentView(R.layout.activity_main);
container = (LinearLayout) findViewById(R.id.Linear);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setWeightSum(100);
ListView v1 = new ListView(context);
v1.setBackgroundColor(Color.CYAN);
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(0,
50);
p1.weight = 90;
v1.setLayoutParams(p1);
EditText v2 = new EditText(context);
v2.setText("Hello");
v2.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(0,
50);
p2.weight = 10;
v2.setLayoutParams(p2);
linearLayout.addView(v1, p1);
linearLayout.addView(v2, p2);
View view = new View(MainActivity.this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
1);
view.setLayoutParams(lp);
view.setBackgroundColor(Color.BLACK);
container.addView(linearLayout);
container.addView(view);
Write this in activity_main:
<LinearLayout
android:id="#+id/Linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>

How to put one buttom right and another left

I try to create dynamically linerlayout and i created two buttoms.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
TextView titleView = new TextView(this);
titleView.setWidth(LayoutParams.WRAP_CONTENT);
titleView.setHeight(LayoutParams.WRAP_CONTENT);
titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
titleView.setText("Hallo Welt!");
layout.addView(titleView);
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
layout.addView(btnConnect);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
layout.addView(btnDisconnect);
I want to put Connect button to left corner and want to put disconnet button to the right corner. How can i do that?
Have you tried to set layout gravity for your buttons?
LayoutParams params;
Button btnConnect = new Button(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.Left;
btnConnect.setLayoutParams(params);
...
Button btnDisconnect = new Button(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.Right;
btnConnect.setLayoutParams(params);
...
Layout gravity defines where to place a view in its parent (see Gravity and layout_gravity on Android too)
Cheers
You can use RelativeLayout instead of linear layout.
and add rules to the layout params of the
RelativeLayout.Layoutparams params = RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

Categories