Trying to center buttons, TextView and Image View I’m creating Dynamically - android

I have a program that generates a rows of 4 buttons, in a horizontal container that gets added to a vertical container.
I cannot figure out how to have it so the buttons are centered, now they are aligns to the right hand side. Code:
for(int i=0; i<cGlobals.mNames.length; i+=2) {
iSoundIdList[i]=soundPool.load(this, cGlobals.mSounds[i], 1);
iSoundIdList[i+1]=soundPool.load(this, cGlobals.mSounds[i+1], 1);
// would like these views to all be centered
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(llp);
favBut[i]=new ImageView(this);
favBut[i].setImageResource(R.drawable.heartunselected);
favBut[i].setId(defStartFavId+i);
favBut[i].setOnClickListener(this);
layout.addView(favBut[i]);
Button but1=new Button(this);
but1.setText( cGlobals.mNames[i]);
but1.setWidth(120);
layout.addView(but1);
but1.setOnClickListener(this);
but1.setId(defStartButId+i);
TextView t=new TextView(this);
t.setText(" ");
layout.addView(t);
favBut[i+1]=new ImageView(this);
favBut[i+1].setImageResource(R.drawable.heartunselected);
favBut[i+1].setId(defStartFavId+i+1);
favBut[i+1].setOnClickListener(this);
layout.addView(favBut[i+1]);
t=new TextView(this);
t.setText(" ");
layout.addView(t);
Button but2=new Button(this);
but2.setText( cGlobals.mNames[i+1]);
but2.setWidth(120);
but2.setId(defStartButId+i+1);
but2.setOnClickListener(this);
layout.addView(but2);
Container.addView(layout);
}

Below snippet will help
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
ImageView imageView = new ImageView(this);
imageView.setImageResource(getResources().getIdentifier(
"sample_image", "drawable", getPackageName()));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(lp);
relativeLayout.addView(imageView);

Related

Display a Linear Layout programmatically in a loop

Hello everyone please i need help. I'm a biginner in Android programming so since i was trying to solve this problem it took me all my days that why i want you to help me please. I'm trying to display a Linear layout according to the loop condition but the issue is that when i'm doing that it's just displaying the last one and i'm not able to see others when it's displaying. Here are my codes
ScrollView sv = new ScrollView(this);
sv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
sv.setPadding(5, 5, 5, 5);
sv.setBackgroundColor(getResources().getColor(R.color.colorWhite));
LinearLayout ll = null;
for (int i=0; i < 2; i++){
try {
//Here iam defining the LinearLayout
ll = new LinearLayout(this);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(params);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackground(getDrawable(R.drawable.circle_view));
//Here iam defining the RelativeLayout
RelativeLayout Rl = new RelativeLayout(this);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Rl.setLayoutParams(param);
//Defining a layout params for widgets
RelativeLayout.LayoutParams image = new RelativeLayout.LayoutParams(100, 90);
image.addRule(RelativeLayout.ALIGN_LEFT);
//Creating widgets
ImageView im = new ImageView(this);
im.setImageResource(R.mipmap.airtel);
im.setId(R.id.image1);
im.setLayoutParams(image);
//----------------
RelativeLayout.LayoutParams txtone = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
txtone.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
txtone.setMargins(0, 5, 0, 45);
TextView txtVone = new TextView(this);
txtVone.setText("ArkaL"+i);
//txtVone.setTextSize(R.dimen.MainTextSize);
txtVone.setTextSize(15);
txtVone.setTypeface(txtVone.getTypeface(), Typeface.BOLD);
txtVone.setLayoutParams(txtone);
//------------------------------------------------
RelativeLayout.LayoutParams txttwo = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
txttwo.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
txttwo.setMargins(0, 45, 0, 0);
TextView txtVtwo = new TextView(this);
txtVtwo.setText("République Démocratique du Congo");
txtVtwo.setTypeface(txtVtwo.getTypeface(), Typeface.SERIF.getStyle());
txtVtwo.setTextSize(10);
txtVtwo.setLayoutParams(txttwo);
Rl.addView(im);
Rl.addView(txtVone);
Rl.addView(txtVtwo);
RelativeLayout.LayoutParams recycler = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RecyclerView rv = new RecyclerView(this);
rv.setLayoutParams(recycler);
ll.addView(Rl);
ll.addView(rv);
}catch (Exception e){
}
}
//Here for assigning Linear to ScrollView
sv.addView(ll);
this.setContentView(sv);
Please help me.
your problem is that you created one LinearLayout and changed it in a loop and then added it to a ScrollView so at the end you will have just one LinearLayout. as you may know an ScrollView should have just one View as a child view so you should add a LinearLayout for a ScrollView and add as many LinearLayout as you want to the primary LinearLayout. your code can be like this:
LinearLayout llMain = new LinearLayout(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llMain.setLayoutParams(params2);
llMain.setOrientation(LinearLayout.VERTICAL);
sv.addView(llMain);
LinearLayout ll = null;
for (int i = 0; i < 2; i++) {
try {
//Here iam defining the LinearLayout
ll = new LinearLayout(this);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(params);
ll.setOrientation(LinearLayout.VERTICAL);
...
llMain.addView(ll);
} catch (Exception e) {
...
note that the Height of primary linearlayour should wrap its content;

setMargins to relative layout not working in Grid layout

GridLayout glSameLoactions;
#OnClick(R.id.tvAddLocation)
void addLocationOnClickListener(){
if(i==0) {
glSameLoactions = new GridLayout(this);
glSameLoactions.setColumnCount(3);
glSameLoactions.setBackgroundResource(R.drawable.shape_rounded_corner_blurfilled);
glSameLoactions.setPadding(16,16,16,16);
}
RelativeLayout rlLocation = new RelativeLayout(this);
rlLocation.setPadding(16, 16, 16, 16);
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(300,200);
relativeParams.setMargins(16, 16, 16, 16);//<<<<----- NOT WORKING
rlLocation.setLayoutParams(relativeParams);
rlLocation.requestLayout();
rlLocation.setBackgroundResource(R.drawable.shape_rounded_corner_blurfilled);
TextView tvLocationName = new TextView(this);
tvLocationName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tvLocationName.setId(R.id.tvLocationName);
tvLocationName.setText("amritsar,PB");
tvLocationName.setTextColor(getResources().getColor(R.color.white));
tvLocationName.setTextSize(14f);
ImageView ivRadiobtn = new ImageView(this);
tvLocationName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ivRadiobtn.setImageResource(R.drawable.ic_circle_empty);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rlLocation.addView(tvLocationName);
rlLocation.addView(ivRadiobtn,lp);
glSameLoactions.addView(rlLocation);
if (i==0) {
llLocations.addView(glSameLoactions);
llLocations.setVisibility(View.VISIBLE);
}
i++;
}
I am making a layout programmatically, in which on a click of button, a new relative layout is added in a grid layout. All my code is working fine, but the problem is that i am unable to set margins to the relative layout inside the grid layout. Please help!!
Thanks
You should use RelativeLayout.LayoutParams instead of LinearLayout.LayoutParams.
Try this:
RelativeLayout.LayoutParams relativeParams =new RelativeLayout.LayoutParams(300, 300);
relativeParams.setMargins(16, 16, 16, 16);
rlLocation.setLayoutParams(relativeParams);

RelativeLayout with ImageView and TextView - Align TextView below ImageView, both center in RelativeLayout Programatically

I have a RelativeLayout with ImageView and TextView. I want to the TextView right below the ImageView(with a small padding) and both the ImageView and TextView, aligned to center in the RelativeLayout.
The RelativeLayout is added Programatically
I have read some questions on SO regarding the alignment, but, I can't get them to work for me. Below is my current code...
Code for RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
Code for ImageView
ImageView image = new ImageView(this);
RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpImage.addRule(RelativeLayout.CENTER_IN_PARENT);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
relativeLayout.addView(image);
Code for TextView
TextView textview = new TextView(this);
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTextView.addRule(RelativeLayout.BELOW, image.getId());
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
relativeLayout.addView(textview);
If I set RelativeLayout.CENTER_IN_PARENT for both image and text, they overlap eachother, which is understandable as RelativeLayout supports overlapping of views.
I thought setting RelativeLayout.BELOW for textview will make it align itself below the image, but it's not the case. I even tried RelativeLayout.ALIGN_BOTTOMfor textview, but even that didn't work.
Try this..
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams lprela = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(lprela);
ImageView image = new ImageView(this);
RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpImage.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
relativeLayout.addView(image);
TextView textview = new TextView(this);
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTextView.addRule(RelativeLayout.BELOW, image.getId());
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
relativeLayout.addView(textview);
Or
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(lp_ineer_ver);
ImageView image = new ImageView(this);
LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
linearLayout.addView(image);
TextView textview = new TextView(this);
LinearLayout.LayoutParams lpTextView = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
linearLayout.addView(textview);
Below process may also work for you.
1) Add Image view & Text view in vertical LinearLayout.
2) Add the linear layout to Center of Relative layout.(using CENTER_IN_PARENT).

Creating an imageview without using XML in Android [duplicate]

This question already has answers here:
ImageView without xml definition
(2 answers)
Closed 7 years ago.
I want to create an Imageview programmatically without using XML. Is there any way to create it using the height, width and margin property.
Is it possible to add multiple component like this to different places of the screen using the height, width and margin property?
I wrote some code you can use. It relies on the fact that your container is a RelativeLayout:
ImageView myImageView = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.topMargin = myTopMargin;
params.leftMargin = myLeftMargin;
myImageView.setLayoutParams(params);
myImageView.setImageResource(R.id.my_image_resource);
myRelativeLayout.addview(myImageView);
Hope this helps :)
try this..
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//setting image margin
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
imageView.setLayoutParams(layoutParams);
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
Hope this will help..
Use this:
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.something);
//setting image position
//set the margin to the layoutparams
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
//add view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);

How to overlay text on image programmatically

I have a image placed in the relative layout. How can i place a small image over the background image and two text view side by side next to the image. Any help will be really appreciated. The only thing is I want to do this programmatically. I have tried doing it the following way but for some reason the textview appears on the top left of the screen. I want it appear on the imagebutton.
LinearLayout LinearLayout = new LinearLayout(this);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams;
Button[] btn = new Button[1];
btn[0] = new Button(this);
btn[0].setId(1);
btn[0].setGravity(Gravity.CENTER);
btn[0].setText("text");
btn[0].setBackgroundResource(R.drawable.button_grey);
btn[0].setTextColor(Color.parseColor("#FFFFFF"));
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT | RelativeLayout.ALIGN_PARENT_TOP);
relativeLayoutParams.setMargins(20, 20, 15, 15);
relativeLayout.addView(btn[0], relativeLayoutParams);
ImageButton[] Imgbtn = new ImageButton[10];
Imgbtn[0] = new ImageButton(this);
Imgbtn[0].setId(2);
Imgbtn[0].setBackgroundResource(R.drawable.box_round_corners);
Imgbtn[0].setMaxHeight(200);
Imgbtn[0].setClickable(true);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.BELOW,btn[0].getId());
relativeLayoutParams.height = 100;
relativeLayoutParams.setMargins(15, 0, 15, 10);
Imgbtn[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(Imgbtn[0], relativeLayoutParams);
TextView[] txtview = new TextView[10];
txtview[0] = new TextView(this);
txtview[0].setId(3);
txtview[0].setTextColor(Color.parseColor("#000000"));
txtview[0].setText("text");
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT | RelativeLayout.ALIGN_TOP);
relativeLayoutParams.addRule(RelativeLayout.BELOW,Imgbtn[0].getId());
relativeLayoutParams.height = 20;
relativeLayoutParams.setMargins(5, 5, 5, 5);
txtview[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(txtview[0], relativeLayoutParams);
android.widget.ScrollView ScrollV = new ScrollView(this);
ScrollV.addView( relativeLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addContentView(ScrollV, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
try to set your views LayoutParams with setLayoutParams() method;
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(width,height);
lp.topMargin=//what is your desired y coordinate
lp.leftMargin=//what is your desired x coordinate
lp.gravity=Gravity.NO_GRAVITY;
view.setLayoutParams(lp);
FrameLayout is better choise for overlaying purposes in my opinion.

Categories

Resources