• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps button in second xml

I cant remember the code as i'm new at it.. but basically, if you know how to do an onlick event from the first button, use the same code, but for the 2nd button, then you can use setContentView(R.layout.xml_name); I *think*.

I'm new at java so I may not be quite correct, maybe post your current code for your first button.
 
Upvote 0
I think when you create a new xml, the controls on it get an id and that is automatically added to the resource file (R), so you can talk to the button (so to speak) from anywhere in the code as far as I know... when you are using your button1 code on the first layout, are you referencing it using findViewById(R.something_here) ?

Like

Button myButton = (Button)findViewById(R.id.Button01); for example?

My code may be a bit wrong im afraid , but if your button was called 'Button645' and on a different xml layout, I think you can still use it in the same way, so change 01 to 645 and it should pick it up.

As I say, I could be wrong, i've only been looking at it for a week!

P.S, I experimented with this stuff in the main onCreate class.
 
Upvote 0
I have tried it several times but it does not seem to work. The app will not load and it force closes. There are no errors in the code though. So I'm confused. It seems like it should work. This is my 119th button and it repeats in more than a hundred screens. So it is a reoccurring button if that makes a difference. Thanks for the help so far.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones