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);
Related
I want to have 3 layouts in my ScrollView but when I add them it actually doesn't scroll. I tried to put all layouts in different files, then include them, tried to put them in ListView and it doesn't work too. In this option, when I put in ScrollView the LinearLayout and there include the rest of the layouts the application show nothing. That's probably becouse I don't know how to refer to that nested layouts... This is my XML:
activity_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="match_parent"
android:layout_height="wrap_content"
>
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include layout="#layout/first"/>
<include layout="#layout/second"/>
<include layout="#layout/third"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
#EDIT views included (all are the same so I put just one):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</RelativeLayout>
And in the main activity I put the objects into layouts like this:
setContentView(R.layout.activity_main);
RelativeLayout first = (RelativeLayout) findViewById(R.id.first);
RelativeLayout second = (RelativeLayout) findViewById(R.id.second);
RelativeLayout third = (RelativeLayout) findViewById(R.id.third);
...some code creating views...
first.addView(myView1);
second.addView(myView2);
third.addView(myView3);
Need to put fix size in included layout, for example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
I don`t understand why do you need to add view manually when you already added it to the layout.
You should also eliminate top Linear Layout since there is no use of it.
Does anybody know some tool which convert layout.xml into Java Code generated programmatically. Simple example:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/redownload_of_assets"
android:layout_gravity="center"
android:id="#+id/btn_redownload_of_assets" />
Button button = New Button(this)
button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Reason of that solution is that I need add some controls into FrameLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fr_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/slider_menu"/>
</android.support.v4.widget.DrawerLayout>
Try this service http://www.xmltojava.com/ it does not support custom view's but still can be useful. Any way i strongly recommend use plain xml. If you need to change layout dynamically there is two options.
Use ViewStub
Add all view's you need to layout and hide/show it when you need.
All xml tags are actually classes which you just inflate. Just give the layout and id, access it via the java and add whatever methods you want.
I am trying to design a 3D page curl.
In my mainActivity, I have a viewPager which contains pages to be curled. For each page I have a separate layout file.
In the layout file if i just add a text view like below it is showing up fine:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/PageTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green"
android:text="VIEW 1" /> -->
But if I wrap the textview inside relative layout, it just shows a white blank screen and textview is not shown.
<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"
tools:context=".DemoActivity" >
<TextView
style="#style/PageTitle"
android:id="#+id/sampletextview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="VIEW 1" />
</RelativeLayout>
Can someone please help me to know why i am not able to use relative layout in my view layout file:
I inflate the view like this:
LayoutInflater inflater = (LayoutInflater) myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view1 = inflater.inflate(mViewIds[0], null);
Try changing the second parameter of the inflate() call from null to the ViewGroup that view1 is added to. Or post the code where you add it (and include the part where you define the LayoutParams).
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.
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.