Not being able to add a textView to LinearLayout through code - android

I am a beginner of android, and was studying the layout.
What i am trying to do is i am trying to add a textView to a LinearLayout through code, and the application stops with the error that the application has stopped unexpectedly. The code is:
setContentView(R.layout.main);
LinearLayout abc = (LinearLayout)findViewById(R.id.cLayout);
TextView tv = new TextView(this);
tv.setText("Text Changed!!!!");
abc.addView(tv);
And in the xml, id is set like: android:id="#+id/cLayout".
I know there are better ways to do it, but i want to know why this doesn't work. Contents of textView are changing just fine. Also i have also tried
LinearLayout abc = (LinearLayout)findViewById(R.layout.main);
What am i doing wrong? One thing is clear that i am accessing uncreated items (unassigned pointer).
Edit
Main xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/cLayout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:id="#+id/myTV"
/>
</LinearLayout>

I've tried to reproduce it, and it works for me, no exceptions.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"/>
</LinearLayout>
MyActivity.java:
public class MyActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll = (LinearLayout) findViewById(R.id.cLayout);
TextView tv = new TextView(this);
tv.setText("Hello Again!");
ll.addView(tv);
}
}
UPD When I posted your code, I got an exception, too. Make sure you call super.onCreate().

Related

Why can I not add a TextView to my LinearLayout in Android?

I am having a bunch of trouble adding a TextView to my LinearLayout. Here is what I have:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some button text" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout);
TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setText("Hello, world!");
mainLayout.addView(textView);
}
}
I don't get any errors in LogCat, it just doesn't work.
Any ideas?
There is no space for your TextView because Button is taking them all.
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="some button text" />
Change it to wrap_content
Make sure the spacing between your views is correct
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some button text" />
</LinearLayout>
You can also try mainLayout.addView(newView, 0); to add it before the button for verification
Brian is right. There is also an typo in the first line. I guess it should be android.com
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
and not android/com.

Display two TextViews from a Layout

In Xamarin, how can I display two objects (TextViews in this case) vertically that refer to the same Resource.Id?
Here is my Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/TextViewAutoLink"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all">
</TextView>
</LinearLayout>
Here is my C# code:
SetContentView (Resource.Layout.AutoLinkTextView);
TextView Web = (TextView)FindViewById(Resource.Id.TextViewAutoLink);
Web.Text = "Test address of http://www.google.com";
TextView Web2 = (TextView)FindViewById(Resource.Id.TextViewAutoLink);
Web2.Text = "Test address of http://www.stackoverflow.com";
The Web2 TextView is the only TextView that is being displayed.
Can I have some help please?
Thanks in advance
You can use "\n" between both texts. Like:
SetContentView (Resource.Layout.AutoLinkTextView);
TextView Web = (TextView)FindViewById(Resource.Id.TextViewAutoLink);
Web.Text = "Test address of http://www.google.com"+"\n"+"Test address of
http://www.stackoverflow.com";
Yuo can add two TextViews in your Layout.
Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/TextViewAutoLink1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all">
</TextView>
<TextView
android:id="#+id/TextViewAutoLink2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all">
</TextView>
</LinearLayout>
Source code
SetContentView (Resource.Layout.AutoLinkTextView);
TextView Web = (TextView)FindViewById(Resource.Id.TextViewAutoLink1);
Web.Text = "Test address of http://www.google.com";
TextView Web2 = (TextView)FindViewById(Resource.Id.TextViewAutoLink2);
Web2.Text = "Test address of http://www.stackoverflow.com";
You are getting same TextView id for two different TextView.
So when yousetting your text , it's overriding previous text.
So basically you need two different TextView.

Android application doesn't run with AdMob

I have problem with AdMob in my Android application. I have done exactly everything described here, but when I start the program, it doesn't run (there are a lot of bugs). I will show you a fragment of program:
public class MenuMainActivity extends Activity{
private AdView adView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_main);
adView = new AdView(this, AdSize.BANNER, "(code-15 signs)");
RelativeLayout layout = (RelativeLayout)findViewById(R.layout.activity_menu_main);
layout.addView(adView);
adView.loadAd(new AdRequest());
And also xml:
<RelativeLayout 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" >
<TextView
android:id="#+id/titleOfGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:text="#string/title_of_game_text" />
...
When I comments two lines:
//layout.addView(adView);
//adView.loadAd(new AdRequest());
Everything is ok. Where is the problem?
If you use findViewById() you must pass an id, there you are passing a "layout".
should be something like:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adLayer);
create a Layout with "adLayer" as the "id" and position it in your layout where you like the Ads to show up.
As the previous posters have mentioned you need to create a specific place in your layout where you want yout advert to appear, for example, using your supplied code:
<RelativeLayout 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" >
<TextView
android:id="#+id/titleOfGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:text="#string/title_of_game_text"/>
<RelativeLayout
android:id="#+id/adLayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
etc. etc./>
You then need to change the code in your activity to :
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adLayer);
as BrainCrash suggested.
Hope this helps!

Problem with LayoutInflator to set TextView Android

Hey folks Im using two layouts, one embedded through an 'include' in the main layout file.
I wish to set the TextView in the embedded one within the activity for the main xml. Here's what I've come up with so far......
Main xml: date_list_layout.xml
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/bgoption">
<include layout="#layout/cur"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</RelativeLayout>
Embedded xml: cur.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/currency"
android:textSize="14px"
android:paddingLeft="10dp"
android:textColor="#d17375"
></TextView>
</LinearLayout>
Then my Activity code sets the content to the main xml but tries to inflate the embedded one to set the text as follows......
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_list_layout);
View v = LayoutInflater.from(getBaseContext()).inflate(R.layout.cur,
null);
TextView currency = (TextView) v.findViewById(R.id.currency);
currency.setText("test");
}
I'm not getting any errors but the TextView remains empty. Any ideas what I'm doing wrong
Thanks
A
You can use setText directly in TextView. Without calling LayoutInflater in activity.
setContentView(R.layout.date_list_layout);
TextView currency = (TextView) v.findViewById(R.id.currency);
currency.setText("test");
Just directly use the TextView:
TextView currency = (TextView) findViewById(R.id.currency);
Once you have included cur.xml you no longer need to inflate manually. It is automatically inserted into the structure, so you can safely remove the additional LayoutInflater call.

Android ScrollView can host only one direct child

I have a onclick listener from the gallery that should clear all the rows inside the tableview, then add row/rows to a tableview inside a scrollview. The fragment should change on every button click.
However I am getting: java.lang.IllegalStateException: ScrollView can host only one direct child.
myactivity button listener
TrackerFragment tf = (TrackerFragment) getFragmentManager().findFragmentById(R.id.tracker1);
tf = TrackerFragment.newInstance(listOfList.get(id).get(position));
fragmentTransaction.add(R.id.tracker1, tf);
fragmentTransaction.commit();
t1 = true; //being used
getFragmentManager().executePendingTransactions();
tracker fragment
public class TrackerFragment extends Fragment
{
private Dish dish;
public static TrackerFragment newInstance(Serializable dish)
{
TrackerFragment tf = new TrackerFragment();
Bundle args = new Bundle();
args.putSerializable("dish", dish);
tf.setArguments(args);
return tf;
}
public static TrackerFragment newInstance(Bundle bundle)
{
Dish dish = (Dish) bundle.getSerializable("dish");
return newInstance(dish);
}
#Override
public void onCreate(Bundle myBundle)
{
super.onCreate(myBundle);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.tracker, container, false);
TableLayout tableLayout = (TableLayout) v.findViewById(R.id.tracker_layout);
tableLayout.removeAllViews();
if (dish != null)
{
//display others
//display subsystem stuff
for (int i = 0; i < dish.getSubsystems().size(); i++)
{
TableRow tableRow = new TableRow(v.getContext());
tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(v.getContext());
tv.setText(dish.getSubsystems().get(i).getConnFuncAddr());
tableRow.addView(tv);
tableLayout.addView(tableRow);
}
}
else
{
TableRow tableRow = new TableRow(v.getContext());
tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(v.getContext());
tv.setText("Click on image to view more data");
tableRow.addView(tv);
tableLayout.addView(tableRow);
}
return v;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Trackers -->
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="300dp"
android:layout_weight="1"
android:orientation="vertical">
<fragment
android:name="android.gallery.TrackerFragment"
android:id="#+id/tracker1"
android:layout_height="500dp"
android:layout_width="300dp"
android:layout_weight="1">
</fragment>
</LinearLayout>
<!-- Gallery -->
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="600dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="245dp">
<Gallery
android:id="#+id/galleryid0"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="245dp">
<Gallery
android:id="#+id/galleryid1"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="245dp">
<Gallery
android:id="#+id/galleryid2"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tracker_layout">
</TableLayout>
</ScrollView>
Does anyone know whats wrong? I am guessing I am adding a table row to the scrollview and not the tableview, however, I dont see where I am doing that wrong in my code.
In fact, you can't have things like :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/widget27"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TableLayout
android:id="#+id/layoutOption"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<ImageView
android:id="#+id/logoImmoweb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logoimmoweb"
android:layout_gravity="top"
/>
...
</TableLayout>
</ScrollView>
but if you have an extra element outside the table, it crash with an java.lang.IllegalStateException: ScrollView can host only one direct child
example
<ScrollView
android:id="#+id/widget27"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="#+id/logoImmoweb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logoimmoweb"
android:layout_gravity="top"
/>
<TableLayout
android:id="#+id/layoutOption"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
...
</TableLayout>
</ScrollView>
Hope this help.
I know this question is quite old but I ran into the same problem and it end up being that you cant have a ScrollView as you base view for the layout.
Just wrap the Scrollview in your tracker.xml layout with a RelativeLayout and you should be good to go
You should add LinearLayout into ScrollView, it will working.
Another possibility is that the closing element is not matching. Ie.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout ...>
...
</LinearLayout>
</LinearLayout>
The error is in the last line.
Um... don't put multiple views inside of a scroll view. Put a single view inside of it that is the kind of layout you want for your multiple views -- TableLayout, FrameLayout, etc.
edit (2nd answer):
I've been playing with the code and I'm still not sure WHY this happens, definitely there's something wrong on the fragment manager when it swapping fragments (replace(resId, fragment))
In my fragment onCreateView was this code:
View view = inflater.inflate(R.layout.post_fragment, null, false);
if (item == null)
return view;
return BuildView(view);
post_fragment is an empty canvas with 1 ScrollView, 1 LinearLayout inside it, and some other ViewGroups inside it. This is to load an empty canvas on the 1st load (item is null) and on the 2nd time, through a user input, it would get the actual data to show.
for some reason beyond my knowledge, as seen in the logcat, FragmentManagerImpl.moveToState was calling ScrollView.AddChild causing the error.
Now I changed my fragments onCreateView to:
if (item == null)
return inflater.inflate(R.layout.empty, null, false);
View view = inflater.inflate(R.layout.post_fragment, null, false);
return BuildView(view);
so that the 'empty' canvas is a FrameLayout with nothing on it, and now everything is working happily.
I'm sure this sounds like some odd error on the actual fragments framework, but it's a work around it.
1st answer:
Raptrex, as far as I can see. Yes, that table layout is the only child your scrowview have. I am having the exactly the same problem, and very frustrated trying to solve it.
From the LogCat/Debug, it seems that there's something in the Fragments manager that is calling and .AddView(view) in my ScrollView (check the log) it depends on how I change my XML layout I get this error or not, which let's say sounds pretty absurd!
Any ideas of how?why?what? I'm listening...
FATAL EXCEPTION: main
java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:219)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:787)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:977)
at android.app.BackStackRecord.run(BackStackRecord.java:638)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1309)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:398)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Try to use a <FrameLayout> instead of the <fragment> in your main.xml
So instead of
<fragment
android:name="android.gallery.TrackerFragment"
android:id="#+id/tracker1"
android:layout_height="500dp"
android:layout_width="300dp"
android:layout_weight="1">
</fragment>
use
<FrameLayout
android:id="#+id/tracker1"
android:layout_height="500dp"
android:layout_width="300dp"
android:layout_weight="1"
/>
It solved a similar problem with the ScrollView, where I was replacing the fragment from the activity with:
getSupportFragmentManager().beginTransaction()
.replace(R.id.details_container, new DetailsFragment(), DETAILFRAGMENT_TAG)
.commit();
I am suffering from this problem last week and then find the best possible answer of that problem.
Scrollview can host only one direct child

Categories

Resources