Android ScrollView seems doesn't recognize its direct child - android

I'm using a ScrollView to show all the products that users add to his shopping cart.
I create a scroll view and, into it, i create a linear layout 'intern'.
Then, i build every single row in loop.
Here's the XML
<ScrollView
android:id="#+id/scroll"
android:layout_width="300dp"
android:layout_height="230dp"
android:layout_marginLeft="10dp" >
<LinearLayout
android:id="#+id/intern"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
</ScrollView>
Here's the Java code.
for(Carrello cns : carrello){
LinearLayout prodotto = new LinearLayout(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(/**/LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
prodotto.setLayoutParams(lp);
//Product name
TextView nome_p = new TextView(context);
nome_p.setWidth(dpToPx(115));
nome_p.setTextColor(getResources().getColor(R.color.grey));
nome_p.setText(cns.getNomeProdotto()); /**/
//Counts
TextView quantita = new TextView(context);
quantita.setWidth(dpToPx(80));
quantita.setGravity(Gravity.CENTER);
quantita.setTextColor(getResources().getColor(R.color.grey));
quantita.setTextAppearance(context, R.style.quantita);
quantita.setText(cns.getQuantita());
//Price
TextView prezzo = new TextView(context);
prezzo.setWidth(dpToPx(70));
prezzo.setPadding(dpToPx(5), 0, 0, 0);
prezzo.setTextColor(getResources().getColor(R.color.grey));
prezzo.setTextAppearance(context, R.style.prezzo);
prezzo.setText("€"+cns.getCosto());
//Cancella
Button canc = new Button(context);
LinearLayout.LayoutParams lp_btn = new LinearLayout.LayoutParams(/*LinearLayout.LayoutParams.WRAP_CONTENT*/40,40/*LinearLayout.LayoutParams.WRAP_CONTENT*/);
lp_btn.setMargins(0, dpToPx(10), 0, 0);
canc.setLayoutParams(lp_btn);
canc.setBackground(getResources().getDrawable(R.drawable.btn_canc));
canc.setId(Integer.parseInt(cns.getIDprodotto()));/**/
Log.d("ID-CANC",String.valueOf(canc.getId()));
//create the line
prodotto.addView(nome_p);
prodotto.addView(quantita);
prodotto.addView(prezzo);
prodotto.addView(canc);
intern.addView(prodotto);
i++;
}//end for
Then, add intern to the scroll view:
scroll.addView(intern);
But when i run the emulator, logCat says that "ScrollView can host only one direct child", and the app crashes.

Don't use scroll.addView(intern);,
because you have already defined child in SCrollView in your layout.xml
Instead try as below.
LinearLayout intern = (LinearLayout) findViewById(R.id.intern);
then use your for loop to add views in intern
That,s it...
No need to call addview for scroll
Hope this helps

The ScrollView already has a child defined in the layout file.
scroll.addView(intern); will cause error.
Just add
scroll=(ScrollView) findViewById(R.id.intern) before the loop and remove scroll.addView(intern);.

Related

How to show TextView programmatically?

I have a RelativeLayout called current_layout which I place my views on. When I attempt to addView(TextView) , nothing is displayed. However when adding an ImageView, it works just fine. Why is my TextView not displaying?
public static void draw_shard(int x, int y, int amount_collected){//X and Y are GAMESURFACE values. Needs to increment by gamesurface y.
ImageView shard = create_iv(); // Creates a new instance of an ImageView (parameter is the context of MainActivity)
shard.setBackgroundDrawable(shard_icon);
shard.setX(x);
shard.setY(y+ImageLoader.get_score_bar_height());
TextView tv = new TextView(MainActivity.current_context);
tv.setX(shard.getX() + shard.getWidth());
tv.setY(shard.getY());
tv.setTypeface(Variables.joystix);
tv.setTextSize(shard.getHeight());
tv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
tv.setText("+" + amount_collected);
tv.setTextColor(Color.WHITE);
current_layout.addView(shard);
current_layout.addView(tv);
}
I am adding the TextView on top of a black background also.
The problem was with shard.getWidth() and shard.getHeight() , which were returning 0.
An alternate and easy way to do that is:
Add the TextView in the layout file and set its visibility to gone, and when you need to show the TextView, just change the visibility of that TextView.
Sample code for XML file:
<TextView
android:id="#+id/textview"
android:layout_height="wrap_content"
android:layout-width="wrap_content"
android:visibility="gone">
<!-- Add other attributes too -->
And when you need that TextView, add this line of code:
findViewById("textview").setVisibility(View.VISIBLE);
findViewById("textview").setText("" + amount_collected);
// Create LinearLayout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
// Create TextView
TextView product = new TextView(this);
product.setText(" Product");
ll.addView(product);
Please try it.
But why are you adding TextView using java code?
You can easily do it in XML.
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="#color/colorPrimary"
android:textColor="#color/colorPrimary" />
It will help you to understand
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(this);
textView.setText("Your Text that you want to add");
linearLayout.addView(textView);
Thanks

Programmatically added checkbox with title does not fit in the layout in Android

I am developing an Android app. In my app I am adding checkboxes programmatically to a LinearLayout. But after the checkboxes are added, it does not properly fit to the layout.
Screenshot:
As you can see in screenshot "x-samll" text and its checkbox are not fitted properly. What I want is both checkbox and its text together go to new line when there is not enough space. How can I achieve it?
This is how I programmatically add checkboxes:
if(attrs.length()>0)
{
LinearLayout attrControlsSubContainer = new LinearLayout(getBaseContext());
attrControlsSubContainer.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
attrControlsSubContainer.setLayoutParams(layoutParams);
for(int i=0;i<attrs.length();i++)
{
CheckBox chkAttribute = new CheckBox(getBaseContext());
chkAttribute.setText(attrs.getJSONObject(i).getString("name"));
chkAttribute.setTextColor(Color.BLACK);
chkAttribute.setId(attrs.getJSONObject(i).getInt("id"));
attrControlsSubContainer.addView(chkAttribute);
}
attributeControlContainer.addView(attrControlsSubContainer);
}
use below code:
if(attrs.length() > 0) {
ScrollView mScrollView = new HorizontalScrollView(getApplicationContext());
mScrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mScrollView.setFillViewport(true);
LinearLayout attrControlsSubContainer = new LinearLayout(getBaseContext());
attrControlsSubContainer.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); attrControlsSubContainer.setLayoutParams(layoutParams);
for(int i=0;i<attrs.length();i++)
{
CheckBox chkAttribute = new CheckBox(getBaseContext());
chkAttribute.setText(attrs.getJSONObject(i).getString("name"));
chkAttribute.setTextColor(Color.BLACK);
chkAttribute.setId(attrs.getJSONObject(i).getInt("id"));
attrControlsSubContainer.addView(chkAttribute);
}
mScrollView.addView(attrControlsSubContainer);
attributeControlContainer.addView(mScrollView);
}
LinearLayout is not enough for this you must use FlowLayout
<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">
</com.wefika.flowlayout.FlowLayout>
Gradle Dependancy :- compile 'org.apmem.tools:layouts:1.10#aar'
Then add check box dynamically in FlowLayout
use FlowLayout.LayoutParams
FlowLayout.LayoutParams params = new FlowLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

How to create Scrollview programmatically?

I have one table "TABLE_SUBJECT" which contain a number of subjects. I need to create
one horizontal scroll view with Subject.
How do I create a ScrollView with database items programmatically? If I enter 1o subject then it will be appear in scroll view as a button. Is it possible?
you may create it as below:
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(yourTableView);
if you have many elements first you need to wrap-up and add in the Scroll view; for example i need a many text view inside of scrollview, so you need to create ScrollView->LinearLayout->Many textview
ScrollView scrollView = new ScrollView(context);
scrollView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TextView textView = new TextView(context);
textView.setText("my text");
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.RIGHT);
linearLayout.addView(textView);
scrollView.addView(linearLayout);
this may help you.
HorizontalScrollView hsrll = (HorizontalScrollView)findViewById(R.id.hrsll);
b = new Button(this);
for (int i = 0; i < 5; i++) {
b.setWidth(LayoutParams.WRAP_CONTENT);
b.setHeight(LayoutParams.WRAP_CONTENT);
b.setText("b"+i);
b.setId(100+i);
hsrll.addView(b);
}
instead of for loop just modify the code as your need(no of records in db). but this the code for creating buttons in dynamically.
I was doing it like this:
Create xml with LinearLayout inside the ScrollView
Create xml as item in ScrollView
In activity set main content as xml with ScrollView
Loop through all table elements with adding new View to LinearLayout form main view
For me works fine.
In Kotlin you can use the below code
val scroll = ScrollView(context)
scroll.setBackgroundColor(R.color.transparent)
scroll.layoutParams = LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT
)
scroll.addView(yourTableView)

Dynamically Add A TextView - Android

How can I dynamically add a TextView to this? The commented out code doesn't work.
public class myTextSwitcher extends Activity {
private TextView myText;
public myTextSwitcher(String string){
//myText = new TextView(this);
//myText.setText("My Text");
}
}
You're creating a text view and setting its value but you're not specifying where and how it should be displayed. Your myText object needs to have a container of some sort which will make it visible.
What you're trying to do is dynamically layout a view. See here for a good starter article. From the article:
// This is where and how the view is used
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
// this part is where the containers get "wired" together
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
First of all, you shouldn't be adding it in the constructor, non-default constructors are pretty much useless for an Activity. Finally, you are correctly creating a new TextView but you are not adding it anywhere. Get ahold of some layout in your content view (probably with findViewById), and call layout.addView(myText) with it.
Did you add the your text view to the activity using setContentView(myText);
make this
myText = new TextView(this);
myText.setText("foo");
setContentView(myText);
in oncreate() method
final TextView tv1 = new TextView(this);
tv1.setText("Hii Folks");
tv1.setTextSize(14);
tv1.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout ll = (LinearLayout) findViewById(R.id.lin);
ll.addView(tv1);
Your activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
</LinearLayout>

Dynamically adding content to RelativeLayout

Since I'm still just learning Android (and it appears Amazon says it'll be 2 months till I get the Hello, Android book) I'm still playing around with doing simple things. I have no problem getting an icon to display with the click of a button on my RelativeLayout using ImageView. The code for creating it is as follows:
private int mIconIdCounter = 1;
private ImageView addIcon(){
ImageView item = new ImageView(this);
item.setImageResource( R.drawable.tiles );
item.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
if( mIconIdCounter != 1 ){
params.addRule(RelativeLayout.RIGHT_OF, 1 );
}
item.setLayoutParams( params );
item.setId( mIconIdCounter );
++m_IconIdCounter;
return item;
}
and the code to add the item is:
Button addButton = (Button)findViewById(R.id.add_new);
addButton.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View view) {
addContentView( addIcon(), new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ) );
}
});
When I click my button what happens is all the newly created views are placed atop one another. I'd like them to be placed to the right of the next element. I did a quick search on SO for articles relating to RelativeLayout and found some that were similar (here, here, here, and here) but while these addressed getting the content into the RelativeView they didn't seem to address the positioning aspect.
What am I doing wrong?
EDIT:
My main xml looks like:
<?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"
>
<Button
android:id="#+id/add_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_new"
android:layout_alignParentBottom="true" />
</RelativeLayout>
It looks like you might be adding the view to the root of the layout xml instead of the RelativeLayout.
You could try:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.my_layout);
layout.addView(addIcon());
you are creating new relative layout inside function call. So every time new relative layout created and it added in the view when click button . Use common relative layout.

Categories

Resources