Trending
- Can We Build Elite Search Agents Without Massive Industrial RL Pipelines?
- Google Cloud AI Agents With Gemini 3: Building Multi-Agent Systems That Actually Work
- Mastering Fluent Bit: Beginners' Guide for Contributing to our CNCF Project Docs
- Combining Temporal and Kafka for Resilient Distributed Systems
- DZone
- Testing, Deployment, and Maintenance
- Deployment
- Creating Android UI Programmatically
Creating Android UI Programmatically
Join the DZone community and get the full member experience.
Join For Free
So far, in all my examples, I have been
using the declarative way of creating an Android UI using XML. However,
there
could arise certain situations when you may have to create UI
programmatically.
Sincere advice would be to avoid such a design since android has a
wonderful
architecture where the UI and the program are well separated. However,
for
those few exceptional cases where we may need too… here is how we do it.
Every single view or viewgroup
element has an equivalent java class in the SDK. The structure and
naming of
the classes and methods is very similar to the XML vocabulary that we
are used
to so far.
Let us start with a LinearLayout. How would we declare it in an XML?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
This just contains a TextView embedded in a LinearLayout. A very trivial example. But serves the purpose intended. Let me show how almost every single element here corresponds to a class or a method call in the class. So the equivalent code in the onCreate(…) method of an activity would be like this:
super.onCreate(savedInstanceState);
lLayout = new LinearLayout(this);
lLayout.setOrientation(LinearLayout.VERTICAL);
//-1(LayoutParams.MATCH_PARENT) is fill_parent or match_parent since API level 8
//-2(LayoutParams.WRAP_CONTENT) is wrap_content
lLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
tView = new TextView(this);
tView.setText("Hello, This is a view created programmatically! " +
"You CANNOT change me that easily :-)");
tView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
lLayout.addView(tView);
setContentView(lLayout);
Like this any layout view can be created.
But from this small example you can notice two outstanding things – very
tedious to code for every attribute of the view. And any simple change
in the
view, you need to change the code, compile, deploy and only then you see
the
effect of the change – unlike in a layout editor.
You can download the sample code here.
Android (robot)
Published at DZone with permission of Sai Geetha M N. See the original article here.
Opinions expressed by DZone contributors are their own.
Related
-
Pragmatic Paths to On-Device AI on Android with ML Kit
-
Deep Linking in Enterprise Android Apps: A Real-World, Scalable Approach
-
Diving into JNI: My Messy Adventures With C++ in Android
-
Elevate Customer Engagement by Adding Google Play Instant to Your Android App
Partner Resources
×
