Adding views in Android from Java source - android

I'm working on an app that requires the user to either increase or decrease the number of edit texts during application run-time (eg, in the default Android contacts app).
Someone please assist with some Java source code

Assume we have a container defined in an xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Now we can do this:
ViewGroup container = (ViewGroup) findViewById(R.id.container);
EditText editText = new EditText(this);
container.addView(editText);
Note: The container doesn't even have to be defined via xml. We can also create the container dynamically and use setContentView. It also does not have to be the root ViewGroup of your layout.

Related

ViewGroup.findViewById() returns null because the view has a different id at runtime

In the onCreateView() method of a fragment, I am trying to initialize a CustomView with behavior similar to a fab button.
Upon clicking on this custom view an animation is supposed to run which utilizes an empty RelativeLayout which is present in the parent ViewGroup in the Fragment.
I am passing this ViewGroup to the CustomView in a constructor.
When I try to create a reference to the empty RelativeLayout by calling ViewGroup.findViewById(R.id.target), the reference create holds null ultimately causing a NullPointerException upon animation
However, if I use ViewGroup.getChildAt(1) to create the reference, the animation works just fine.
By using watchers, I found that the id of the ViewGroup.getChildAt(1) is different from the R.id.target which has been assigned to it via xml.
What sorcery is this?
I have browsed already for a few days to be able to find a question which describes this situation.
Extra Information:
We recently switched from support libraries to androidX.
fragment.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/top_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout>
<!-- Other views -->
</LinearLayout>
<!-- This is the layout i am trying to instantiate -->
<RelativeLayout
android:id="#+id/target"/>
</RelativeLayout>

Adding items to the ScrollView dynamically make it doesn't scroll

I have a simple layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:padding="15dp">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/scrollLayout">
</LinearLayout>
</ScrollView>
</RelativeLayout>
Now, I inflate the outer RelativeLayout to retrieve the inner LinearLayout to put items in it.
RelativeLayout relative = (RelativeLayout) LayoutInflater.from(activity).inflate(R.layout.gradient_pick_view, null);
LinearLayout view = (LinearLayout) relative.findViewById(R.id.scrollLayout);
After that I created a method to add some buttons to it:
for(int i=0;i<10;i++){
LinearLayout wrapper = (LinearLayout) LayoutInflater.from(activity).inflate(R.layout.button_wrapper, null);
Button button = (Button)wrapper .findViewById(R.id.button);
view.addView(layout);
}
Everything works fine, but it doesn't scroll.
What am I doing wrong here?
Here's the screenshot (displaying 7 of 10 buttons):
I forgot to mention - I'm using a MaterialDialog library and add this RelativeLayout as a custom view to a dialog.
Try to set the following attribute to your scrollview,
android:fillViewport="true"
above attribute is used to make your scrollview to use entire screen of your application.
I had a false parameter passed to a customView in a MaterialDialog.
dialog = new MaterialDialog.Builder(activity)
.title(R.string.about)
.customView(view, true)
.positiveText(R.string.changing_fragments)
.show();
As doc says:
If wrapInScrollView is true, then the library will place your custom view inside of a ScrollView for you. This allows users to scroll your custom view if necessary (small screens, long content, etc.). However, there are cases when you don't want that behavior. This mostly consists of cases when you'd have a ScrollView in your custom layout, including ListViews, RecyclerViews, WebViews, GridViews, etc. The sample project contains examples of using both true and false for this parameter.
Now it's working.

Android manifest xml file

After deleting an Android xml activity file, got problem in AndroidManifest.xml file.
What can be done to cure the issue?
In an activity created an EditText field and gave it a green background. And also gave Relative layout background a custom color, but nothing happened to the layout and the text field. So decided to delete the Relative layout(not main layout) to create a fresh one. And got the problem in Android manifest xml file.
Code:
layout seekbarlayout = (RelativeLayout) findViewById(R.id.layoutwithseekbar);
seekbarlayout.setBackgroundColor(Color.parseColor(xxxxxxxx));
text1 = (EditText) findViewById(R.id.textwithseekbar);
text1.setBackgroundColor(Color.parseColor(xxxxxxx));
Well, your RelativeLayout must had EditText in it, so when you removed RelativeLayout, the EditText should be removed with it.
Structure:
main_layout.xml
|--+ RelativeLayout (layoutwithseekbar)
|--> EditText (textwithseekbar)
try making new layout set id layoutwithseekbar on it and put EditText in it
<RelativeLayout android:id="#+id/layoutwithseekbar android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText android:id="#+id/textwithseekbar" android:layout_height="wrap_content"
android:layout_width="match_parent" />
</RelativeLayout>

How to inject a custom view to a viewGroup declared in an xml at runtime?

I' ve designed a xml layout-file with some standard ui-components in it. So far it works fine.
In my Java-code I've implemented a custom view which extends a SurfaceView for the purpose of animating things. Now I want to inject this custom view at runtime to the ui defined in the layout-file.
Do I have to provide for an empty view filler for that custom view in the XML?
How does this work? Can you show me a simple code snippet please ? Thanks
Use ViewGroup addView() method.
Also you can use ViewStub
You may use
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/titlebar"/>
where, this linearLayout is in basic layout while this layout in include is another layout file. So you can even reuse this layout multiple times.
Check examples here
this way it works, but is there any more elegant way to do this, e.g. with a ViewStub ?
// the animation-view:
final LighthouseView lighthouseView = new LighthouseView(this, controller);
controller.registerView(lighthouseView);
setContentView(R.layout.activity_lighthouse);
ViewGroup container = (ViewGroup) findViewById(R.id.lighthouse_container);
container.addView(lighthouseView, 0);
setContentView(container);

How to make the custom xml layout for this android project?

I am trying make application from this project: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html
But here, there is a Layout that generating dynamicly. I want to create my own layout in xml file. So what should i have to do for it.
Please anyone can help me to make the xml layout from this dynamic layout ??
Thanks.
That example is not creating a "dynamic layout". The layout, which is the part you'd be defining in XML, consists of only one View object, MyView.
What I assume you are referring to by "dynamic layout" is the MyView class, which is a custom View object which accepts touch input and draws on the screen. This cannot be defined in XML... you must write the Java code to handle the logic necessary, since the regular View class (which MyView is extending) does not support such functionality.
What you would need to do is create a Java file defining the MyView class. Say for example, com.example.MyView. Then, in XML, you can include this custom view in your layout by referring to the full name, including the package name. For example...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<com.example.MyView>
android:layout_height="fill_parent"
android:layout_width="fill_parent"
</com.example.MyView>
</LinearLayout>
You can use this layout in an activity as usual using setContentView.

Categories

Resources