//assuming your layout is in a LinearLayout as its root LinearLayout layout = (LinearLayout)findViewById(R.id.rootlayout); ImageView image = new ImageView(this); image.setImageResource(R.drawable.image); int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2; int orgWidth = image.getDrawable().getIntrinsicWidth(); int orgHeight = image.getDrawable().getIntrinsicHeight(); //double check my math, this should be right, though int newWidth = Math.floor((orgWidth * newHeight) / orgHeight); //Use RelativeLayout.LayoutParams if your parent is a RelativeLayout LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( newWidth, newHeight); image.setLayoutParams(params); image.setScaleType(ImageView.ScaleType.CENTER_CROP); layout.addView(image);
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = new TextView(this); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100); tv.setLayoutParams(layoutParams); tv.setText("testing 1 2 3"); tv.setTextColor(Color.BLACK); tv.setBackgroundColor(Color.TRANSPARENT); Bitmap testB; testB = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(testB); tv.layout(0, 0, 80, 100); tv.draw(c); ImageView iv = (ImageView)findViewById(R.id.menuIcon); iv.setLayoutParams(layoutParams); iv.setBackgroundColor(Color.GRAY); iv.setImageBitmap(testB); iv.setMaxHeight(80); iv.setMaxWidth(80); }
parms_left_layout.weight = 1F; LinearLayout left_layout = new LinearLayout(context); left_layout.setOrientation(LinearLayout.HORIZONTAL); left_layout.setGravity(Gravity.CENTER); left_layout.setLayoutParams(parms_left_layout); LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams( 20, 20); parms_legen_layout.setMargins(0, 0, 20, 0); LinearLayout legend_layout = new LinearLayout(context); legend_layout.setLayoutParams(parms_legen_layout); txt_unit.setText(legend.getLabel(i)); left_layout.addView(txt_unit); middle_layout.setLayoutParams(parms_middle_layout); TextView txt_leads = new TextView(this); txt_leads.setText("450"); middle_layout.addView(txt_leads);
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100); tv.setLayoutParams(layoutParams); tv.setText("testing 1 2 3"); tv.setTextColor(Color.BLACK); tv.setBackgroundColor(Color.TRANSPARENT); Bitmap testB; testB = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(testB); tv.layout(0, 0, 80, 100); tv.draw(c); ImageView iv = new ImageView(this); iv.setLayoutParams(layoutParams); iv.setBackgroundColor(Color.GRAY); iv.setImageBitmap(testB); setContentView(iv); }
LinearLayout llTemp = new LinearLayout(this); llTemp.setOrientation(LinearLayout.HORIZONTAL); llTemp.setBackgroundResource(R.drawable.chip); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); llTemp.setLayoutParams(params); llTemp.setGravity(Gravity.CENTER_VERTICAL); TextView tv = new TextView(this); tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); tv.setPadding(0, 0, 5, 0); tv.setText("Some text"); CircularImageView iv = new CircularImageView(this); LinearLayout.LayoutParams ivParams = new LinearLayout.LayoutParams(50, 50); ivParams.setMargins(0, 0, 5, 0); iv.setLayoutParams(ivParams); iv.setBorderWidth(0); Picasso.with(this).load(IMAGE_LINK + IMAGE_FILE).into(iv); ImageView ivCloseBtn = new ImageView(this); ivCloseBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); ivCloseBtn.setPadding(2, 2, 2, 2); ivCloseBtn.setBackgroundResource(R.drawable.icon_close);
private void addNewView() { TextView myTxt = new TextView(this); LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(105, 105); parms.setMargins(5, 5, 5, 5); myTxt.setLayoutParams(parms); myTxt.setText(""); view.addView(myTxt); }
TextView tv1 = new TextView(this); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100); tv1.setLayoutParams(layoutParams); tv1.setText("testing 1 2 3"); tv1.setTextColor(Color.BLACK); tv1.setBackgroundColor(Color.TRANSPARENT);
public void setClassicLabel(JSONArray listLabel, RelativeLayout rl) throws JSONException { if (listLabel.length() > 0) { DisplayMetrics metrics = new DisplayMetrics(); LinearLayout ll = new LinearLayout(getContext()); ll.setOrientation(LinearLayout.HORIZONTAL); for (int i = 0; i < listLabel.length(); i++) { TextView tv = new TextView(context); tv.setText(listLabel.getJSONObject(i).getString("text")); tv.setTextColor(Color.parseColor(listLabel.getJSONObject(i).getString("colortext"))); tv.setBackgroundColor(Color.parseColor(listLabel.getJSONObject(i).getString("color"))); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("size_x")), (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("size_y"))); params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("position_x")); params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("position_y")); ll.addView(tv); //rl.addView(ll); } this.addView(ll); } } }
TextView textV = new TextView(context); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(128, 128); textV.setLayoutParams(layoutParams); textV.setTextColor(Color.WHITE); textV.setBackgroundColor(Color.TRANSPARENT); textV.setGravity(Gravity.CENTER); textV.setText(text); textV.setDrawingCacheEnabled(true); Bitmap b = Bitmap.createBitmap( textV.getLayoutParams().width, textV.getLayoutParams().height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); textV.layout(0, 0, textV.getLayoutParams().width, textV.getLayoutParams().height); textV.draw(c); textV.setDrawingCacheEnabled(false);
childLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 200)); childLayout.setBackgroundColor(Color.parseColor("#0F0")); childLayout.setPadding(10, 10, 10, 10); ParentLayout.addView(childLayout); textView1.setLayoutParams(tvParams); textView1.setText("Offers"); textView1.setBackgroundColor(Color.parseColor("#FF0000")); textView1.setPadding(0, 5, 0, 10); // in pixels (left, top, right, bottom) LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(40,40); //width:40dp and height:40dp btnAB.setLayoutParams(btnParams); final int sdk = android.os.Build.VERSION.SDK_INT;
//--- Row Layout --- LinearLayout rowLayout = null; LinearLayout.LayoutParams rowLayoutparams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); /** Create the TextViews and Spinners */ for (int j=0; j<gameArray.length; j++) { //Create new row rowLayout = new LinearLayout(getActivity().getApplicationContext()); rowLayout.setLayoutParams(rowLayoutparams); rowLayout.setGravity(Gravity.CENTER_HORIZONTAL); //--- TEXT VIEW --- TextView myTextView = new TextView(getActivity().getApplicationContext()); myTextView.setId(j); myTextView.setText("CONTENT"); myTextView.setGravity(Gravity.CENTER); //--- SPINNER --- spinnersArray[j] = new Spinner(getActivity().getApplicationContext()); spinnersArray[j].setId(j+100); //set a different ID //Populate the spinner here ... //--- LAYOUT PARAMS --- LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(100, 100); //first and last item //edit the item params here ... //Add the params to the Views myTextView.setLayoutParams(itemParams); spinnersArray[j].setLayoutParams(itemParams); //--- Add the view to the Layout --- rowLayout.addView(myTextView); rowLayout.addView(spinnersArray[j]); //--- Add the row to the layout only if (NOT even) OR the chars are finished --- ((LinearLayout)getView().findViewById(R.id.parent)).addView(rowLayout); }
LinearLayout.LayoutParams parms_left_layout = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); parms_left_layout.weight = 1F; LinearLayout left_layout = new LinearLayout(context); left_layout.setOrientation(LinearLayout.HORIZONTAL); left_layout.setGravity(Gravity.CENTER); left_layout.setLayoutParams(parms_left_layout); LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams( 20, 20); parms_legen_layout.setMargins(0, 0, 20, 0); LinearLayout legend_layout = new LinearLayout(context); legend_layout.setLayoutParams(parms_legen_layout); legend_layout.setOrientation(LinearLayout.HORIZONTAL); legend_layout.setBackgroundColor(colorcodes[i]); left_layout.addView(legend_layout); TextView txt_unit = new TextView(context); txt_unit.setText(legend.getLabel(i));
LinearLayout LLT = new LinearLayout(context); LLT.setOrientation(LinearLayout.VERTICAL); LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); //get booktheme by bookID theme = db.getthemeByID(id); String themePath = theme.getFilepath(); int resid = getResources().getIdentifier(themePath, "drawable", getPackageName()); //imageView ImageView imageTheme = new ImageView(context); LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(500, 700); layoutParams.gravity=Gravity.CENTER_HORIZONTAL; imageTheme.setLayoutParams(layoutParams); imageTheme.setScaleType(ImageView.ScaleType.CENTER_INSIDE); imageTheme.setPadding(0, 20, 0, 10); imageTheme.setAdjustViewBounds(true); imageTheme.setImageResource(resid); LLT.addView(imageTheme); // add view VF.addView(LLT);
layout.setLayoutParams(layoutParams); layout.setGravity(Gravity.CENTER); layout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams((100),( 100)); imageViewParams.gravity = Gravity.CENTER_HORIZONTAL; iv.setLayoutParams(imageViewParams); iv.setScaleType(ImageView.ScaleType.FIT_XY); iv.setImageResource(R.mipmap.ic_launcher); layout.addView(iv, imageViewParams);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); LinearLayout layout = new LinearLayout(getApplicationContext()); layout.setOrientation(LinearLayout.VERTICAL); layout.setGravity(Gravity.CENTER); LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams((100), (100)); ImageView iv = new ImageView(getApplicationContext()); iv.setScaleType(ImageView.ScaleType.FIT_XY); iv.setImageResource(drawable_spinner); layout.addView(iv, imageViewParams); addContentView(layout, layoutParams);
LinearLayout MainLinearObj = (LinearLayout) findViewById(R.id.abc); for(int j=0;j<10;j++) { LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100); llp.setMargins(10, 10, 10, 10); llp.weight = 1; LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); LinearLayout newLinearLayout = new LinearLayout(this); newLinearLayout.setOrientation(LinearLayout.HORIZONTAL); newLinearLayout.setLayoutParams(llp1); for(int i = 0; i < 3; i++) { LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(LinearLayout.VERTICAL); ll.setBackgroundColor(Color.RED); ll.setLayoutParams(new LinearLayout.LayoutParams(100,100)); ll.setId(i); newLinearLayout.addView(ll,llp); } MainLinearObj.addView(newLinearLayout); }
LinearLayout view = (LinearLayout) findViewById(R.id.layout); ImageView myImage = new ImageView(this); LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(105, 105); parms.setMargins(5, 5, 5, 5); myImage.setLayoutParams(parms); myImage.setBackgroundColor(Color.TRANSPARENT); myImage.setImageBitmap(bitmap); view.addView(myImage);
LinearLayout layoutView = new LinearLayout(this.ctx); layoutView.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(150, 150); layoutView.addView(imgView, params1); LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams( 150 ,LayoutParams.WRAP_CONTENT); layoutView.addView(tv, params2); ImageView i1= new ImageView(this.ctx); i1.setImageResource(R.drawable.ic_launcher); layoutView.addView(i1); return layoutView;
public void addTableImage(String imageName, LinearLayout L){ ImageView newImage = new ImageView(c); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150); newImage.setLayoutParams(layoutParams); int id = c.getResources().getIdentifier(imageName, "drawable", c.getPackageName()); RelativeLayout l1 = new RelativeLayout(c); LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(400, 160); lp.gravity=Gravity.CENTER; l1.setLayoutParams(lp); l1.setBackgroundColor(Color.WHITE); l1.setGravity(Gravity.CENTER); newImage.setImageResource(id); l1.addView(newImage); L.addView(l1); }
protected TextView createDefaultTabView(Context context) { int padding = (int) (14 * getResources().getDisplayMetrics().density); TextView textView = new TextView(context); textView.setGravity(Gravity.CENTER); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP); textView.setTypeface(Typeface.DEFAULT); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); params.setMargins(padding, padding, padding, 0); textView.setLayoutParams(params); TypedValue outValue = new TypedValue(); getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true); textView.setBackgroundResource(outValue.resourceId); textView.setPadding(0, 0, 0, padding); return textView; }