I have the following code:
scroll = (ScrollView) findViewById(R.id.scrollView1);
...
public void addItem(String str, int id) {
LinearLayout lay = new LinearLayout(this);
lay.setId(id);
TextView txt = new TextView(this);
txt.setText(str);
lay.addView(txt);
scroll.addView(lay);
}
And when i call addItem() once it's Okay, but when i call it twice or more, like this:
addItem("text1",1);
addItem("text2",2);
my app crashes :(
It's because ScrollView can only host 1 direct child.
You could create a LinearLayout as the only child of the ScrollView, and then add to the LinearLayout instead of the ScrollView in your addItem method.
scroll = (ScrollView) findViewById(R.id.scrollView1);
LinearLayout lay = new LinearLayout(this);
scroll.addView(lay);
// maybe do some more with lay here, or define it in xml instead of adding it here in the code
...
public void addItem(String str, int id) {
LinearLayout lay2 = new LinearLayout(this);
lay2.setId(id);
TextView txt = new TextView(this);
txt.setText(str);
lay2.addView(txt);
lay.addView(lay2);
}
Related
I want to parse text, and create for each word - button, but i don't know how to arrange them one after the other
String s = "Alice was beginning to get very tired of sitting";
String[] q = s.split(" ");
for (int i = 0; i < q.length; i++) {
Button myButton = new Button(this);
myButton.setText(q[i]);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
layout.addView(myButton, params);
}
See this custom library: FlowLayout
While you're adding views inside FlowLayout, it automatically wraps when there is no space for the next item.
There's not much wrong about your approach, it's only that relative layout as name suggests requires child views to have some parameters to align the views relative to them e.g. above, below etc. As a result you are getting views overlapping each other and hence only the last added view is visible being on top.
Use FlowLayout instead and you'll be fine.
You need to define RelativeLayout parameters as in example below
Heres an example to get you started, fill in the rest as applicable:
TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);
The docs for RelativeLayout.LayoutParams and the constructors are
here
From: How to add a view programmattically to RelativeLayout?
Check the link below to get more useful informations.
Hope it will help
In the following code, you should change the upper limits of the for, to a variable.
public class MainActivity
extends Activity
implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=13; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=9; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setOnClickListener(this);
tr.addView(b, 30,30);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
public void onClick(View view) {
((Button) view).setText("*");
((Button) view).setEnabled(false);
}
} // class
I want that the user can scroll throw my HorizontalScrollView and if he press my Button, a TextView will be shown on the current position of my HorizontalScrollView.
So far I already know how to present a TextView, but not on a specific position...
TextView textView;
textView = new TextView(MainActivity.this);
textView.setText(editText.getText().toString());
linearLayout.addView(textView);
Any help is welcomed!
Do some thing like this ,
final HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scroller);
final RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
findViewById(R.id.addButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView iv = new TextView(Act2.this);
iv.setText(new Date().toString());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(300, 400);
params.leftMargin = scrollView.getScrollX();
params.topMargin = scrollView.getScrollY();
container.addView(iv, params);
}
});
And the relative layout should be inside the HorizontalScrollView. This will add a textview exactly in the current position , but you should also write code for avoiding the overlapping the TextViews.
I've tried with a piece of code but it is not displaying vertical scroll bar. my code is pasted below:
public void init() {
popupButton = (Button) findViewById(R.id.textview1);
popupText = new TextView(this);
insidePopupButton = new Button(this);
layoutOfPopup = new LinearLayout(this);
LinearLayout lt=new LinearLayout(this);
view=new ScrollView(this);
insidePopupButton.setText("OK");
popupText.setText("This is Popup Window.press OK to dismiss it.");
popupText.setBackgroundColor(Color.WHITE);
popupText.setPadding(0, 0, 0, 20);
layoutOfPopup.setOrientation(1);
lt.addView(popupText);
layoutOfPopup.addView(insidePopupButton,350,35);
layoutOfPopup.setBackgroundColor(Color.BLACK);
view.addView(lt);
layoutOfPopup.addView(view);
Thank you in advance..:)
ScrollView cannot be combine with popupWindow in android, no matter what. Sad but true.
You need to add your views in this way:
LinearLayout linearLayout = new LinearLayout(this);
// add all your views to linearLayout(or RelativeLayout)
linearLayout.addView(popupText);
linearLayout.addView(insidePopupButton);
// add linearLayout to ScrollView instance
view.addView(linearLayout);
// add ScrollView instance to main layout
layoutOfPopup.addView(view);
ScrollView is a single element container.
I had to implement the expand/collapse(show/hide) a particular view on click of a some other view..Finally got it working but with a small problem.
I have a relative layout with two children: first is the textview and second is a linear layout.
On the click event of the textview i set the visibility(VISIBLE or GONE) of the following linear layout.
Now the problem is that after the linear layout is visible it somehow manages to hide the textview..
I tried textview.bringToFront() but it just makes the textview overlap the first row of the linearlayout ie the textview comes on top of the linear layout content.
I tried putting the textview in a linearlayout, but it makes no difference.
I tried setting the linear layout as BELOW. All in vain..
I know the textview exists because when i click the first row(which is overlapping the textview) the click event gets fired..
All i want is that no matter what my textview should be visible and the linear layout must take its position below the textview if it is visible..
EDIT
RelativeLayout wrapperlayout = new RelativeLayout(getActivity());
//wrapperlayout.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT));
//-------------HANDLE---------------------------
TextView txtHeader = new TextView(getActivity());
txtHeader.setOnClickListener(new OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) v;
RelativeLayout rParent = (RelativeLayout) tv.getParent();
LinearLayout lInner = (LinearLayout) rParent.getChildAt(1);
if(lInner.isShown())
{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.GONE);
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up));
}
else{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.collapse, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.VISIBLE);
lInner.setTop(tv.getBottom());
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down));
}
}
});
txtHeader.setText("Header");
txtHeader.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
txtHeader.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
//--------------CONTENT-------------------------
LinearLayout lContent = new LinearLayout(getActivity());
lContent.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
lContent.setOrientation(LinearLayout.VERTICAL);
HashMap<String, String> MySet = new HashMap<String, String>();
MySet = getData();
Iterator<String>RowItr = MySet.keySet().iterator();
int rowcnt = 0;
while (RowItr.hasNext()) {
LinearLayout lRow = new LinearLayout(getActivity());
LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lRow.setLayoutParams(lparams1);
lRow.setOrientation(LinearLayout.HORIZONTAL);
TextView txtLbl = new TextView(getActivity());
txtLbl.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
TextView txtVal = new TextView(getActivity());
txtVal.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
String Lbl = RowItr.next();
txtLbl.setText(Lbl);
if(rowcnt % 2 != 0)
lRow.setBackgroundColor(Color.parseColor("#dbe4f0"));
else
lRow.setBackgroundColor(Color.parseColor("#ffffff"));
txtVal.setText(MySet.get(Lbl));
lRow.addView(txtLbl);
lRow.addView(txtVal);
lRow.setPadding(10, 10, 10, 10);
lContent.addView(lRow);
rowcnt++;
}
lContent.setVisibility(View.GONE);
wrapperlayout.addView(txtHeader);
wrapperlayout.addView(lContent);
RelativeLayout.LayoutParams rPARAMS = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
//rPARAMS.addRule(RelativeLayout.ABOVE, txtHeader.getId());
//rPARAMS.addRule(RelativeLayout.BELOW, lContent.getId());
wrapperlayout.setLayoutParams(rPARAMS);
well you can store your LinearLayout as an instance variable, and simply call layout.setvisibility(View.GONE); in your onClick method. Doubt theres any other solution unless you want to save the parent(the layout that both of your views are attached to) and perform findViewById inside onClick or call getChildAt(1) orso
Got a solution.. i tried setting margins..and it all worked out well.. te handle and content both are visible without hiding any other views..
I am creating a dynamic interface based on a string from sharedpreferences.
Heres my code
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
CreateInterface();
}//End-OnCreate
public void CreateInterface()
{
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
TableRow[] tr = null;
// Here is a loop that creates tablerow, create button in that, and add tablerow to tablelayout tl. This part is irrelevant couse it works perfectly.
ll.addView(tl);
sv.addView(ll);
setContentView(sv);
}
Now, i want to change the background color of the screen. How would i go about doing that?
ll.setBackgroundColor() should does the job.
Set ScrollView or LinearLayout Background Color using below method.
sv.setBackgroundColor(); or ll.setBackgroundColor();
Get Color string using getResources().getColor(R.color.yourColorID);