Issue
What I am trying to achieve (which I do not know if it is even doable, since I'm new to android development) is to have:
- A MainActivity class utilize/reference and XML layout (this i know is doable)
- Within the MainActivity class also call a custom Layout programmatically (I am uncertain how to do this)
To elaborate on my question
I have the following XML layout which is compromised of two items:
- A dropdown menu
- A RelativeLayout view (*** this is where I would like to link to my custom class to)
Here is the XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/weekDropdown"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:hint="@string/select_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:labelFor="@+id/weekSelected">
<AutoCompleteTextView
android:id="@+id/weekSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:layout_weight="1"
android:padding="14dp"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
<RelativeLayout <-- Here is where I would like to link my custom class
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</RelativeLayout>
</LinearLayout>
In my custom class, I build my layout/view programmatically:
public class MainActivityLayout extends RelativeLayout {
Context context;
TableLayout tableA;
TableLayout tableB;
TableLayout tableC;
TableLayout tableD;
public MainActivityLayout(Context context) {
super(context);
this.initComponents();
this.setComponentsId();
etc...
}
}
Which I call it from my MainActivity
class like so:
setContentView(new MainActivityLayout(this));
So is it possible to link the RelativeLayout in the XML to my custom class? If so can you let me know how to do so?
Solution
If you want to use your custom view, the easiest way is to include it in the xml. So this needs to be changed:
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</RelativeLayout>
into this
<myapp.mypackage.MainActivityLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</myapp.mypackage.MainActivityLayout>
Change myapp.mypackage
based on the real path of MainActitvityLayout
.
Then just use it like any other usual layouts.
setContentView(R.layout.main_activity);
Also you must override these constructors:
public MainActivityLayout(Context context)
{
super(context);
}
public MainActivityLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MainActivityLayout(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
public MainActivityLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
}
However, if you insist on using it like this:
setContentView(new MainActivityLayout(this));
You can not use the main xml layout at all, but you can still inflate xml files onto your custom view. I won't write them here, you can easily search on this site on how implementing them.
Answered By - Darkman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.