Android: fixed button in each view - android

Is there a way to make a button fix in the whole application views? I mean instead of adding the button to every xml file and code it.
Thank you.

You can keep your button xml code a different xml file. And to every other activity xml layout you can use the xml tag include like
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
<include layout="#layout/okcancelbar_button"/>
</LinearLayout>
Or like jack said, create a base activity that creates the buttons and the rest of the activities can extend from it.

you can create a custom activity in which you create one time button and every activity you will call, extended to your custom activity will have that button

Related

Get id's when click (ANDROID)

Simple question, i have this code
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
...
<LinearLayout
android:id="#+id/insideLinearLayout1"
...
<TextField>
android:text='beforeClick'
...
<Button
android:id="#+id/button1"
android:onClick="updateExpression"
...
</Linear Layout
<LinearLayout
android:id="#+id/insideLinearLayout2"
...
<TextField>
android:text='beforeClick'
...
<Button
android:id="#+id/button1"
android:onClick="updateExpression"
...
</Linear Layout
</LinearLayout>
</HorizontalScrollView>
The idea, is to change the textField text property, when clicking the button on the same layout!
Ok for 2 is easy, just need the reference of each button and each textfield and change use the get and set method!
But i want to add more layouts dynamically, and grab each one's reference would be a hard task.
So two questions :
How can i grab the id of the Linear layout, that have the clicked button?
I want to handle the 'insideLinearLayout(1 or 2 depending on the clicked button)'id in the 'updateExpression'!
How can i add more layouts with the same widgets as the ones created manually?
Thank you in advance.
Best of codings!
1)Your onClick function is passed the view that was clicked. Every view has a getParent() function. You can use it to get the LinearLayout, then get the id.
2)Create them with the new keyword then add them to the parent layout. For something like this I would probably make a custom compund view holding everything you want to instantiate at once, so you can treat the entire set of widgets as one.

Android Parent Activity Button on top of all Child Activities

So here is my problem and Question
I have a Header and Footer layout in a Main.xml and its FragmentActivity is Main.java.
Now this header and footer is common across all activities in my application as shown by Image.
https://www.dropbox.com/s/pgox67k33u0zxct/device-2013-10-23-002417.png
So to solve this problem i used Fragments as Shown in Figure Main.java Fragment A is inside Main.xml . Now this all works fine Fragment A is showing header and footer of Main.xml
Problem starts when i start an activity from Fragment A it totally opens in a New Window full screen . My understanding was if i start an Activity from Fragment A then this Activity will only take place of Fragment A . I hope you understand my question .
Re-using Layouts with <include/>
Define your Header and Footer layout
and use that like
<include layout="#layout/header"/>
and
<include layout="#layout/footer"/>
e.g
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/header" />
Go through Android Document
http://developer.android.com/training/improving-layouts/reusing-layouts.html
Why are you using activities in such case?
It seems that fragments would be more convenient for this type of problem especially since you want that header/footer are used from Parent activity.
so you would have in your xml:
header
framelayout (this would be replaced by fragment within activity)
footer
Each of the fragments have its own lifecycle and parent activity as well.
You can find example of this in Android documentation:
http://developer.android.com/guide/components/fragments.html

using same buttons many times

I have 3 buttons that is modify ,view, clear and i want it to be used in 20 activities. I want to re use the same as if in user control in android?
Button 1- Modify,
Button 2 - Clear,
Button 3- view.
Is it good to make common view for all?
You can keep your 3 buttons in another layout say layout_button.xml and then include this in ur activity's layout wherever required like :
For ex. this ur activty layout :
<?xml version="1.0" encoding="utf-8"?>
<-- Inflate your other elemnts of ur activity -->
<include
android:layout_width="fill_parent"
android:layout_x="0dp"
android:layout_y="0dp"
layout="#layout/layout_button" />

Start each Layout with the same LinearLayout automatically

I Have simple question, i Have some linear layout with buttons on it.
how can I make this linear layout share to all my application layouts without copy it to the beginning of each layout.
I mean that any xml layout will start from the same LinearLayout, and will share the same references , like browser bar but when I launch to another Activity it's still visible.
Thank's guys
Use the include tag in each of your layouts
<include android:id="#+id/layout_id" layout="#layout/my_layout" />
see this blog post http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/

How to make the custom xml layout for this android project?

I am trying make application from this project: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html
But here, there is a Layout that generating dynamicly. I want to create my own layout in xml file. So what should i have to do for it.
Please anyone can help me to make the xml layout from this dynamic layout ??
Thanks.
That example is not creating a "dynamic layout". The layout, which is the part you'd be defining in XML, consists of only one View object, MyView.
What I assume you are referring to by "dynamic layout" is the MyView class, which is a custom View object which accepts touch input and draws on the screen. This cannot be defined in XML... you must write the Java code to handle the logic necessary, since the regular View class (which MyView is extending) does not support such functionality.
What you would need to do is create a Java file defining the MyView class. Say for example, com.example.MyView. Then, in XML, you can include this custom view in your layout by referring to the full name, including the package name. For example...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<com.example.MyView>
android:layout_height="fill_parent"
android:layout_width="fill_parent"
</com.example.MyView>
</LinearLayout>
You can use this layout in an activity as usual using setContentView.

Categories

Resources