I have a TextView I use as the headline of my menu page:
<TextView
android:id="#+id/menuTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu"
android:textColor="#color/white"
android:textSize="25sp"
android:textStyle="bold" />
Now I need a TextView with the same color, size and style on every sub menu in my app. Instead of copy pasting the whole TextView to every layout and just change the text in each one I thought I'd make one layout with the TextView and include it in every sub menu view, only overriding the text.
My code looks like this:
/layout/menutextview.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menuTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/default"
android:textColor="#color/white"
android:textSize="25sp"
android:textStyle="bold" />
The includes in each layout xml file tries to override the text attribute:
<include layout="#layout/menutextview" android:text="#string/menu" />
<include layout="#layout/menutextview" android:text="#string/settings" />
But the default text is displayed everywhere. Anyone have an idéa of what the problem might be?
Regards,
Mattias
Include cannot be used to "overrride" children properties. It doesn't know which type of layout you will include, it will only inflate it and add it to the current layout.
To dynamically change the text, you need to do it in code.
final TextView textView1 = (TextView) findViewById(R.id.menuTextView);
textView1.setText(R.string.menu);
final TextView textView2 = (TextView) findViewById(R.id.settingsTextView);
textView2.setText(R.string.settings);
Try using styles and have the TextView implement that style. This will make it easier to maintain consistency in your Views.
You can achieve this with DataBinding. First you define a variable in your child layout:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="buttonText"
type="String" />
</data>
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{buttonText}"/>
</layout>
Then you set it in the other layout file where you include it:
<!-- .... other views -->
<include
layout="#layout/inc_icon_button"
bind:buttonText="#{`Put your String here`}" />
<!-- .... other views -->
Best case you would also have a variable in your parent layout to then just forward the binding.
You could use the following solution:
Give the include tag and the TextView in the include layout a specific id (like "section")
Declare the include tag as a view and a TextView in your code View section; and TextView textview;
Bind the View with the id of your include section = findViewById(R.id.section);
Bind the TextView of your include with View.findViewById();
textview = section.findViewById(R.id.textview);
I used the information from this side.
Related
I'm trying to dynamically set the text of a button that I'm inflating at runtime, but the text isn't being set.
I'm creating a custom Dialog for which I set SetContentView with a View that I inflate at runtime. The view contains a single Button, e.g:
var dialog = new SetUnsetDialog();
var view = View.Inflate(_context, Resource.Layout.view_set_unset_buttons, null);
view.FindViewById<Button>(Resource.Id.btnPartsetA).Text = _blockDescriptions["partseta"];
dialog.SetContentView(view);
dialog.SetTitle("Set/Unset " + _area);
dialog.Show();
The layout is defined as:
<?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="wrap_content"
android:orientation="vertical">
<!-- irrelevant stuff removed -->
<LinearLayout
android:id="#+id/linearLayoutSetUnsetButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<!-- irrelevant stuff removed -->
<Button
android:id="#+id/btnPartsetA"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:drawableTop="#drawable/partset_a" />
<!-- irrelevant stuff removed -->
</LinearLayout>
</LinearLayout>
Why is the button text not being set? What do I need to do to set it?
UPDATE:
It turns out this is a non-issue. The content view gets reset later and overwrites the values that I set.
what about using the setText method instead of .Text?
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);
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.
Assume we have a layout contains a TextView:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/text1"
style="#style/text_style"/>
</LinearLayout>
and then include this layout several times:
<include android:id="#+id/info1" layout="#layout/myLayout" />
<include android:id="#+id/info2" layout="#layout/myLayout" />
<include android:id="#+id/info3" layout="#layout/myLayout" />
Is it possible to assign text into each TextView in the xml file which contains these layouts?
If not, then how to assign in runtime?
You can, For this you need to identify you layout
LinearLayout info1 = (LinearLayout)findViewById(R.id.info1);
Then with this layout object you need to identify you TextView
TextView text1 = (TextView)info1.findViewById(R.id.text1);
text1.setText("Your text");
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.