Controls overlap when trying to position programmatically using relative layout (Android) - android

I am trying to create a fragment. In the fragment I plan to place a text view next to it a Spinner control. I tried using this code. However Spinner always is placed over textview to the left of the screen. Can anyone suggest what could be the problem?
Code:
public class FragmentTest extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.ll2, null);
RelativeLayout lref = (RelativeLayout)view.findViewById(R.id.ll2ll);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(lref.getLayoutParams());
lp.addRule(RelativeLayout.ALIGN_LEFT);
RelativeLayout rl = new RelativeLayout(getActivity());
TextView tv = new TextView(getActivity());
tv.setId(1);
tv.setLayoutParams(lp);
tv.setText("My ");
rl.addView(tv, lp);
Spinner s = new Spinner(getActivity());
lp.addRule(RelativeLayout.RIGHT_OF, tv.getId());
s.setId(2);
String currencyData[] = {"USD",
"EUR","INR"
};
ArrayAdapter<Object> currencyAdapter; currencyAdapter = new ArrayAdapter<Object>(getActivity(), android.R.layout.simple_spinner_item, currencyData);
currencyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(currencyAdapter);
rl.addView(s,lp);
return rl;
}
}
XML file is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:isScrollContainer="true"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/ll2ll"
android:layout_width="140dp"
android:layout_height="50dp"
android:textSize="#dimen/font_size" />
</RelativeLayout>

When a view is inflated from a XML you will not get the LayoutParams properly. Try using new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(lref.getLayoutParams());
your problem is this, you can't use the same layout parameters for adding all the layouts and modifying the lp.
because the lp is used after you have initialized all the layouts, that means you are using same parameters for all the layouts spinner and textview both.
try using different layout parameters for all the layouts
RelativeLayout.LayoutParams lp
RelativeLayout.LayoutParams lptextview
RelativeLayout.LayoutParams lpspinner
for all of them
if your problem is solved mark my answser as correct

Related

Programmatically creating layouts and adding to view

I have to create a complicated layout (A receipt view) using a lot of
data. Because there can be x amount of charges or payments, this view must be done programmatically.
I am trying to build reusable layouts that I can attache to different textviews or edittexts and have them line up.
Broken down it looks something like this:
Which comes down to 2 basic layouts:
A full width (where the text is centered)
A Split column
a) where one column takes up 3/4's the view on either the left or right.
b) and the second column takes up 1/4 the view.
Here is my attempt. But I don't seem to be getting it right.
Here is my error:
The specified child already has a parent. You must call removeView()
on the child's parent first.
Can anyone help?
private ScrollView mScrollView;
private LinearLayout mLinearLayout;
private ProgressBar mProgressBar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_payment, container, false);
mScrollView = (ScrollView) view.findViewById(R.id.svPayment);
mScrollView.setVisibility(View.GONE);
mProgressBar = (ProgressBar) view.findViewById(R.id.pbPayment);
mLinearLayout = (LinearLayout) view.findViewById(R.id.llPayment);
return view;
}
public void setupGUI() {
//RESUABLE LAYOUT
LinearLayout.LayoutParams paramsFull = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramsFull.setLayoutDirection(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams paramSmall = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramSmall.setLayoutDirection(LinearLayout.HORIZONTAL);
paramSmall.weight = 0.2f;
LinearLayout.LayoutParams paramLarge = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramLarge.setLayoutDirection(LinearLayout.HORIZONTAL);
paramLarge.weight = 0.8f;
// HOLDS THE LEFT AND RIGHT COLUMNS
LinearLayout columnShell = new LinearLayout(getContext());
columnShell.setLayoutParams(paramsFull);
//Small column
LinearLayout columnSmall = new LinearLayout(getContext());
columnSmall.setLayoutParams(paramSmall);
//Large column
LinearLayout columnLarge = new LinearLayout(getContext());
columnLarge.setLayoutParams(paramLarge);
//First get rid of all the views, incase of refresh
mLinearLayout.removeAllViews();
TextView textView = new TextView(getContext());
//CUSTOMER
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
textView.setLayoutParams(fullwidth);
textView.setText("Mary Jane");
columnShell.addView(textView);
mLinearLayout.addView(columnShell);
LinearLayout columnRight = new LinearLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
columnRight.setLayoutDirection(LinearLayout.HORIZONTAL);
//LEFT SIDE (1/4)
textView = new TextView(getContext());
textView.setLayoutParams(fullwidth);
textView.setText("PO: ");
columnSmall.addView(textView);
columnShell.addView(columnSmall);
//RIGHT SIDE (3/4)
textView = new TextView(getContext());
textView.setLayoutParams(fullwidth);
textView.setText("4465465456");
columnLarge.addView(textView);
columnShell.addView(columnLarge);
mLinearLayout.addView(columnShell);
}
}
fragment_payment.xml
<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"
tools:context="com.mycompany.myapp.Views.MasterDetails.PaymentFragment">
<ProgressBar
android:id="#+id/pbPayment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:id="#+id/svPayment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/COLOR_LIGHT_GREY">
<LinearLayout
android:id="#+id/llPayment"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"></LinearLayout>
</ScrollView>
</LinearLayout>
Why use scrollview with linearlayout, you might have to use listview with two textview as chid, and to add new data just add it into list and notify to adpter, your new data automatically added last of the list.

Placing a view inside a layout dynamically at a specific position in Android

I am trying to create a dialog window in Android where I am dynamically creating the layout and adding the views in to it. But I want to add my view to a specific position in layout.
Here is my code snippet
final Dialog dialog = new Dialog(this);
FrameLayout fl=new FrameLayout(this);
TextView et = new TextView(this);
et.setText("asdas");
fl.addView(et,100,1200);
ColorDrawable cd=new ColorDrawable(android.graphics.Color.TRANSPARENT);
dialog.getWindow().setBackgroundDrawable(cd);
dialog.setContentView(fl);
I used this to place a image button at a specified location in my relative layout. hope this helps:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = (int)(marginLeft);
layoutParams.topMargin = (int)(marginTop);
imageButton.setLayoutParams(layoutParams);
EDIT:
You can do this for other layouts that support positioning of child views. This method works even for lower API versions. Atleast it worked on API 10. :)
yes you can do this ...something like this
AdView ad = new AdView(this, AdSize.SMART_BANNER, getString(R.string.admob_id));
LinearLayout control_container =(LinearLayout)findViewById(R.id.ad_container);
control_container.addView(ad);
AdRequest adRequest = new AdRequest();
ad.loadAd(adRequest);
and in your xml file...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E9E5E6" >
<LinearLayout
android:id="#+id/ad_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
suppose if you want to add TextView at (200,200) position
try this...
FrameLayout frameLayout = new FrameLayout(this);
TextView textView = new TextView(this);
textView.setTop(200);
textView.setLeft(200);
frameLayout.addView(textView, textViewWidth, textViewHeight);
I think this should work.
Dialog dialog = new Dialog(this);
FrameLayout frm_layout = new FrameLayout(this);
android.widget.FrameLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.leftMargin = 100;
params.topMargin = 1200;
TextView et = new TextView(this);
et.setText("asdas");
frm_layout.addView(et, params);
dialog.setContentView(frm_layout, new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.FILL_PARENT));

How to create Hierarchical RelativeLayouts programmatically in android?

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);

setBackgroundColor(int color) and RelativeLayout

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?

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" />

Categories

Resources