Coding layouts without using XML layout

on Sunday, January 6, 2013 | 6:15 PM

Coding layouts without using XML layout. Simply on this short tutorial I will provide sample code for creating a simple layout that displays text and Imaga without using layout.xml on Android. [java coding of only]. Immediately, this short code :
public class Layout_01 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//make object textview
TextView label = new TextView(this);
//set text, gravity, and size
label.setText("Nama saya dede pradana");
label.setGravity(Gravity.CENTER);
label.setTextSize(20);
//make object imageview
ImageView imageView = new ImageView(this);
//specify the image, from the folder res / drawable [my photos]
imageView.setImageResource(R.drawable.dede);
//specify the parameters of imageView, width : wrap_content, height : wrap_content
imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
//fit_xy only image size to get the Fix size
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setAdjustViewBounds(true);
//and create a layout that will be the root layout / viewgroups containing the view2
LinearLayout layout = new LinearLayout(this);
//set layout root ini dengan width & height match_parent
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//set orientasi vertical
layout.setOrientation(LinearLayout.VERTICAL);
//the last, add a second object view that we created earlier
layout.addView(label);
layout.addView(imageView);
//set content activity.
setContentView(layout);
}
}
Share this post :
Coding layouts without using XML layout Published By Technoledge Terima Kasih Telah membaca Coding layouts without using XML layout
Link Published on: 2013-01-06T18:15:00+07:00
Coding layouts without using XML layout. Simply on this short tutorial I will provide sample code for creating a simple layout that displays... Rating 5 ★★★★★ Reviews 1110
Index »

Android App

 
Copyright © 2013. Technoledge - All Rights Reserved
Proudly powered by Blogger