Initializing View in fragment - java

Ok so my problem is that I have included a layout in my fragment view using <include.../> tag but whenever I try to initialize it throws a null pointer exception error
<include layout="#layout/pro_ocr" />
I tried a couple of methods to findViewById() like:
v.findViewById(R.id.crossProOCR);
getActivity.findViewById(R.id.crossProOCR);
but it didnt work a solution would be great help now

I'm guessing you are finding the id of the parent layout inside pro_ocr, hence you are getting an NPE.
An include is used like any other view inside your layout.
You need to give the include tag an id:
<include
android:id="#+id/proOcr"
layout="#layout/pro_ocr" />
Then you can use it as View in your Fragment.
Getting the ids of the views inside the included layout is similar to any other view.

Related

How to dynamically change a sublayout which was assigned using <include/> view in android studio?

I am using < include /> to show a sublayout (layoutA) inside a Main layout.
however, what I want to do is to change the sublayout from layoutA to layoutB dynamically in Java. Which means in <include../> the line layout="#layout/layoutA" should change to layout="#layout/layoutB".I am not sure how to achieve this. Not sure if there is an option like view.setLayout() for .
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
<other data/>
<include
android:id="#+id/id1"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/layoutA"
/>
</LinearLayout>
Note: I have about 7-8 sublayouts so I do not just want to create multiple and hide them. Which could be an option.
you can replace include with fragment and keep replace fragment if subviews have different and complex logic if not or simple stuff you can use include and remove all views to remove old one and add a new view
ViewGroup parentLayout = findViewById(R.id.id1);
parentLayout.removeAllViews();
LayoutInflater inflater = LayoutInflater.from(this);
View newSublayout = inflater.inflate(R.layout.layoutB, parentLayout, false);
parentLayout.addView(newSublayout);

Android onClick() on include view won't let binding

I have a separate layout that I want to call onClick and update a field on callback
<include
android:onClick="#{() -> viewModel.changeItem(2)}"
layout="#layout/item"
app:attr="#{viewModel.title}"
app:desc="#{viewModel.description}"
app:active="#{viewModel.isSelected}"
/>
But it returns the following binding error:
Cannot find the setter for attribute 'android:onClick' with parameter
type lambda on com.X.databinding.ItemBinding.
But I can binding on other views
<TextView
android:onClick="#{() -> viewModel.changeItem(1)}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
What I suggest you is to call the onClick directly from code.
Setup an ID for your view in your .xml, then set something like this :
val item = findViewById(R.id.your_id) etc.
item.onClick { functionYouWantToCall() }
However, I do not know if this is possible to set an ID or an onClickListener on an include layout.
If you can not do it, simply put your include inside a LinearLayout, then set your onClick on it.
I used to set onClicks in the .xml too, but I think it is much more efficient to set it in the code.
Moreover, I do not know if you can set arguments in a function you call from the xml.

Could Resource xml can be called from layout.xml?

i just come trough a doubt
We had rexource XML and Layout
we can call another layout.xml by this way
<include layout="#layout/content_main" />
in same way can we call #xml ?
Just a doubt
and I know we can so it from code java by (R.XML.ook);
Is there any other way from layout ?
Thank you
It's impossible. I'm wondering what is your purpose when you want to call #xml in layout file?
Layout file can only include View and ViewGroup. So only view tag (TextView, Button,...) or layout tag (include, merge tag) or ViewStub are available for using in layout file. xml file can have many invalid tags, so it can't be use in layout file.

Facebook LikeView change button size

I am using a LikeView in my android application and want to change the button size. Changing the view's size using layout parameters or the width/height at addView() does not help.
I'm using libGDX and have never worked with XML + libGDX, so a way to do it with code only would be nice!
Does anyone know how to do this? Currently I use the following code to create my LikeView:
likeButton = new LikeView(application);
likeButton.setLikeViewStyle(LikeView.Style.BUTTON);
likeButton.setX(x);
likeButton.setY(y);
likeButton.setObjectId(LIKE_URL);
likeButton.setVisibility(View.GONE);
application.layout.addView(likeButton,width,height);
likeButton.invalidate();
application.layout is a RelativeLayout
Try to create your LikeView from XML (or inflate this layout at runtime):
<com.facebook.widget.LikeView
android:id="#+id/button_like"
android:layout_height="31dp"
android:layout_width="wrap_content" />
I finally fixed it using setScale on the view. It's not the most elegant solution, but it does the job.
likeButton.setScaleX(1.8f);
likeButton.setScaleY(1.8f);

How to have a header in every layout page?

I want that this code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView android:id="#+id/logo" style="#style/logo"
android:layout_alignParentTop="true"
android:text="#string/logo" tools:context=".MainActivity" />
to start in every layout. How can I do that I wouldn't need to add this to every layout page? Like for example in PHP I would use <?=include("header.php");?> (just an example, actually it's bad practise, doesn't matter here). Thank you.
Save your header in XML file and then include this XML as child layout in other layouts:
<include layout="#layout/headerlayout" android:id="#+id/headerLayoutid" ... />
The headerlayout.xml is name of your above layout that should be defined in res/layout and you like that be shown as header in all layouts(layout="#layout/headerlayout"),also headerLayoutid is id of your headerlayout(in it's parent) and you can reference to it in your parent layouts or in your code.
You can override all the layout parameters. This means that any android:layout_* attribute can be used with the tag. Here is an example:
<include android:layout_width="fill_parent" layout="#layout/image_holder" />
You can see more details in about include in this page.
Edit:
If you have problems in about finding views in included layout,see this questions,I hope these help you:
findViewById not working for an include?
Finding an view by id?
The <include> tag is what you want. Read about it in Re-using Layouts with <include/>
You can use the <include .../> tag in your layout to reuse your header where is needed. See here for an example.

Categories

Resources