Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6170349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:01:16+00:00 2026-05-23T23:01:16+00:00

Is it possible to generate a main.xml inside onCreate()? Such as: package avm.project; import

  • 0

Is it possible to generate a main.xml inside onCreate()? Such as:

package avm.project;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Field;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;


public class AVMOrderSystemActivity extends Activity { 
      ViewFlipper flipper;


      @Override 
      public void onCreate(Bundle icicle) { 
        super.onCreate(icicle);

        //File[] listOfFiles = folder.listFiles();

        /*for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                Log.i("XML Generator","File " + listOfFiles[i].getName());
            } else if (listOfFiles[i].isDirectory()) {
                Log.i("XML Generator","Directory " + listOfFiles[i].getName());
            }
        }*/
        String xml="";
        xml=xml+"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

        xml=xml+"<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"+
                "android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\"\n"+
                "android:orientation=\"horizontal\" android:padding= \"5px\">\n";
        xml=xml+"<ViewFlipper android:id=\"@+id/adflipper\"\n"+
                "android:inAnimation=\"@android:anim/fade_in\" android:outAnimation=\"@android:anim/slide_out_right\"\n"+
                "android:paddingLeft=\"15px\" android:layout_width=\"fill_parent\"\n"+
                "android:layout_height=\"fill_parent\" android:autoStart=\"true\"\n"+
                "android:flipInterval=\"5000\">";

        Resources a=this.getResources();
        try
        {
            for (int i=0x7f020000 ;i<0x7f020020;i++)
            {
                String name=a.getResourceName(i);
                xml=xml+"<ImageView android:layout_width=\"fill_parent\" android:src=\"@"+name.substring(name.indexOf(':')+1)+"\"\n"+
                    "android:layout_height=\"fill_parent\"></ImageView>\n";
            }
        }
        catch(Exception e)
        {
            Log.i("resoruces","exception");
            e.printStackTrace();
        }
        xml=xml+"</ViewFlipper>\n</RelativeLayout>";
        xml=xml+"<!--xxx-->";


        try{
            // Create file 
            FileWriter fstream = new FileWriter("res/layout/main.xml");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(xml);
            //Close the output stream
            out.close();
        }catch (Exception e){//Catch exception if any
            Log.i("xml write","exception");
            e.printStackTrace();
        }

        setContentView(R.layout.main); 

        flipper=(ViewFlipper)findViewById(R.id.adflipper); 
        Button btn=(Button)findViewById(R.id.restbutton);


        btn.setOnClickListener(new View.OnClickListener() { 
          public void onClick(View view) { 
            flipper.showNext();
          } 
        }); 
      } 
    }

I have implemented the code and it can get the project drawables to generate a main.xml accordingly but its not working (I got an exception: file not found res/layout/main.xml). (My aim is to generate main.xml according to varying number of png images) What am I doing wrong, Is it possible to get over this or Is there any other solution?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:01:17+00:00Added an answer on May 23, 2026 at 11:01 pm

    You can change the src of your ImageView dynamically without having to generate the XML.

    ImageView imageView = (ImageView) findViewById(R.id.your_image_view);
    imageView.setImageDrawable(getResources().getDrawable(R.drawable.your_png));
    

    And you can add more by creating them dynamically and adding them to the parent RelativeLayout:

    for (int i=0x7f020000 ;i<0x7f020020;i++)
        ImageView imageView = new ImageView(this);
        imageview.setBackgroundResource(R.drawable.your_png_reference);
        RelativeLayout ll = (RelativeLayout) findViewById(R.id.your_relative_layout_id);
        ll.addView(imageView);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to generate a set of wiki pages from XML comment file
Is it possible to generate a XML Schema of a Database programatically with .Net
Is it possible to generate a file in javascript and then prompt the user
A library that my java application uses needs looks for a file (log4j.xml) in
I have a main pom.xml which has multiple modules. These modules need to generate
See the following Maven generated project target classes test-classes src main java scripts resources
Is it possible to generate a maven stub project via mvn archetype:generate that has
Back in the old days, Help was not trivial but possible: generate some funky
Is it possible to generate PDF Documents in an Adobe AIR application without resorting
Is it possible to generate a list of all source members within an iSeries

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.