protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView img=new ImageView(this);
img.setImageResource(R.drawable.img);
img.setScaleType(ImageView.ScaleType(CENTER_CROP));
setContentView(img) }
I want to set the size of image view in Java and I could not find the syntax working.
Try code(edit as per your view names):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout picLL = new LinearLayout(this);
picLL.layout(0, 0, 100, 100);
picLL.setLayoutParams(new LayoutParams(100, 100));
picLL.setOrientation(LinearLayout.HORIZONTAL);
ImageView myImage = new ImageView(this);
myImage.setImageResource(R.drawable.ic_launcher);
picLL.addView(myImage);
setContentView(picLL);
}
And set height or width:
myImageView..getLayoutParams().height = your_size_value;
myImageView..getLayoutParams().width = your_size_value;
<ImageView
android:id="#+id/imageView1"
android:layout_margin="20dp"
android:src="#drawable/stop"
android:layout_width="50dp"
android:layout_height="50dp"/>
change width and height attribute with your values.
//Linear layout setting here
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.bull);
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
linearLayout.addView(imageView);
setContentView(linearLayout);
Related
I'm trying to create programmatically parent LinearLayout
and 3 TextViews.
One TextView must be aligned in top left, the second one in the center of parent
and the third one to the right and bottom of my screen. Everything must be done in code.
I'm almost done, but there is still some problem.
All my 3 views are on the top
My code:
public class ActivityFour extends AppCompatActivity {
private LinearLayout mLinearLayout;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private static final int TV_ID1 = 101;
private static final int TV_ID2 = 102;
private static final int TV_ID3 = 103;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
ViewGroup.LayoutParams llParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setContentView(mLinearLayout, llParams);
LinearLayout.LayoutParams linlayout_params1 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linlayout_params2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linlayout_params3 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linlayout_params1.setMargins(16,16,16,16);
tv1 = new TextView(this);
tv1.setId(TV_ID1);
linlayout_params1.gravity = Gravity.START;
linlayout_params1.gravity = Gravity.TOP;
mLinearLayout.addView(tv1, linlayout_params1);
tv1.setText("TextView number ONE");
linlayout_params2.setMargins(16,16,16,16);
tv2 = new TextView(this);
tv2.setId(TV_ID2);
linlayout_params2.gravity = Gravity.CENTER;
mLinearLayout.addView(tv2, linlayout_params2);
tv2.setText("TextView number TWO");
linlayout_params3.setMargins(16,16,16,16);
tv3 = new TextView(this);
tv3.setId(TV_ID3);
linlayout_params3.gravity = Gravity.END;
mLinearLayout.addView(tv3, linlayout_params3);
tv3.setText("TextView number THREE");
}
}
After adding the weight property to all view.params it looks like this
In order to make the LinearLayout fill all the available space, you need to set the android:layout_weight attribute on the middle child View. You can do it programmatically by using a Constructor which takes the weight as third parameter:
LinearLayout.LayoutParams linlayout_params2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
First of all no need to create liner layout for all textview you can do like this:
in xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/test"
tools:context=".activity.TestActivity"/>
and in activity file :
LinearLayout layout = findViewById(R.id.test);
TextView product_1 = new TextView(this);
product_1.setText("Product 1");
product_1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
layout.addView(product_1);
TextView product_2 = new TextView(this);
product_2.setText("Product 2");
product_2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
product_2.setGravity(Gravity.CENTER);
layout.addView(product_2);
TextView product_3 = new TextView(this);
product_3.setText("Product 3");
product_3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
product_3.setGravity(Gravity.END|Gravity.BOTTOM);
layout.addView(product_3);
I don't know what is the problem. I am creating the simple phone-book, so i have name of abonent, image and call button under the name. And when i try to set width with the layoutParams parameter for button, it doesn't change anything, btn.setWidth(); doesn't work either.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout linear = (LinearLayout)findViewById(R.id.linear);
LinearLayout.LayoutParams sublparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams childparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(200, 200);
linear.setOrientation(LinearLayout.VERTICAL);
for(int i = 0; i < 10 ; i++){
LinearLayout sublinear = new LinearLayout(this);
Button btn = new Button(this);
ImageView img = new ImageView(this);
TextView txt = new TextView(this);
img.setImageResource(R.drawable.img);
txt.setText("Abonent " + (i+1));
btn.setBackgroundColor(getColor(R.color.mygreen));
btn.setText("+3800000");
txt.setLayoutParams(childparams);
btn.setLayoutParams(imageparams);
img.setLayoutParams(imageparams);
linear.addView(txt);
sublinear.addView(img);
sublinear.addView(btn);
linear.addView(sublinear, sublparams);
}
}
}
result of programm, green vertical lines are buttons, that i cannot to make wider
your button is using LayoutParams to set width and heigth so you need to change this properties here (LinearLayout.LayoutParams(widht, height)):
LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(500, 300);
and the size of your button must change, because you are setting this values.
btn.setLayoutParams(imageparams);
I have created programmatically a RelativeLayout which contains a button. I have also created a ScrollView which contains a LinearLayout in which are more than 10 TextViews. I want to have the RelativeLayout to be aligned top and fixed. When someone tries to scroll down, i want all the TextViews to go behind the fixed RelativeLayout. I want that button to be always visible. With this code, the RelativeLayout and the button are not displayed. Where am i wrong?
RelativeLayout (fixed)
- Button
LinearLayout
- ScrollView
- TextView
- + other 10 TextViews
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
this.setContentView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
this.setContentView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
Thanks!
In your code you replace your RelativeLayout with ScrollView. Just set firstly some LinearLayout as contentView and then put there your RelativeLayout via addView(relativeLayout) and the put there you scrollView also via addView(scrollView)
EDIT:
your new code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout mainLinearLayout = new LinearLayout(this);
LinearLayout.LayoutParams mainLinearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mainLinearLayout.setLayoutParams(mainLinearLayoutParams);
mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
this.setContentView(mainLinearLayout);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
mainLinearLayout.addView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
mainLinearLayout.addView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
EDIT 2: renamed first linearLayout to mainLinearLayout according to the comment
I have the following code that upon image capture, places a RelativeLayout container, which contains ImageView of the image and Button that is supposed to be the X (Remove) button.
private void addImage(Bitmap photo) {
RelativeLayout rLayout = new RelativeLayout(getApplicationContext()); // Any difference between 'this' and 'getApplicationContext()'?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParas(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rLayout.setLayoutParams(params);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(photo);
RelativeLayout.LayoutParams bParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
bParams.addRule(RelativeLayout.ALIGN_RIGHT, imageView.getId());
bParams.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
bParams.setMargins(0, -10, -10, 0);
Button closeButton = new Button(getApplicationContext());
closeButton.setText("X"); // This will be an icon later
closeButton.setBackgroundColor(Color.RED); // For visualization purposes
closeButton.setLayoutParams(bParams);
rLayout.addView(imageView);
rLayout.addView(closeButton);
parentLinearLayout = (LinearLayout) findViewById(R.id.horizontal_scrolling_linear_layout);
parentLinearLayout.addView(rLayout);
}
The ideal scenario is having a close/delete button on top-right corner of the image, that upon click will remove the parent RelativeLayout from it's parent. Some styling and placements in the code might not make sense, I am JUST starting Android Development so any help will be appreciated. I am also including small questions in comments that would help me clear things up in Android Dev.
Question is: How can I capture button (X) click and remove the parent relative layout from the code?
Any help is appreciated, thank you!
Try this:
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
((ViewGroup) rLayout.getParent()).removeView(rLayout);
}
});
I'd start by moving the following line into your Activity's onCreate():
parentLinearLayout = (LinearLayout) findViewById(R.id.horizontal_scrolling_linear_layout);
This helps to reduce unnecessary reassignment as well as clutter. Once that's done, the following should suit your needs.
Button closeButton = new Button(getApplicationContext());
closeButton.setText("X"); // This will be an icon later
closeButton.setBackgroundColor(Color.RED); // For visualization purposes
closeButton.setLayoutParams(bParams);
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (parentLinearLayout != null) {
parentLinearLayout.removeView(rLayout);
}
}
});
Just use
closeButton.setOnClickListener(new OnClickListener(){
//override method
public void onClick(View v) {
//do something
}
});
In addition,you can't remove the parent RelativeLayout ,because any view must be included by viewGroup.
And the difference between "this" and "getApplicationContext()" as you mentioned at the beginning is:
this == yourActivity.this ,it is activity's instance. However,getApplicationContext() will get Application's instance.
// Try this way,hope this will help you to solve your problem...
private void addImage(Bitmap photo) {
RelativeLayout rLayout = new RelativeLayout(getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rLayout.setLayoutParams(params);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(photo);
RelativeLayout.LayoutParams bParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
bParams.addRule(RelativeLayout.ALIGN_RIGHT, imageView.getId());
bParams.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
bParams.setMargins(0, -10, -10, 0);
Button closeButton = new Button(getApplicationContext());
closeButton.setText("X"); // This will be an icon later
closeButton.setBackgroundColor(Color.RED); // For visualization purposes
closeButton.setLayoutParams(bParams);
closeButton.setTag(0,parentLinearLayout);
closeButton.setTag(1,rLayout);
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
LinearLayout parentLinearLayout = (LinearLayout)view.getTag(0);
RelativeLayout rLayout = (RelativeLayout)view.getTag(1);
parentLinearLayout.removeView(rLayout);
}
});
rLayout.addView(imageView);
rLayout.addView(closeButton);
parentLinearLayout = (LinearLayout) findViewById(R.id.horizontal_scrolling_linear_layout);
parentLinearLayout.addView(rLayout);
}
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>