How to manage linear sub layout programmatically? - android

Issue about layout aliment.
i want to add view dynamically.
Logical code :
linear1 = (LinearLayout) findViewById(R.id.parent);
linear2 = (LinearLayout) findViewById(R.id.chiled);
int len = 4;
for (int i = 1; i <= len; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params1.gravity = Gravity.RIGHT;
final int id_txt;
ImageView iv = new ImageView(this);
iv.setId(i);
id_txt = iv.getId();
iv.setBackgroundResource(R.drawable.ic_launcher);
linear1.addView(iv, params);
iv = ((ImageView) findViewById(id_txt));
for (int j = 1; j < 2; j++) {
final int id_;
Button btn = new Button(this);
btn.setId(i);
id_ = btn.getId();
btn.setText("button " + id_);
linear2.addView(btn, params1);
btn = ((Button) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(v.getContext(),
"Button clicked index = " + id_,
Toast.LENGTH_SHORT).show();
}
});
}
// btn.setBackgroundColor(Color.rgb(70, 80, 90));
// linear1.addView(txt, params);
// params.addRule(RelativeLayout.RIGHT_OF, txt.getId());
iv.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"text clicked index = " + id_txt,
Toast.LENGTH_SHORT).show();
}
});
}
}
Xml Code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/chiled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
I want to add image in parent view and two button in child view dynamical.
I inspires to make this type view form
Android heterogeneous gridview like pinterest?
It should be like as
current output as
I don't know where is the problem.
One strange issues i am facing now in my editor if i see layout in code then its shows me android:orientation="vertical" and if i see in outline that shows android:orientation="horizontal" for each layout. how is it possible ?
Help me to solve out.Thanks

EDIT:
First off, start by adding your buttons to the xml. Along with the image view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/extra_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/chiled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Like" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dislike" />
</LinearLayout>
</LinearLayout>
You can then remove any concept of adding views, as they already exist.
package com.example.yul;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class extra extends Activity {
Button like, dislike;
LinearLayout root, sub;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.extra);
like = (Button) findViewById(R.id.button1);
dislike = (Button) findViewById(R.id.button2);
// add button listeners here.
ImageView iv = (ImageView) findViewById(R.id.image);
iv.setBackgroundResource(R.drawable.ic_launcher);
}
}
}
This will only display the one image though, with buttons.
What you need to do is apply this xml, and code to a gridView or similar. As you will have id clashes otherwise.

Since the horizontal LinearLayout chiled is the first child of the vertical LinearLayout parent it is aligned as the first item. So adding Views to it will place them on top.
Try moving the chiled Layout to the root LinearLayout root.

Related

Remove all images created by Imageview

I am creating n number of images with wordImage and assign a differnt Id.This happens on a button click add. There is another button remove when I click that all the images that I have created should be removed from the view(Head is the view). Plz help how to do that.
onclick of add button
for (int getwordcount = 0; getwordcount <5; getwordcount++) {
int i=0;
WordImage = new ImageView(this);
WordImage.setId(getwordcount);
WordImage.setBackgroundResource(alphabetsimages[getwordcount]);
RelativeLayout.LayoutParams para=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
para.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
para.leftMargin=maragin+=54;
para.bottomMargin=25;
para.width=BtnNext.getWidth()/3;
para.height=BtnNext.getHeight();
WordImage.setLayoutParams(para);
WordImage.setTag(getSplitString);
Head.addView(WordImage);
}
onclick of remove button
only the last -------------> Head.removeView(WordImage);
create image is removed from view.
How do I remove all images created using wordimage.Any help would be greatly appreciated.
WordImage saves link just for the last ImageView. Previous link lost in this line:
WordImage = new ImageView(this);
This is why Head.removeView(WordImage) removes only the last image.
If you want delete all added image elements, the easiest way do this is to add RelativeLayout container like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout android:id="#+id/Head" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:onClick="onClickAdd"
android:id="#+id/button" android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remove"
android:onClick="onClickRemove"
android:layout_alignBaseline="#id/button"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Then you can delete all images only by one command:
Head.removeAllViews();
In other case, if you need to remove separate images, you have to delete your items in cycle similar to your "add" block:
for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
Head.removeView(findViewById(getwordcount));
}
Activity class will be like this:
public class MyActivity extends Activity {
RelativeLayout Head;
int alphabetsimages[] = {android.R.drawable.arrow_down_float, android.R.drawable.btn_dropdown, android.R.drawable.btn_minus, android.R.drawable.ic_dialog_map};
ImageView WordImage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickAdd(View v){
int margin = 0;
for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
int i=0;
WordImage = new ImageView(this);
WordImage.setId(getwordcount);
WordImage.setBackgroundResource(alphabetsimages[getwordcount]);
RelativeLayout.LayoutParams para=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
para.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
para.leftMargin=margin+=54;
para.bottomMargin=25;
para.width = 40;
para.height = 40;
WordImage.setLayoutParams(para);
Head= (RelativeLayout)findViewById(R.id.Head);
Head.addView(WordImage);
}
}
public void onClickRemove(View v){
//Head.removeAllViews();
for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
Head.removeView(findViewById(getwordcount));
}
}
}
I hope that's what you need!

How to align these 2 buttons next to each other programmatically in Android?

I have written this piece of code. But it is not giving the proper result. Please let me know where is the mistake. And I don't want to use Linear Layout.
Here is the xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/custom_relativeLayout1"
android:orientation="horizontal"
android:background="#ffffff">
</RelativeLayout>
</LinearLayout>
String[] but = {"Hello", "Bye"};
int buttonCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customLayout = (RelativeLayout) findViewById(R.id.custom_relativeLayout1);
//customLayout is object of relativelayout.
buttonCount = but.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button [] butArray = new Button[buttonCount];
for (int i = 0; i < 2; i++)
{
butArray[i] = new Button(this);
butArray[i].setLayoutParams(params);
RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
butArray[i].setText(but[i]);
butArray[i].setId(i+1); // Setting the ids
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
if (butArray[i].getId() != 1)
{
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
}
else
{
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
}
}
RelativeLayout.RIGHT_OFhoryzontally or vertically align?
so for vertically
Btnparams.addRule(RelativeLayout.BELOW, butArray[i-1].getId());
customLayout.addView(butArray[i], Btnparams);
or for horizontally
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
customLayout.addView(butArray[i], Btnparams);
instead of
Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
butArray[i].setLayoutParams(Btnparams);
customLayout.addView(butArray[i]);
LinearLayout is very nice to use if you want do dynamically add Views and scale them the same. For example if you want to add buttons and have max 3 buttons per row, you can also use another LinearLayout to make a new row.
I 'abuse' the LinearLayout row and a Button for layoutparams templating.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_root"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="#+id/ll_row1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"/>
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"/>
</LinearLayout>
</LinearLayout>
Example code:
// Get LL and template params
LinearLayout root = (LinearLayout) findViewById(R.id.ll_root);
LinearLayout row1 = (LinearLayout) findViewById(R.id.ll_row1);
ViewGroup.LayoutParams llRowParams = row1.getLayoutParams();
ViewGroup.LayoutParams btnParams = findViewById(R.id.btn1).getLayoutParams();
// Make new button
Button newBtn = new Button(context);
newBtn.setLayoutParams(btnParams);
newBtn.setText("Button 3");
// Add button to row1
row1.addView(newBtn);
// Add new row
LinearLayout row2 = new LinearLayout(context);
row2.setLayoutParams(llRowParams);
root.addView(row2);
// TODO: add buttons to new row
// TODO: some logic to decide between adding to row or creating new row and adding button
Note: code is not tested, but you should get the idea.
buttonCount = but.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button [] butArray = new Button[buttonCount];
for (int i = 0; i < 2; i++)
{
butArray[i] = new Button(this);
butArray[i].setLayoutParams(params);
RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
butArray[i].setText(but[i]);
butArray[i].setId(i+1); // Setting the ids
butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher, 0, 0);
butArray[i].setBackgroundColor(Color.TRANSPARENT);
if (i != 0) {
Btnparams.addRule(RelativeLayout.RIGHT_OF, i-1);
}
customLayout.addView(butArray[i], Btnparams);
}

button with image is not responding to onclick listener when scrolling

I am a android beginner, I have added images dynamically inside the linear layout, which will scroll from right to left. The problem is that the images are moving / scrolling properly but when I click on it the on-click listener is not called.
Actually when moving the images the image button is moving but the onclick position is not changed according to the image scrolling.
public class ImageLayoutsActivity extends Activity {
int ImageInt, fromXDelta, toXDelta;
int PosInt, myHarizantalLayoutInt, aDisplayInt, aDisplayDiv = 0;
AnimationSet myAnimation = new AnimationSet(true);
LinearLayout myHarizantalLayout;
HorizontalScrollView myHorizontalScroll;
View myView;
Intent myIntent;
private Button clickedButton = null;
public int currentimageindex = 0;
TranslateAnimation myTranslateAnimation = null;
private String[] imgArr = { "icn01", "icn02" };
private String[] URLArray = { "http://www.google.co.in/",
"http://in.yahoo.com/?p=us" };
LinearLayout.LayoutParams Parems;
Display aDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View aView = LayoutInflater.from(GroupActivity.myGroup).inflate(
R.layout.horizantal, null);
setContentView(aView);
GroupActivity.myGroup.setTitle("---CII---");
aDisplay = getWindowManager().getDefaultDisplay();
aDisplayInt = aDisplay.getWidth();
aDisplayDiv = aDisplayInt / 5;
Log.i("value##########################", String.valueOf(aDisplayDiv));
Log.i("aDisplayInt##########################",
String.valueOf(aDisplayInt));
// define xml layout here
myHarizantalLayout = (LinearLayout) findViewById(R.id.horiztonal_outer_layout_id);
myHorizontalScroll = (HorizontalScrollView) findViewById(R.id.horiztonal_scrollview_id);
// Button aBtnOne = (Button) findViewById(R.id.BtnOneId);
// Hide HorizantalLayout scroll bar
myHorizontalScroll.setHorizontalScrollBarEnabled(false);
myHorizontalScroll.isSmoothScrollingEnabled();
myHarizantalLayout.measure(aDisplay.getWidth(), aDisplay.getHeight());
Log.v("EmailActivity#########", myHarizantalLayout.getMeasuredHeight()
+ ", " + myHarizantalLayout.getMeasuredWidth());
int aLayoutInt = myHarizantalLayout.getMeasuredWidth();
Log.i("aLayoutInt###########", String.valueOf(aLayoutInt));
// Add images in for loop
for (int i = 0; i < imgArr.length; i++) {
// int aVal=myHarizantalLayout.getChildCount();
Log.i("ImageInt############################", "=====Inside the loop======");
// create button instance
final Button aImgBtn = new Button(this);
// myButton.setOnClickListener(null);
// add background image resource files
ImageInt = getResources().getIdentifier(imgArr[i], "drawable",
getPackageName());
Log.i("ImageInt############################", "ImageInt");
// add integer image values to drawable control
Drawable aDrawable = this.getResources().getDrawable(ImageInt);
Log.i("aDrawable$$$$$$$$$$$$$$$$$$$$$$$$$", "aDrawable");
// add drawable file to button instance
aImgBtn.setBackgroundDrawable(aDrawable);
aImgBtn.measure(aDisplay.getHeight(), aDisplay.getWidth());
Log.i("aButton#############################", "aButton");
Parems = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Parems.setMargins(10, 0, 5, 0);
Log.i("Parems%%%%%%%%%%%%%%%%%%%%%%%%%%", "Parems");
// int aParemIn = Parems.width;
// Log.i("aparemIn$$$$$$$$$$$$", String.valueOf(aParemIn));
myHarizantalLayout.measure(aDisplay.getHeight(),
aDisplay.getWidth());
int myHarizantalLayoutInt = myHarizantalLayout.getMeasuredWidth();
Log.i("myHarizantalLayoutInt$$$$$$$$$$$$",
String.valueOf(myHarizantalLayoutInt));
myTranslateAnimation = new TranslateAnimation(aDisplayInt - 300,
-myHarizantalLayoutInt - aDisplayDiv, 0, 0);
myTranslateAnimation.setDuration(20000);
myTranslateAnimation.setRepeatCount(-1);
myTranslateAnimation.setRepeatMode(1);
myAnimation.addAnimation(myTranslateAnimation);
Log.i("myAnimation###########################", "myAnimation");
aImgBtn.setLayoutParams(Parems);
myHarizantalLayout.addView(aImgBtn);
Log.i("myHarizantalLayout&&&&&&&&&&&&&&&&&&&&&&&&&",
"myHarizantalLayout");
// add animation to HarizantalLayout
myHarizantalLayout.startAnimation(myTranslateAnimation);
// Call webview based on image button click event
aImgBtn.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Log.i("view$$$$$$$$$$$$#############", "---btn clicked -----");
int aVal = myHarizantalLayout.indexOfChild(view);
Log.i("indexOfChild*************************",
"indexOfChild");
if (view == aImgBtn) {
Log.i("aButton%%%%%%%%%%%%%%%%%%%%%%%%", "aButton");
aImgBtn.setId(aVal);
String aUrlStr = URLArray[aVal];
Log.i("aUrlStr$$$$$$$$$$$$#############", aUrlStr);
}
// Log.i("aUrlStr$$$$$$$$$$$$#############", aUrlStr);
// Pass values to webview activity
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="bottom" android:background="#00f">
<LinearLayout android:layout_below="#+id/RelativeLayout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal" android:gravity="bottom">
<ImageView android:id="#+id/barImageId"
android:layout_width="150dp" android:layout_height="58dp"
android:background="#drawable/cii" android:layout_alignParentRight="true" />
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="58dp" android:id="#+id/horiztonal_scrollview_id"
android:fadingEdge="none" android:background="#000" >
<LinearLayout android:id="#+id/horiztonal_outer_layout_id"
android:fadingEdge="none" android:layout_height="fill_parent" android:gravity="center_vertical"
android:layout_width="fill_parent" android:background="#f00">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
Try to Use Pager instead of HorizontalScrollView,
This link may be helpful https://github.com/nostra13/Android-Universal-Image-Loader

linear layout inside linear layout is adding but is not visible

I'm having one empty Linear Layout in xml. And I'm adding some text view to it dynamically.
once these textviews exceeds 5 then i'm creating one more linear layout inside it and adding text views to newly created Layout. And finally adding this new layout to main layout.
I'm able to add, i.e in emulator it occupy that space but, will not display the info in the textviews.
my xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add TextViews" />
</LinearLayout>
and my java file is as follows:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AddTextViewsDynamically extends Activity implements
OnClickListener {
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_textview_dynamically);
button = (Button) findViewById(R.id.dyn_button1);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = null;
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2 = new LinearLayout(this);
layout2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
layout2.setPadding(10, 60, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.HORIZONTAL);
layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
layout.addView(layout2);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}
}
Where am I going wrong?
Your primary LinearLayout is set to Horizontal so the first 5 text view and the layout2 are shown on the same line. Adding Layout3 to Layout2 makes the Layout3 to be shown from the right of the last text view from primary Linear Layout. On a 10 inch tablet i see only the first 2 elements of your LinearLayout. Perhaps on a smaller screen you don't see them. Try using
text.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT));
instead of
text.setLayoutParams(new LayoutParams(155, LaoutParams.WRAP_CONTENT));
and you should see all your text views.
EDIT :
In your case this should do the trick;
xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip" >
</LinearLayout>
<LinearLayout
android:id="#+id/dyn_layout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
android:padding="10dip" >
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add TextViews" />
</LinearLayout>
and code :
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = (LinearLayout) findViewById(R.id.dyn_layout2);
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2.setPadding(10, 60, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.HORIZONTAL);
layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}
Actually you are doing things right, I just removed padding and made orientation of all Linear Layouts to vertical. Modify this on xml file too and it will work :))) Its just you are adding a padding of 10 or 60 which is going of the view taken by the parent.
Modified Java Code:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dyn_button1:
LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
LinearLayout layout2 = null;
LinearLayout layout3 = null;
for (int i = 0; i < 10; i++) {
if (i > 4) {
if (i == 5) {
layout2 = new LinearLayout(this);
layout2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);
// layout2.setPadding(10, 10, 10, 10);
layout3 = new LinearLayout(this);
layout3.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
layout3.setOrientation(LinearLayout.VERTICAL);
// layout3.setPadding(10, 10, 10, 10);
}
System.out.println("**** Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout3.addView(text);
if (i == 9) {
System.out
.println("Added second linear layout to first");
layout2.setVisibility(View.VISIBLE);
layout2.addView(layout3);
layout.addView(layout2);
}
} else {
System.out.println("###### Adding text view " + i);
TextView text = new TextView(this);
text.setText("The Value of i is :" + i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(155,
LayoutParams.WRAP_CONTENT));
layout.addView(text);
}
}
}
}
Try this one, then you will see that your code is OK.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="80" >
<LinearLayout
android:id="#+id/dyn_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/dyn_button1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="20"
android:text="Add TextViews" />
</LinearLayout>

Android: dynamically set the position of button into center of screen in RelativeLayout

There has been many questions on dynamically setting the position of a button, but I just want to change the position of a button into center of the screen. My layout is RelativeLayout.
Updated:
At the beginning, myButton has been set position in XML as:
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Menu" />
Any suggestion would be greatly appreciated
See an example below (click the button and it will be moved to the center).
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/my_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Menu" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends Activity {
private View mView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mView = findViewById(R.id.my_view);
mView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ViewGroup.LayoutParams vg_lp = mView.getLayoutParams();
// Make sure we are in RelativeLayout
if (vg_lp instanceof RelativeLayout.LayoutParams) {
RelativeLayout.LayoutParams rl_lp = new RelativeLayout.LayoutParams(vg_lp);
rl_lp.addRule(RelativeLayout.CENTER_IN_PARENT);
mView.setLayoutParams(rl_lp);
}
}
});
}
}
Try:
......
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) button.getLayoutParams();
layoutParams.leftMargin = parentLayout.getWidth()/2;
layoutParams.topMargin = parentLayout.getHeight()/2;
button.setLayoutParams(layoutParams);
......
Try this one:
RelativeLayout RL = (RelativeLayout)findViewById(R.id.relative_layout);
RL.gravity(Gravity.CENTER);
with this all subviews of RL will be in center;
Button anyButton = new Button(this);
RL.addSubview(anyButton);//this genereted button will be in center as well
Try this,
RelativeLayout my_layout = new RelativeLayout(this);
my_layout.setId(some_value);
Button my_btn = new Button(this);
RelativeLayout.LayoutParams my_params = new RelativeLayout.LayoutParams(btn_height,btn_width);
my_params.addRule(RelativeLayout.CENTER_IN_PARENT,my_layout.getId());
my_layout.addView(my_btn,my_params);
Add this 2 lines to the component you want to center in the xml file :
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

Categories

Resources