Obtaining widget UI references - android

After doing some coding for widgets, I've been wondering if I'm correct in assuming that the only way you can modify UI elements is through RemoteViews and there is no way to obtain a reference to any elements on the widget (TextView, ImageView, etc.). Or is there a way to store references to these specific elements of my layout? Perhaps through intents or constructors? Thanks for any input.

After doing some coding for widgets
Given the context of the rest of your question, I am interpreting "widgets" to mean "app widgets".
I've been wondering if I'm correct in assuming that the only way you can modify UI elements is through RemoteViews
Generally speaking, yes.
there is no way to obtain a reference to any elements on the widget (TextView, ImageView, etc.)
Correct. The app widgets are rendered by the home screen, and so the home screen's process is the one that has the View objects, not yours. The exception would be if you write your own home screen.

Related

Android: Widget, AppWidget, Fragment, Activity and App - What's the difference?

I've got a lot of experience with Java and C#, but I'm new to Android. I mainly use C# because I am enamored of the Control hierarchy. I love the plug-and-play of the ontology. I'm trying to understand the ontology in this new paradigm and I may have been given some false information.
With respect to Apps, that should be the largest component. Within the App, there may be several Activities. An activity can display a number of Fragments. AppWidgets appear to be a special case as they exist as a component of the App, but are shown on their own. And I was told that you can extend Buttons or ProgressBar to create your own components which again appear to be called Widgets.
As I said, I may have this completely wrong. Ideally I would like to create my own widgets which I can put on a Fragment, an AppWidget or an Activity; any of which I might compose into an App. All the online sources I've found only discuss Widget in the sense of an AppWidget? Was I given incorrect information? Can anyone clarify the ontology?
Thanks
"Widget" is a bit of an overloaded term. You will probably have better luck if you search for tutorials on "custom Views" instead. I'll include a brief rundown of various terms and what they mean at the bottom.
A custom View is pretty much anything that extends the View class (or any of its subclasses) and isn't part of the framework. Custom views can be used wherever typical Views are expected, e.g. in layout files or directly constructed in Java. One thing to note: only certain Views can be used in an AppWidget because they are running in another process outside of your app. This means your custom Views cannot be used in AppWidgets. In my experience this tends not to matter too much.
App: An application. Contains components, which are defined in the manifest within the <application> tag.
Activity: One of the four application components. Nearly always has an associated UI, composed of a hierarchy of Views.
Fragment: A framework class that helps modularize your application's code and UI. Fragments can be attached to an Activity and can contribute some UI to the View hierarchy of the Activity. They are entirely optional; you don't have to use Fragments in your app, and you can attach a Fragment without it contributing any UI to the Activity.
View: A UI component, such as text (TextView) or images (ImageView). These are also referred to as "widgets", and you may notice the framework classes are found in the android.widget package. Some views contain other views, so that you can build a UI hierarchy; these will extend ViewGroup and are referred to as "view groups" or "layouts" more or less interchangeably.
AppWidget: Something the user can add to his or her homescreen. This is provided by the app, but is not one of the 4 application components mentioned previously (it is managed by an application component, namely a special subclass of BroadcastReceiver). Most people colloquially refer to these as "widgets" because it's shorter to say and launchers used that terminology as well, thus conditioning users to it.

Android UI - How to create Dynamic UI on XML

I have a weird problem(I assume, not common in Android). I'am working on a product which has its UI "elements" defined by a individual(admin) using our website. Positioning, characteristic and order of the UI elements is decided by the admin. However, type of UI elements is pre selected. Some UI elements are - checkbox, editText,spinner etc.
Although we handle this programatically, I was wondering if same can be achieved by pre-defined XML(since we know the UI elements we support) and dynamically modify the position and characteristic of the UI elements.
I wanted to do this because it is becoming very painful to manage the UI using the java code.
Any suggestions is appreciated,
SKU
The question is puzzling. You say that your admin defines the UI elements, "however", the type of the UI elements is pre-selected? Preselected by who? Exactly what in XML is pre-defined, and what isn't?
If a widget class is defined in XML (a widget is something like , , etc.), you can modify some aspects of the widget's appearance by inflating its layout (which is done automatically for the main layout), finding the widget within the layout tree, and changing the widget's characteristics.
You can't change the widget's type. You'd have to delete the widget from the layout and add in a new one that fits into roughly the same space.

Dynamically change the content/layout/flow controlled via server

I am new to android development, and we have a very specific requirement.
We need to change the content/layout/flow of the app on the fly. For e.g. we have a layout which consists of some images, textarea and textboxes. There might be a request coming to change the textarea to a textbox.
We thought about this and are thinking to provide the apk with a json/xml which will contain all these changes.
My question is will it be possible to re-draw the objects again dynamically and change the content?
Yes this is possible. You can dynamically design what has to be displayed in your Activity UI screen. If you feel there are only 2 or 3 different UI screens that would be repeatedly used, then you can have XMLs for these screens and you can just change their labels in OnCreate() of Activity class before rendering. LayoutInflater class would be helpful here.
When you design a Android Application with Activities and Fragments your XML layout definition is always static. If you want a true dynamic layout structure you should use a Web View with a HTML content pointing some URL.
As Rahul says, another approach is to manage the "default cases". For me that is the standard way to design an Android Application.
The dynamic content (values) can be done with a simple http call to server you can get values for your views.
The navigation could be handled by switching Intents, but, definitively you have to associate these intents to UI elements like buttons in the most cases, and ¿How you can do that if your layout is changing over time?.
I think, that the WebView could be a very easy solution for your problem.

creating popup dialog like facebook android app

I want to make popup dialog like in facebook app with pointing marker to the selection area.
here is an example how it should look like.
http://imageshack.us/photo/my-images/9/facebookandroid.png/
please help if you know something about it.
Thanks in advance..
May be your question would get solved from this
https://github.com/lorensiuswlt/NewQuickAction
There are multiple strategies that can solve this problem, but each solution has to deal with the placement of the triangle/pointer at the top of the dialog since it has to be positioned to indicate the user's selection.
I think the easiest way to solve this problem is:
Start an activity that has a transparent theme. So, apply something like android:theme="#android:style/Theme.Translucent.NoTitleBar" to the Activity in the manifest. If you want a Title Bar, do some searching for the right android-style.
Pass an extra to the activity that represents the navigation element chosen by the user. this will be used by the activity to position the triangle pointer.
Handle this extra in the activity's onCreate() method by drawing the triangle/pointer in code. This means you will want to call setContentView(...) in onCreate(), then get a reference to the ViewGroup (i.e. RelativeLayout, AbsoluteLayout, etc.). Once you have that you can call ViewGroup#addView(View child, int index) to add the ImageView that represents the triangle. You probably want to use index = 0.
Ensure that whatever View(s) you add programatically is/are not covered (i.e. hidden) by elements in the XML.
Adding the triangle is relatively easy, but positioning can get tricky. First of all, the screenshot you posted assumes a fixed-width device, and that's just not true for an Android app in the market that needs to work on 1000s of screen sizes. The Y-coordinate of the triangle/pointer is not a problem, since the height of the top-nav is probably hardcoded. It's the X-coordinate that can get tricky. What if the top-nav elements stretch? Let's assume we have a robust/decent way to get the X-position where the center of the triangle/pointer should be. Given that, I'd try using a vertical LinearLayout in my Activity and use an ImageView with a left-margin for the triangle/pointer. That's just a guess and you'll probably have to tinker with this to get the right View elements and positioning strategy for your specific implementation.

Using a Widget in Activity

it's me again :-)
Is there a way to use a widget (for home screen) in my own activites?
i think the homescreen is made with java, too?!
i do not want to change thinks in the widget, i still want to display them.
Regards
FLy
If you are looking to "re-use" a widget's layout and may be its base code, that is possible. I don't think it will be a good idea [or possible] to display a widget as an activity.
There are a lot of limitations in the number of view elements that can be used inside a widget. And again the way you will update a widget [RemoteViews] is actually very different from that of an Activity.
So in short, the answer would be a "NO".
Edit: This question has also been asked and answered a lot of times: difference between view and widget

Categories

Resources