Reputation: 121
I am beginner in android. And i could not understand differences between layouts. i want to make a button and next to button i want to set an image. So which layout should i have yo use and how can i set the positions.(Programitacilly)
Upvotes: 0
Views: 601
Reputation: 409
Cemal wanted to see this done programatically. The above references are good for showing the XML versions. Here is a quick example of the button and image in a linear layout done entirely programtically.
package com.example.android.ProgramLinearActivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ProgramLinearActivity extends Activity {
private static final int HORIZONTAL = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(HORIZONTAL); //HORIZONTAL is default but here for clarity
ImageView imageView = new ImageView(this);
imageView.setImageResource( R.drawable.icon);
Button button = new Button (this);
button.setText("Test");
linearLayout.addView(button);
linearLayout.addView(imageView);
setContentView(linearLayout);
}
}
Hit ctrl+space a lot in the eclipse editor to see tutorials on the other attributes for the button and imageview wigets.
Upvotes: 0
Reputation: 7752
You will want to use the LinearLayout - inside the linear layout you can place your button and image.
There's a good tutorial on how to use LinearLayout over here: Android Developers-LinearLayout
If you are new to Android, I would recommend checking out the other "Hello *" tutorials over there.
Upvotes: 0