i have some xml layout file.this is a my layout code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/rot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff0000" >
<ImageView
android:id="#+id/btn_categorry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
i wrote some code witch can add layout programmatically.this is a my code
staticlayout = new RelativeLayout(getApplicationContext());
staticlayout.setBackgroundColor(Color.parseColor("#000000"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
//
//
add_btn.setLayoutParams(parms2);
add_btn.setOnClickListener(new listener());
staticlayout.addView(add_btn);
parms.addRule(RelativeLayout.BELOW, R.id.rot);
staticlayout.setLayoutParams(parms);
mainlayout.addView(static layout);
it's working perfect.now i want to add another layout,witch would be below layout with i add programmatically.
this is a my full code
public class MainActivity extends Activity {
RelativeLayout myImage, mainlayout;
private ImageView img;
RelativeLayout staticlayout;
RelativeLayout.LayoutParams parms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (RelativeLayout) findViewById(R.id.rot);
mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
img = (ImageView) findViewById(R.id.btn_categorry);
parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
staticlayout = new RelativeLayout(getApplicationContext());
staticlayout.setBackgroundColor(Color.parseColor("#000000"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
//
//
add_btn.setLayoutParams(parms2);
add_btn.setOnClickListener(new listener());
staticlayout.addView(add_btn);
parms.addRule(RelativeLayout.BELOW, R.id.rot);
staticlayout.setLayoutParams(parms);
mainlayout.addView(staticlayout);
// parms.addRule(RelativeLayout.BELOW, R.id.rot);
}
});
}
class listener implements OnClickListener {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams costomparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout relativelayout = new RelativeLayout(getApplicationContext());
relativelayout.setBackgroundColor(Color.parseColor("#AB3232"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams imagelayoutparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imagelayoutparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imagelayoutparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
//
//
add_btn.setLayoutParams(imagelayoutparam);
relativelayout.addView(add_btn);
costomparam.addRule(RelativeLayout.BELOW, staticlayout.getId());
relativelayout.setLayoutParams(costomparam);
mainlayout.addView(relativelayout);
}
}
}
at the moment this part does not working
costomparam.addRule(RelativeLayout.BELOW, staticlayout.getId());
what am i doing wrong? if anyone knows solution please help me
thanks
Before you doing:
staticlayout.addView(add_btn);
please try to add something like
staticlayout.setId(999);
i am not sure, but where did you add your staticLayout? make sure you have added staticlayout before calling staticlayout.getId() and you have also set an id to your staticLayout (which is missing in your code snippets), otherwise your desired rule should not work expectedly
I think you forgot to call setId to your staticlayoutbefore calling staticlayout.getId().
Create a resource named ids.xml in values folder.
Yours should look similar to this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="your_static_layout_id" type="id" />
</resources>
Related
I wanted to re-create my XML layout programatically, but it doesn't seem to work. I found out how to (supposedly) create it programatically from another thread. Any help is appreciated, thanks.
The XML:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test1"
android:id="#+id/txt1"
android:paddingTop = "36dp"
android:paddingRight = "36dp"
android:paddingLeft = "18dp"
android:paddingBottom = "36dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test2"
android:layout_toRightOf="#id/txt1"
android:id="#+id/txt2"
android:paddingTop = "36dp"
android:paddingRight = "36dp"
android:paddingBottom = "36dp"/>
</RelativeLayout>
Here is the code I'm attempting to match the XML above:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
// Creating a new TextView
TextView txt1 = new TextView(this);
txt1.setText("Test1");
TextView txt2 = new TextView(this);
txt2.setText("Test2");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.RIGHT_OF, txt1.getId());
txt1.setPadding(18,36,36,36);
txt2.setPadding(0,36,36,36);
// Setting the parameters on the TextView
txt1.setLayoutParams(lp);
txt2.setLayoutParams(lp2);
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(txt1);
relativeLayout.addView(txt2);
// Setting the RelativeLayout as our content view
setContentView(relativeLayout, rlp);
}
}
What I see when I run it programmatically
I want to create a ImageView dynamically and place it at the center of the center. I tried doing it in xml and I was able. I tried to convert the same thing to code but the imageView always appears at the top left corner of the screen. Can somebody point out what the mistake is ? I'm still new to Android and finding my way. There might be blunders as well.
Thanks
The XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<ImageView
android:id="#+id/myimg"
android:layout_width="100dip"
android:layout_height="100dip"
android:scaleType="fitXY"
android:layout_gravity="center_horizontal"
android:background="#drawable/exmp"
/>
</LinearLayout>
The code
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Resources res = getResources();
String pluginPackageName = this.getApplicationContext().getPackageName();
int anim = res.getIdentifier("anim_android", "anim",pluginPackageName);
int transparent_style = res.getIdentifier("TransparentProgressDialog", "style", pluginPackageName);
int drawable_spinner = res.getIdentifier("exmp", "drawable", pluginPackageName);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams((100),( 100));
imageViewParams.gravity = Gravity.CENTER_HORIZONTAL;
ImageView iv = new ImageView(getApplicationContext());
iv.setLayoutParams(imageViewParams);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageResource(drawable_spinner);
layout.addView(iv, imageViewParams);
addContentView(layout, imageViewParams);
}
To make it exactly as in the XML layout file you have to change addContentView(layout, imageViewParams); to setContentView(layout);
EDIT:
full code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Resources res = getResources();
String pluginPackageName = this.getApplicationContext().getPackageName();
//int anim = res.getIdentifier("anim_android", "anim",pluginPackageName);
//int transparent_style = res.getIdentifier("TransparentProgressDialog", "style", pluginPackageName);
//int drawable_spinner = res.getIdentifier("exmp", "drawable", pluginPackageName);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams((100),( 100));
imageViewParams.gravity = Gravity.CENTER_HORIZONTAL;
ImageView iv = new ImageView(getApplicationContext());
iv.setLayoutParams(imageViewParams);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageResource(R.mipmap.ic_launcher);
layout.addView(iv, imageViewParams);
setContentView(layout);
}
You have this as the last line in onCreate:
addContentView(layout, imageViewParams);
Perhaps you meant this?
addContentView(layout, layoutParams);
Alternatively, maybe this:
setContentView(layout, layoutParams);
Use layoutGravity insted of gravity.
imageViewParams.layoutGravity = Gravity.CENTER_HORIZONTAL;
You've to set the layout_gravity. You can do this way:
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
I am trying to add some buttons to a Relative Layout programatically. I used some examples seen on StackOverflow, but for some reason I cannot understand, my code is not working: I want to have my buttons one above the other, but they end up being one on top of the other.
Here is my layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.android.myApp.MainActivity" >
</RelativeLayout>
And here is my code:
mLayout = (RelativeLayout) findViewById(R.id.mainLayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btnTag = new Button(this);
btnTag.setText("Button0");
btnTag.setId(0);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
lprams.addRule(RelativeLayout.BELOW, 0);
btnTag = new Button(this);
btnTag.setText("Button2");
btnTag.setId(1);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
Checkout this code....Hope this will help
mLayout = (RelativeLayout) findViewById(R.id.mainLayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btnTag = new Button(this);
btnTag.setText("Button0");
btnTag.setId(10);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
lprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lprams.addRule(RelativeLayout.BELOW, 10);
btnTag = new Button(this);
btnTag.setText("Button2");
btnTag.setId(1);
btnTag.setOnClickListener(mGlobalOnCLickListener);
mLayout.addView(btnTag, lprams);
I am having a relative layout I want to create the layout dynamically the layout in XMl lookes like
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp"
android:background="#drawable/step_background"
android:orientation="vertical" >
I made this like -
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lprams.setMargins(5,5,5,5);
But how to add the background I am doing this in a Fragment not in a activity
try as follows...
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lprams.setMargins(5,5,5,5);
layout.setLayoutParams(lprams);
layout.setBackgroundResource(R.drawable.step_background);
Try This : -
RelativeLayout mRelativeLayoutPopup = new RelativeLayout(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(5,5,5,5);
mRelativeLayoutPopup.setLayoutParams(layoutParams);
mRelativeLayoutPopup.setBackgroundResource(drawable_background);
You can use setBackground for that.
Drawable bg_image= getResources().getDrawable(R.drawable.image);
myLayout.setBackground(bg_image);
Hope it helps.
When i used this XML file...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
..I see that background color wrap content
http://img716.imageshack.us/img716/8225/rlayout.jpg
But if i write this RelativeLayout by code...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setBackgroundColor(Color.parseColor("#FF0000"));
TextView text = new TextView(this);
text.setText("Hello world");
//AƱado texto
layout.addView(text);
layout.setLayoutParams(params);
setContentView(layout);
}
...I see that background color match parent instead of wrap content
http://img404.imageshack.us/img404/1427/rlayoutfull.jpg
Any ideas?
Thanks!
you are never setting the layout params:
layout.setLayoutParams(params);
and I think that you should place the:
TextView text = new TextView(this);
text.setText("Hello world");
and all other view you will add to your layout before you setting the layout params, otherwise there will be no content to wrap-around.
Edit:
what happens when you set:
tried to replace between the two:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
?