I'm working on my first android app and have been trying to make a header consisting of three image buttons for back, home, and next. I was wondering if it's possible to make the header in a separate XML file, and then use include so that I may cut down on redundancy and reuse the header with multiple activities. I've been able to do this so that the navigation buttons display, but have not found a way to make the buttons navigate between activities. Thanks!
You can use an intent to navigate across activities on button pressed. First, in the xml drag and drop a button. Then initialize this button in your activity.
Eg.
Button BackBtn=(Button) findViewById(R.id.button1);
Once you have got reference to the button you can set onclick listener to this button and onclick of it, you can use to below code to navigate across activities.
Intent i =new Intent();
i.setClass(fromClass,ToClass.class);
startActivity(i);
I hope the above code would have helped.
Regarding the second part of the question, although you can define a seperate xml containing 3 buttons for navigation, you need to redefine its functionality in every activity you use them in. So instead i suggest you to create a sub-layout inside your current layout and in that add three buttons. From next activity onwards, you can copy paste that layout in your new xml.
Related
I know it should be in the style sheet area for this, but when I created a new Android app using Android Studio, it has a user logo and user information in a green box. This is what I want to edit.
First it should ask them to login if first time using the app.
But let's deal with one thing at a time, and first is me getting use to where Google puts things.
If you look at your layout activiy you will find a "NavigationView", there are 2 attributes there you need "header" and "menu".
You can see in this answer how.to get the views inside the "header" file: https://stackoverflow.com/a/33631797/4017501
There is another alternative. The "NavigationView" is a ViewGroup. So you can use it as such. Delete thee "header" and "menu" attribute and add the closing brackets as if were a ViewGroup:
<NavigationView></NavigationView>
Now you can simply put a fragment inside and then find every view and handle your logic from the fragment, is a more direct and customizable approach.
I am developing a game in Android. The game is implemented in the MainActivity.java and uses Relative Layout in xml. I want to implement a pause button, which when clicked opens a pop-up window with buttons for options like "1 player","2 Player","Change skin",'Challenge Friend" etc.
1.How to create a layout at runtime with the buttons and can we destroy the layout when an option is clicked.
2.Can we get the id of the option button clicked in the dynamically created layout, to the MainActivity, so that the corresponding function can be called.
3.when dynamically creating the layout, will the values of the already existing layout be reset?
4.Is dynamically creating layout and destroying it very expensive?
I am new to android and so would really appreciate some help...Thankyou!!!
Sounds like you just need an alert dialog with three buttons? Use the onlick functions in this answer. If you need an image view in the pop up see this answer and use similar tactics.
If you want a custom dialog with custom layout , use DialogFragment : http://developer.android.com/reference/android/app/DialogFragment.html
I m new to android but familiar with .net.
I want to add 5 menu buttons in header tht will be repeated on every page. So I'm including headertemplet.xml in every page.
But how can I write single button clicks for all these buttons.
How should I proceed?
Try this as you have created one headertemplet.xml file use include tag to add that xml file where ever you want.
<include layout="#layout/Your_XML_File_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
It might be an overkill, but you could use Fragments to do this.
The idea is a Fragment to handle the layout and clicks of the five buttons (passing the clicks to the Activity to handle it properly if needed), and another fragment to handle the rest of the screen.
As you said, the buttons will be present on all pages, so you only need to change the second fragment using FragmentTransaction.
You cant do with single on click event because onClick Require Context which is seperate for each activity.
BTW you can include it in each of your layout (which might you have done) so Rest of Operation(from finding ID to your last Result) you have to do in each activity
My application requires 2 screens and for this I have created two different XML layout files using RelativeLayout. One layout file loads whenever I run my Activity. Now I want to load the second layout on to the same Activity , when user click on a button in OptionsMenu and also when user press Back button the first screen loads instead of exiting the application. So that i don't need to create another Intent in my application.
Ideally there should be two different activities present in your application.
You can add or remove a view component in a view but if you are looking for two completely different screens then i would suggest you to go for a new activity.
I dint get what you meant by "and also when user press Bakc button the first screen loads instead of exiting the application"
If you dont want to show the first screen just finish() the activity.
Did you try re-calling the setContentView?
Or you may prefer using the ViewFlipper,
Good example here.
I would suggest a rather simpler means.
Put both your layouts in the single XML and show/gone them appropriately as need be. I dont think u need anything more complicated :-)
On my application I'm developing, the main.xml layout (the default layout of my app) has a few buttons that have been assigned onClickListeners (not the implementation way).
One of those buttons I want to have the ability to take you to another view. On the other view (preview.xml), there's another button that takes you back to the main.xml view.
I've used setContentView in the onClickListeners of those buttons and this works fine so far, but after you click the button that takes you back to main.xml, the buttons on main.xml have lost their onClick functionalities.
How can I get this to work right? I presume using setContentView isn't the right way to do this.
Your best bet, at Konstantin says above is to use Activities, as you will come across these a lot whilst developing for android. you can read about them here Activities. I assume you want to pass something onto the preview.xml page? If so, I'd recommend either putting it as an extra in the Intent used to start the activity (see the link) or creating a static reference in the activity (which you set before you launch it).
I'd say use two different activities and switch between them. Another option can be ViewSwitcher.