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
Related
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>
I have the following XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingRight="4dip" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="32dip"
android:layout_height="32dip"
android:layout_marginLeft="4dip"
android:src="#drawable/test_imag" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/imageView1" />
</RelativeLayout>
and the following bindView in a cursor
public void bindView(View view, Context context, Cursor cursor) {
wrapper = (RelativeLayout) view.findViewById(R.id.background);
TextView tview2 = (TextView) view.findViewById(R.id.text2);
ImageView imgview = (ImageView) view.findViewById(R.id.imageView1);
tview2.setText("Test text");
if (cursor.getPosition() % 2 == 0) {
tview2.setBackgroundResource(R.drawable.bubble_left);
} else {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tview2.setBackgroundResource(R.drawable.bubble_right);
params.addRule(RelativeLayout.LEFT_OF, R.id.imageView1);
tview2.setLayoutParams(params);
wrapper.setGravity(Gravity.RIGHT);
}
}
I'm trying to do a bubble chat with an image, the IF part is fine, the image is on the rght side of the screen, and, the text is to the left of it.
In the else part I set the gravity to the right and add a rule to set tview2 to the left of the image.
The result of the above made the image go to the right side of the screen but the tview2 has gone, what did I do wrong?
You need to specify params for the imageview as well since you've declared it initially in the xml to be relative to the textview.
imageparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
The result would be something like this.
else {
RelativeLayout.LayoutParams imageparams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imgview.setLayoutParams(imageparams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tview2.setBackgroundResource(R.drawable.bubble_right);
params.addRule(RelativeLayout.LEFT_OF, R.id.imageView1);
tview2.setLayoutParams(params);
wrapper.setGravity(Gravity.RIGHT);
}
I am new in android and I have done so much googling for this issue. Actually I want to create below UI in Code of android:
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/tab_background">
<RelativeLayout
android:id="#+id/event_1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/color_001">
<TextView
android:id="#+id/txt_event_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="1dp"
android:gravity="center"
android:text="j1"
android:textStyle="bold"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
Can anybody help me, please?
I have written a quick example code based on your xml layout. I tried make it clear by using comments but if you don't understand something, I can try to explain it a little more.
// Creating the outer RelativeLayout which has id "relativeLayout2" in your xml layout.
RelativeLayout outerRelativeLayout = new RelativeLayout(context);
// ------------------------------------------------------------------
// Creating the inner RelativeLayout which has id "event_1" in your xml layout.
RelativeLayout innerRelativeLayout = new RelativeLayout(context);
// Creating the TextView which has id "txt_event_1" in your xml layout.
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setText("j1");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams textViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
textViewParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
// Adding the TextView to the inner RelativeLayout as a child
innerRelativeLayout.addView(textView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// ------------------------------------------------------------------
// Adding the inner RelativeLayout to the outer RelativeLayout as a child
outerRelativeLayout.addView(innerRelativeLayout, new RelativeLayout.LayoutParams(convertDptoPx(40), convertDptoPx(40)));
// ------------------------------------------------------------------
// Defining the layout parameters of the outer RelativeLayout
RelativeLayout.LayoutParams rootParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
// Set the layout parameters associated with the outer RelativeLayout.
outerRelativeLayout.setLayoutParams(rootParams);
Although this code snippet doesn't include some of the xml attributes defined in your xml layout, you can find the related methods in the documentation of the view.
For instance, if you look at the TextView documentation (http://developer.android.com/reference/android/widget/TextView.html), you can see the "XML Attributes" table which shows the attribute name and related method.
use this code.
RelativeLayout layout1= new RelativeLayout(this);
LayoutParams param1= new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
RelativeLayout layout2= new RelativeLayout(this);
LayoutParams param2= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TextView text= new TextView(this);
text.setText("your text");
layout2.addView(text, param2);
layout1.addView(layout2, param1);
setContentView(layout1);
RelativeLayout.LayoutParams childVw = new RelativeLayout.LayoutParams(40, 40);
TextView textVw = new TextView(this);
textVw.setLayoutParams(childVw);
Then add this to your main relative layout
setContentView(R.layout.layoutfilename);
RelativeLayout layout1= (RelativeLayout)findViewById(R.id.yourlayoutid);
LayoutParams param1= new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
RelativeLayout layout2= new RelativeLayout(this);
LayoutParams param2= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TextView text= new TextView(this);
text.setText("your text");
layout2.addView(text, param2);
layout1.addView(layout2);
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);
?
How to add scrollbar or scrollview in android with class java not xml this code?
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.srvr, null);
layout = (RelativeLayout) view.findViewById(R.id.relativeLayout1);
RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(labelLayoutParams);
labelLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
labelLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
labelLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
labelLayoutParams.leftMargin=120;
final EditText tt = new EditText(this);
tt.setTag(i);
tt.setText(DisplayName_list[i].toString());
tt.setMinWidth(230);
tt.setMaxWidth(230);
tt.setMaxLines(1);
As I understood well you want to create a ScrollView programatically. You create it as any other views:
ScrollView sc = new ScrollView(this);
sc.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
sc.setFillViewport(true);
And then you have to add to it your view. Make sure that your view you are adding to the ScrollView is a root view.
sc.addView(ROOT_VIEW)
In your case ROOT_VIEW is a layout which is RelativeLayout
include this as parent
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
// Here should b your Layout tag
</ScrollView>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_below="#+id/activity_main_webview"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />