Changing XML R.layout on Run Time?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Changing XML R.layout on Run Time?

Postby Madz » Sat Sep 20, 2008 4:17 pm

Hi Anddev Community,,

Is there a way to change the XML R.layout in Runtime?
The idea is if a user select a text in a list that text will be the name of the new pre-defined XML layout.


ie. R.layout.(//Text from List))

Thanks
Madz
Freshman
Freshman
 
Posts: 3
Joined: Mon Sep 15, 2008 12:42 pm

Top

Postby Couchtyp » Sat Sep 20, 2008 6:48 pm

Everything is possible...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private void setContentViewByName(String layoutName) {
  3.  
  4.         try {
  5.  
  6.                 int layout = (Integer) R.layout.class.getDeclaredField(layoutName).get(null);
  7.  
  8.                 setContentView(layout);
  9.  
  10.         }
  11.  
  12.         catch (Throwable t) {
  13.  
  14.                 // Error getting specified layout
  15.  
  16.                 // TODO Handle exception
  17.  
  18.         }
  19.  
  20. }
  21.  
  22.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


But, as you know what layouts are defined in your XML files there are more elegant (and a lot faster) ways to solve your problem. ;)
Couchtyp
Freshman
Freshman
 
Posts: 2
Joined: Thu Sep 18, 2008 3:54 pm

Postby Madz » Sat Sep 20, 2008 7:20 pm

@ Couchtyp

Thank you very much.

You have better solution? Please enlighten me on how will I this in a more elegant way.

Thanks
Madz
Freshman
Freshman
 
Posts: 3
Joined: Mon Sep 15, 2008 12:42 pm

Postby ninor » Sat Sep 20, 2008 11:03 pm

Madz wrote:@ Couchtyp

Thank you very much.

You have better solution? Please enlighten me on how will I this in a more elegant way.

Thanks


You can do it the "java" way, by coding the UI inside the .java file and not in the XML.
Then you could control how it would display under certain conditions/parameters.
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests