I'm relatively new to Android programming, and am having a problem with inflation. I'm trying to create a simple activity that spits out a few descriptive fields from a previously selected ListView item.
I can access the source data from the ListView without any problems, but the subsequent activity to display the selected data isn't working. Specifically, the following XML layout excerpt (in layout/info.xml) and corresponding code generates an activity that comes up OK, but doesn't display the string "XYZZY" as desired (as indicated above, the string is normally obtained dynamically from the ListView, but is simplified to a hard-coded string for this example).
What am I doing wrong? Here's the XML excerpt:
<?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="wrap_content"
android:background="#color/black"
android:padding="10dp" >
<!-- Info text -->
<TextView
android:id="#+id/infoname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:gravity="center_horizontal"
android:textColor="#color/white"
android:textSize="24sp" />
...
</RelativeLayout>
And here's the java excerpt:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
LayoutInflater li = getLayoutInflater();
View infoView = li.inflate(R.layout.info, null);
TextView name = (TextView) infoView.findViewById(R.id.infoname);
name.setText("XYZZY");
...
}
by setting the contentView() you don't need to do the inflating yourself. The reason you are not seeing your text is that the inflated layout is not added to the view hierarchy.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
TextView name = (TextView) findViewById(R.id.infoname);
name.setText("XYZZY");
...
}
You can't do setContentView(R.layout.info) and inflate in the same activity for the same layout, choose one of the approaches. For your example above there is the right code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater li = getLayoutInflater();
View infoView = li.inflate(R.layout.info, null);
setContentView(infoView);
TextView name = (TextView) infoView.findViewById(R.id.infoname);
name.setText("XYZZY");
...
}
Related
I have tried everything I can possibly think of, I have tried many solutions in Stack as well. None seem to resolve my issue. I am trying to pass a string that I passed from an earlier intent (parsed JSON data). Everytime I call set text I get a Null Pointer Exception, I have tried using a method to set the text as well as just setting it. Any assistance would be greatly appreciated.
This is the fragment I am trying to update.
public class HomeFragment extends Fragment{
TextView tv;
public HomeFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater lf = getActivity().getLayoutInflater();
View rootView = lf.inflate(R.layout.fragment_home, container, false);
tv = (TextView) rootView.findViewById(R.id.accountName);
tv.setText("test");
return rootView;
}
}
I just put a regular string in to test. Here is the XML.
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/lblCompany"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp" />
</RelativeLayout>
Thanks In advance for absolutely any help.
This is the id set on your TextView in xml:
android:id="#+id/lblCompany"
This is the id you are searching for in onCreateView:
tv = (TextView) rootView.findViewById(R.id.accountName);
The ids need to match for you to update the text in that particular TextView. Try
tv = (TextView) rootView.findViewById(R.id.lblCompany);
instead.
I have an inflated Linear Layout that contains 2 TextViews inside it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_m"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ll_borders"
android:tag="m"
android:text="m" />
<TextView
android:id="#+id/tv_q"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ll_borders"
android:tag="q"
android:text="q" />
</LinearLayout>
All i want is that when this Linear Layout is inflated then i want to get the only TEXTVIEW on which i click. For example if i click on "tv_m" then it shall only return me the text of tv_m.
May b its simple but i am not getting a way to it. So i need help.
Thanks
After inflating the layout get the textview objects as below
LinearLayout layout = inflater.inflate(<your layout name>, null);
TextView textView1 = layout.findViewById(R.id.tv_m));
TextView textView2 = layout.findViewById(R.id.tv_q));
String selectedText;
textView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
selectedText = textView1.getText().toString();
}
});
Similarly you can put listener for textView2 also. The selectedText will be the final string which you want.
You need to set up on click listeners for the text views. Then when one is clicked, it will call a function in your code passing it the view that was touched. Then you can call getText on it.
here is the code just check this out :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lt = (LinearLayout) findViewById( R.id.linearLayout );
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(your xml to be inflate, null, false);
lt.addView(view);
TextView tv_m = (TextView)view.findViewById( R.id.tv_m);
TextView tv_l = (TextView)view.findViewById( R.id.tv_l);
tv_m.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv_m.getText(); // to get the value written on text view
} });
}
I have a layout called view.xml which contains a View called view_related. Basically, in my dictionary app, if there are related words to an entry, I will replace view_related with a LinearLayout that contains a header "Related entries" and a bunch of TextViews representing every related word.
I keep getting a NullPointerException everytime I'm adding a TextView to the LinearLayout in my Activity's onCreate() method, and I don't understand why though my code looks pretty straightforward.
view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/view_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12pt"
android:textColor="#fff"
android:textStyle="bold"
android:background="#990011"
android:padding="10dp"
android:text="lalala word"
/>
<ScrollView android:id="#+id/view_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/view_header">
<LinearLayout android:id="#+id/view_description_content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="#string/demo_definition"
/>
<!-- THIS WILL BE REPLACED -->
<View android:id="#+id/view_related"
android:layout_width="match_parent"
android:layout_height="wrap_content"></View>
</LinearLayout>
</ScrollView>
</RelativeLayout>
view_related.xml, the LinearLayout that will replace the View element in view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_related_entries"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="#+id/view_related_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#fff"
android:textStyle="bold"
android:textSize="7pt"
android:background="#000"
android:text="Related entries"
/>
<LinearLayout android:id="#+id/view_related_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</LinearLayout>
</LinearLayout>
And finally, the onCreate method, which for now assumes the related entries are "Hello" and "Goodbye:"
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list);
TextView tv = new TextView(this);
tv.setText("Hello");
ll.addView(tv); // NULL POINTER EXCEPTION
tv = new TextView(this);
tv.setText("Goodbye");
ll.addView(tv);
LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = findViewById(R.id.view_related);
ViewGroup parent = (ViewGroup) view.getParent();
int index = parent.indexOfChild(view);
parent.removeView(view);
View llview = li.inflate(R.id.view_related_entries, parent, false);
parent.addView(llview, index);
}
setContentView(R.layout.view);
LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list);
The function findViewById only finds views that are actually set. Your current setContentView does not set an xml that has the id you are looking for in it, so the first line I quoted will not result in anything.
You should either load the correct XML, or inflate the view you are looking for with an inflater
the line (LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list);) will search for view_related_list in view.xml, as in the line before that is the view you are using..
You need to inflate view_related.xml first and then get the view from it..
You're doing a lot of wrong things in your code. First you search for a LinearLayout that isn't in the current layout and also isn't in any inflated layout at that moment. Second, you try to inflate an id reference instead of a layout reference. Last, you should use another approach for what you're trying to do. Your code should be(from what I understood):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = findViewById(R.id.view_related);
ViewGroup parent = (ViewGroup) view.getParent();
int index = parent.indexOfChild(view);
parent.removeView(view);
View llview = li.inflate(R.layout.view_related, parent, false); // is view_related the name of the extra layout file?
parent.addView(llview, index);
LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list); // or search for it in llview, llview.findViewById(R.id.view_related_list);
TextView tv = new TextView(this);
tv.setText("Hello");
ll.addView(tv); // NULL POINTER EXCEPTION
tv = new TextView(this);
tv.setText("Goodbye");
ll.addView(tv);
}
This is a situation when you should use a ViewStub to add the new layout file. Although, I don't understand why don't you add the new layout file directly in the view.xml if you're going to add the layout's content in the onCreate method.
i think you need to use setContentView(R.layout.view_related); instead of setContentView(R.layout.view);
or another simple thing you can do is take the linear layout in the same layout "view" and make it invisible when not required and visible when required
You may forgetting to declare your Activity in manifest.xml
I've got 2 layouts (main.xml and footer_layout.xml) and am trying to append the footer_layout to my listview in main. I can get it to display but it won't set the text when I use tvFooter.setText();
Main.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"
android:orientation="vertical" >
<ListView
android:id="#+id/listIssues"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
footer_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center" >
<TextView
android:id="#+id/footerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip">
</TextView>
</LinearLayout>
MainActivity code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.footer_layout);
//Context context = getBaseContext();
tvFooter = (TextView)findViewById(R.id.footerText);
//Load listview control.
setContentView(R.layout.main);
final Context context = getBaseContext();
mListViewIssues = (ListView)findViewById(R.id.listIssues);
int DBVersion = 3;
tvFooter.setText("DBVersion = " + String.valueOf(DBVersion));
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
mListViewIssues.addFooterView(footerView);
//add data to listview through adapter.
mListViewIssues.setAdapter(new IssueInfoAdapter(this, creator.queryAll()));
}
**Note that I removed the code for 'creator' as it wasn't necessary.
This app runs and displays the data but it doesn't display "DBVersion = 3" in the footer like I want.
Any ideas?
this line:
tvFooter = (TextView)findViewById(R.id.footerText);
requires that the view you are finding be "inside" your applications parent layout already.
try moving the findViewById(), and the setText() to after this line:
mListViewIssues.addFooterView(footerView);
Or better yet (and this might actually be neccessary, I am not certain it will work the other way) change it so that you are calling findViewById on one of the views in between the tvFooter and your parent view i.e. the ListView, or the plain View after you inflate it. Like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.footer_layout);
//Context context = getBaseContext();
tvFooter = (TextView)findViewById(R.id.footerText);
//Load listview control.
setContentView(R.layout.main);
final Context context = getBaseContext();
mListViewIssues = (ListView)findViewById(R.id.listIssues);
int DBVersion = 3;
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
mListViewIssues.addFooterView(footerView);
tvFooter = (TextView)footerView.findViewById(R.id.footerText);
tvFooter.setText("DBVersion = " + String.valueOf(DBVersion));
//add data to listview through adapter.
mListViewIssues.setAdapter(new IssueInfoAdapter(this, creator.queryAll()));
}
note the use of footerView.findViewById(), I think you might have better luck if you explicitly call it on the footerView object rather than just from your activity, which is essentially the same as calling it on your activities parent layout.
You shouldn't set footer_layout as activity layout. The right code looks like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Load listview control.
setContentView(R.layout.main);
final Context context = getBaseContext();
mListViewIssues = (ListView)findViewById(R.id.listIssues);
int DBVersion = 3;
View footerView =((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
tvFooter = (TextView)footerView.findViewById(R.id.footerText);
tvFooter.setText("DBVersion = " + String.valueOf(DBVersion));
mListViewIssues.addFooterView(footerView);
//add data to listview through adapter.
mListViewIssues.setAdapter(new IssueInfoAdapter(this, creator.queryAll()));
}
I'm new to android platform , I'd like to settext using textviews , I tried to write set text into two textviews but its just draws one textview why?
I can't draw two textviews
TextView tv1;
TextView tv2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
tv1 = new TextView(this);
tv2 = new TextView(this);
tv1.setText("Hello");
tv2.setText("How are you?");
}
On Android, the user interface normally should be created using XML-files, instead of Java code. You should read up on the tutorials on android.com, especially:
http://developer.android.com/guide/topics/ui/declaring-layout.html
An example:
In your res/layout/main.xml, you define the text TextView's:
<?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/TextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 1"/>
<TextView android:id="#+id/TextView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 2"/>
</LinearLayout>
Then if you use setContentView in the activity to display this, the app will show to TextView's:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
}
If you want to programmatically set the text in the Activity, just use findViewById():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
((TextView)this.findViewById(R.id.TextView1)).setText("Setting text of TextView1");
}
I definitely second TuomasR's suggestion to use XML layouts. However, if you're wanting to add new TextViews dynamically (i.e. you don't know how many you will need until runtime), you need to do a couple of other steps to what you are doing:
First, define your LinearLayout in main.xml (it's just easier that way than LayoutParams, IMO):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_linear_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
Now, you can go to your code, and try the following:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//This inflates your XML file to the view to be displayed.
//Nothing exists on-screen at this point
setContentView(R.layout.main);
//This finds the LinearLayout in main.xml that you gave an ID to
LinearLayout layout = (LinearLayout)findViewById(R.id.my_linear_layout);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
t1.setText("Hello.");
t2.setText("How are you?");
//Here, you have to add these TextViews to the LinearLayout
layout.addView(t1);
layout.addView(t2);
//Both TextViews should display at this point
}
Again, if you know ahead of time how many views that you need, USE XML.