Problem with LayoutInflator to set TextView Android - 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.

Related

Inflating RelativeLayout within another RelativeLayout

I'm trying to contain the layout model for a custom list view in a single custom list view layout.xml file and inflate that using an custom adaptor class using kotlin.
The data seems to come through fine and the row formatting is accurate with the exception of the header being repeated for each row. I'm guessing I could just move the header out of the list view layout.xml file but I'd prefer to keep that self contained so it could be re-used.
This is the layout file in question:
<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" >
<!-- Header aligned to top -->
<RelativeLayout
android:id="#+id/estimated_ftp_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorHeader"
android:gravity="start" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="#string/estimated_ftp"
android:textColor="#000"
android:textSize="20sp" />
</RelativeLayout>
<!-- Content below header -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_below="#id/estimated_ftp_header"
android:orientation="horizontal"
android:padding="6dip"
android:id="#+id/estimated_ftp_row">
<ImageView
android:contentDescription="#string/ftp_type"
android:id="#+id/ftp_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/centerPoint"
android:gravity = "center|end|end"
android:src="#drawable/glyphicons_13_heart"/>
<TextView
android:id="#+id/centerPoint"
android:text=""
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/ftp_value"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/centerPoint"
android:gravity = "center|start|start"
android:text="141 bpm"/>
</RelativeLayout>
I believe the solution would be to figure out how to select the estimated_ftp_row id and inflate it instead of the the whole estimated_fto.xml file.
Here is the code in the adapter that selects the layout before inflating within the getView method:
val inflater = activity?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
view = inflater.inflate(R.layout.estimated_ftp, null)
viewHolder = ViewHolder(view)
view?.tag = viewHolder
How would I change the inflater.inflate to inflate something within R.layout.estimated_ftp?
FWIW, the activity's XML layout has a ListView control in the location used for this as a placeholder. It doesn't reference the custom layout file. That connection is done via the adapter class noted above. Not sure that's a problem or not so I wanted to mention it...
Thanks in advance for any suggestions.
mike
Solution Update:
https://stackoverflow.com/users/2850651/mike-m had the guidance I needed. I was able to separate the row format from the header format XML layouts. The Adapter was unchanged and the Activity has new code to add the header if content is found for the list in question.
val headerView = layoutInflater.inflate(R.layout.estimated_ftp_head, null)
estimated_ftp_list.addHeaderView(headerView)
Solution:
https://stackoverflow.com/users/2850651/mike-m had the guidance I needed. I was able to separate the row format from the header format XML layouts. The Adapter was unchanged and the Activity has new code to add the header if content is found for the list in question.
val headerView = layoutInflater.inflate(R.layout.estimated_ftp_head, null)
estimated_ftp_list.addHeaderView(headerView)

TextView can scroll but scrollbar is not shown

I have a TabHost with several tabs, which content is defined as a FrameLayout, which wraps a TextView, each one of them having a lot of text, more than it can be shown within the screen layout, so I had to enable vertical scrolling for every tab.
The main thing is that those tabs are created dynamically (programatically), so I have to specify all the options this way:
final SoftReference<TabHost> th = new SoftReference<TabHost>((TabHost) ((Activity) globvars.getContext()).findViewById(android.R.id.tabhost));
final TabSpec setContent = th.get().newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
view.setBackground(globvars.getContext().getResources().getDrawable(R.drawable.rounded_edges));
view.setPadding(25, 25, 25, 25);
view.setTextColor(Color.WHITE);
view.setLines(50);
view.setMaxLines(maxLines);
view.setEllipsize(TextUtils.TruncateAt.START);
view.setHorizontalScrollBarEnabled(false);
view.setVerticalScrollBarEnabled(true);
view.setMovementMethod(new ScrollingMovementMethod());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
view.setVerticalFadingEdgeEnabled(true);
view.setGravity(Gravity.BOTTOM);
view.setOverScrollMode(1);
view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
view.setTypeface(Typeface.MONOSPACE);
return view;
}
});
Using this approach, I can scroll backwards and forwards indeed, but visually the scrollbar is not shown. I'm mean this bar:
Am I missing something? Does the scrollbar have to be defined by a customized drawable by imperative?
Thanks!
------ EDIT (12-31-2013) ------
I've been looking around and still can't find any solution to this. I've tried as many combinations of parameters as I was able to find, but no result. Particularly, I've tried this and also wrapping the FrameLayout within a ScrollView, but instead of showing a scrollbar at the TextView itself, the whole TextView is wrapped within a scrollbar and grows when buffer gets bigger and bigger, that's not what I want.
Any help appreciated!
------ EDIT (01-17-2014) ------
Well, at this point, I can assure I've tried any logical step (well, at least to me) to make this work and still can't! I clarify that I have about 7-8 additional activities and I have no trouble with scrolling in any of them. I just work with TabHost in this one, though. So I'm starting a bounty on this, because that's already endangering my mental sanity.
I'm posting my layout below:
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/fondo_imagen"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/TabContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="99"
android:orientation="vertical">
<TabHost
android:id="#+android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/TabLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Horizontal scrolling for the tab widget -->
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget
android:id="#+android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
android:id="#+android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp" />
</LinearLayout>
</TabHost>
</LinearLayout>
<!-- Some additional LinearLayouts that don't have anything to do with the tab widget -->
...
</LinearLayout>
------ EDIT (01-19-2014) ------
Ok, based on corsair992's answer, I could finally get this working. My real mistake was assuming that even the method that creates the tab (posted above) receives a TextView as parameter, working with a View in the TabSpec definition would be casting it to the TextView. So indeed, I wasn't aware I was actually setting the scrollbars on a View (didn't know the View class didn't provide a public programatic method to configure scrollbars neither), so I followed corsair992's advice and created a separate layout with this content:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tabsContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:background="#drawable/rounded_edges"
android:gravity="bottom"
android:padding="8dp"
android:scrollHorizontally="false"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical"
android:textColor="#FFFFFF"
android:textSize="11sp"
android:typeface="monospace"
tools:ignore="SmallSp" />
So now, instead of calling the above method which sets all those attributes to the View, I simply inflate this layout and set the MovementMethod:
final TabSpec setContent = th.get().newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
// tabs_content is the layout above
final View ll_view = LayoutInflater.from(globvars.getContext()).inflate(R.layout.tabs_content, null);
final TextView view = (TextView) ll_view.findViewById(R.id.tabsContent);
view.setMovementMethod(new ScrollingMovementMethod());
return ll_view;
}
});
It appears that the base View class does not provide a public method for initializing scroll bars if they are not specifically enabled in an XML layout resource. However, there is no reason you can't define your tab content in an XML resource, enable vertical scroll bars by setting the android:scrollbars attribute to vertical, and inflate it from the TabContentFactory dynamically.
Something like this in your case:
public View createTabContent(String tag) {
Activity activity = (Activity) globvars.getContext();
TextView view = (TextView) LayoutInflater.from(activity).inflate(R.layout.mytabcontent,
(ViewGroup) activity.findViewById(android.R.id.tabcontent), false);
view.setMovementMethod(new ScrollingMovementMethod());
view.setText("My Text");
return view;
}

Set text of included view

I'm wondering if this is possible:
In a layout file, I've included a view:
<?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="vertical" >
<include
layout="#layout/includedView" />
</LinearLayout>
That includedView contains this:
<?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="48dip" >
<ImageView
...
/>
<TextView
....
/>
</RelativeLayout>
My question is: is it possible to set the text for the textview inside the includedView from the layout that includes the view (so from layout 1)?
Hope my question is clear, if not, then please ask.
you can do that from code as same as single layout. for example
setContentView(R.layout.first_layout);
TextView tv = (TextView)findViewById(R.id.textview_of_second_layout); // just like single layout
tv.setText(something);
But I think it is not possible to do this from the first layout xml as there is no visible way. (someone corrects me if I am wrong)
Yes, it is possible, you can refer to that view as the whole included layout was copied into the main layout file. You can refer to the textview using its id as allways, it will be found
If you want to set something in a included view /layout ,like in navigation drawer if you want to set text(profile name,email etc), then do it like below.
NavigationView mNavigationView = findViewById(R.id.nav_view);
View headerView = mNavigationView.getHeaderView(0);
TextView profilename = headerView.findViewById(R.id.profile_name);
TextView emailtxt = headerView.findViewById(R.id.email_txt);

service show view getLayoutInflater

I would like to show two views in a service context. One is a TextView, and the other is a class that I have developed extending the basic View class.
prompt.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mtPrompt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"
android:text="#string/demoString"
android:textColor="#android:color/white"
android:minLines="2"
android:textSize="25dp" >
</TextView>
and input.xml:
<com.xxx.ime.sssView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inputView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/translucent_gray"
android:cacheColorHint="#null" >
</com.xxx.ime.sssView>
in my onInitializeInterface() implementation I inflate them as follows:
sssView sv =
(sssView) getLayoutInflater().inflate(R.layout.input, null);
TextView tv =
(TextView) getLayoutInflater().inflate(R.layout.prompt, null);
I see the sssView, but not the TextView. Any ideas why?
This isn't quite an answer, but it is a solution. Rather than having two separate views, all I needed to do was inherit my sssView from TextView rather than view. Then I inflated the one view, and placed my text where I want it using gravity.

Not being able to add a textView to LinearLayout through code

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().

Categories

Resources