[Q] Widgets with Image views.. - Android Software Development

Hello Xda! Im having an issue. i made what was going to be a widget in an application it was a simple digital clock, but i found out converting from an application to a widget was very difficult ( For me at least)
Well here i have this code from the original application
Code:
ImageView imageh1 = (ImageView) findViewById(R.id.imgh1);
imageh1.setImageResource(R.drawable.number_0);
My failed attempt to convert to a widget...
Code:
remoteViews = new RemoteViews( context.getPackageName(), R.layout.main );
watchWidget = new ComponentName( context, main.class );
remoteViews.setTextViewText( R.id.widget_textview, setBitmap(R.drawable.number_0);
im extremly confused...

bumpingg bump

in your app code you have an imageview and in your appwidget you have a textview and try to pass an image as text. use an imageview in your appwidget and try
Code:
remoteViews.setImageViewResource(R.id.imgh1, R.drawable.number_0);

Related

Inflating layout XML files and layout parameters

Why are the layout parm's specified in a layouts XML file not used when you inflate the layout manually in Java. This is a view that is being added to another view.
I have to manually create the layout parms using something like the following after inflating
and before adding to the parent view.
LayoutParams lp = new LayoutParams(-1,-2);
inflatedView.setLayoutParams(lp);
Is there a way to make the newly created view adhere to what is in the XML. I really
hate doing this in Java and divorcing it from the layout's definition.
TIA!
use the LayoutInflater service
Code:
// getSystemService is a method for context and can be called directly from an activity
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.my_layout, null);
Even better, use the
Code:
inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
variant with the future parent of the inflated view (with attachToRoot set to false if needed, as required for ListViews) to properly inherit LayoutParams from the parent.
And I simply use LayoutInflater.from(Context) instead of getSystemService(), makes it easy to chain the call :
Code:
LayoutInflater.from(context).inflater(R.layout.my_layout, parent, false);
seydhe said:
Even better, use the
Code:
inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
variant with the future parent of the inflated view (with attachToRoot set to false if needed, as required for ListViews) to properly inherit LayoutParams from the parent.
And I simply use LayoutInflater.from(Context) instead of getSystemService(), makes it easy to chain the call :
Code:
LayoutInflater.from(context).inflater(R.layout.my_layout, parent, false);
Click to expand...
Click to collapse
I'm not a fan of chaining calls but that's a style thing. I don't really like the readability/debugability/maintainability of them down the line. But again that's just a subjective opinion, to each their own.
And of course what you posted is pretty readable. But I don't think you really meant to pass an int to the XMLPullParser?
Also why do the API docs say this then...
http://developer.android.com/reference/android/view/LayoutInflater.html
This class is used to instantiate layout XML file into its corresponding View objects. It is never be used directly -- use getLayoutInflater() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on. For example:
Code:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
Context.LAYOUT_INFLATER_SERVICE);
Click to expand...
Click to collapse
Is there some danger to using the static LayoutInflater.from(context) ? Or just a badly worded api that is telling us not to instantiate the LayoutInflater ourselves? (I'm guessing the latter).

AppWidget problem when process is running

So I'm programming a application with a widget.
On my widget I got a button that starts an activity with a PendingIntent. This activity that's getting launched isn't my main activity.
Now when I'm doing it like this I get a Strange problem:
- Launching App as normal over appdrawer -> main activity opens up
- Pressing Home or Back button
- Add widget to homescreen
- Click button on widget -> nothing happens!
If I force stop my application before adding the widget to the homescreen, a click on the button on the widget opens up the activity like it should. Now I can also launch my main activity, pause it and the widget still works. So the widget only fully works if I my application isn't running in the background while adding the widget.
Has anyone of you experienced something like that?
Thank you in advance!
it is really dependent on how your onClickListener is added to the button. ive had problems where the first widget i create doesnt have the onClickListener attached but the second one will. do you mind sharing your code that you use to attach the onClickListener?
here is mine:
Code:
/** Called when the activity is first created. */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Toast.makeText(context, "onUpdate()", Toast.LENGTH_SHORT).show();
//super.onUpdate(context, appWidgetManager, appWidgetIds);
//attach an onClick intent to the layout
final Intent onClick = new Intent(context, GITextCloud.class);
onClick.setAction(LAUNCH_GMAIL_GAPPS);
PendingIntent onClickPending = PendingIntent.getBroadcast(context, 0, onClick, 0);
RemoteViews rv1 = new RemoteViews(context.getPackageName(), R.layout.gitc_html);
rv1.setOnClickPendingIntent(R.id.full_widget, onClickPending);
for (int appWidgetId : appWidgetIds) {
appWidgetManager.updateAppWidget(appWidgetId, rv1);
}
}
My code is really almost the same. Just that I need PendingIntent.getActivity instead of getBroadcast.
what flags do you set for your intent? cause if your activity is already running and thats when the button wont work it could be that you need a Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT or some other flags in the PendingIntent
try setting onClick.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) or what ever your Intent is called. i think the fact that your widget and your app are going to be seperate programs means they must start in different threads. cause not all widgets need an Activity running to function
I got it now! I somehow managed to screw up my remoteviews, they didn't got updated properly.
Anyway, thanks a lot for your answers

Open Maps and show a place.

Hello, im trying to program an intent for open google maps and show a place.
I want to select the place by coordinates.
I have tested
Code:
String uri = "geo:0,0?q="+address;
context.startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
It works fine and show the location of the address with a marker
But using coordinates:
Code:
String uri = "geo:"+ lat+ "," +log;
context.startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
Is the zone is shown but there's no marker for showing the exact place.
how can I place the marker in the intent (with a label if possible)
Thanks in advance.
i take it you've seen this http://developer.android.com/guide/appendix/g-app-intents.html
it looks like it says that the use of the geo tag is underdevelopment at the moment. it might just be that the implementation of this uri is broken.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")");
startActivity(intent);

[Q] How to keep layouts inflated after activity restarts

Hi guys,
On a button click I am inflating a layout like so:
Code:
public void plusLayout(View v) {
// inflating layout here:
LinearLayout ll1 = (LinearLayout) findViewById(R.id.main_layout);
// this layout is being inflated:
View newView = getLayoutInflater().inflate(R.layout.layout_to_be_added, null);
// add layout
ll1.addView(newView);
}
But when the activity restarts, the inflated layouts are gone.
I'd like the layouts to stay there.
(The user can click a button to remove the layout by hand).
I must be missing something trivial here right?
Cheers,
Daan
Which way is it restarted?
If the complete app is restarted, a new layout will be set in the onCreate method.
nikwen said:
Which way is it restarted?
If the complete app is restarted, a new layout will be set in the onCreate method.
Click to expand...
Click to collapse
Yeah when you press back button and start the app again or completely kill it.
It also happens on orientation change as the activity get restarted then as well.
But I think you can override that in the manifest somewhere.
DaanJordaan said:
Yeah when you press back button and start the app again or completely kill it.
It also happens on orientation change as the activity get restarted then as well.
But I think you can override that in the manifest somewhere.
Click to expand...
Click to collapse
Ah ok.
The point is: If you open the app or turn your device, the onCreate method is called. There you set a completely new layout. You would need to save that the layout is inflated (you could use a SharedPreferences entry) and inflate it in the onCreate method. If you just want it to appear again after turning the device, use the onSaveInstanceState method and the onRestoreInstanceState method. That would be better practice.
Look at the activity lifecycle.
Just so I'm sure I get this right :
The user launches the app, the layouts are not inflated
He presses a button which calls your plusLayout() method, so the layouts are now inflated
The user quits the activity and restarts it, the layouts are not inflated anymore but you want them to.
Is that correct ?
If it is, 2 ways I can think of :
Overriding savedInstanceState() & onRestoreInstanceState() :
First, declare a private Boolean before the onCreate() of your activity :
Code:
private Boolean isInflated = false;
Then, set it to true in the onClick() of your button, and override savedInstanceState and onRestoreInstanceState like so :
Code:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save state changes to the savedInstanceState.
// This bundle will be passed to onCreate if th activity is
// killed and restarted.
savedInstanceState.putBoolean("inflate", isInflated);
}
Code:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
Boolean myBoolean = savedInstanceState.getBoolean("inflate");
if (myBoolean == true)
plusLayout(myView);
}
Using the sharedPreferences
Same logic, different way to save the boolean :
Before onCreate(), declare a private boolean and a private SharedPreferences :
Code:
private Boolean isInflated = false;
private SharedPreferences prefs = getSharedPreferences("MY_PREFS");
in the onClick of your button :
Code:
isInflated = true;
Editor e = prefs.edit();
e.putBoolean("inflate", isInflated);
e.commit();
Then, in your onCreate(), retrieve the stored value and if it's true, call your plusLayout() method :
Code:
Boolean doInflate = prefs.getBoolean("inflate", false // this is the default value);
if (doInflate == true)
plusLayout(myView);
nikwen said:
Ah ok.
The point is: If you open the app or turn your device, the onCreate method is called. There you set a completely new layout. You would need to save that the layout is inflated (you could use a SharedPreferences entry) and inflate it in the onCreate method. If you just want it to appear again after turning the device, use the onSaveInstanceState method and the onRestoreInstanceState method. That would be better practice.
Look at the activity lifecycle.
Click to expand...
Click to collapse
Okay I'm working on that at the moment.
Whenever a layout is created an (int) "counter" get incremented.
I will save this "counter" in the SharedPreferences.
When the app starts layouts get created "counter" times.
Is this good practice?
It seems so strange that there isn't an easier way to save layout/activity states.
Edit:
Androguide.fr said:
Just so I'm sure I get this right :
The user launches the app, the layouts are not inflated
He presses a button which calls your plusLayout() method, so the layouts are now inflated
The user quits the activity and restarts it, the layouts are not inflated anymore but you want them to.
Is that correct ?
Click to expand...
Click to collapse
That is correct. Big thanks for the examples.
DaanJordaan said:
Okay I'm working on that at the moment.
Whenever a layout is created an (int) "counter" get incremented.
I will save this "counter" in the SharedPreferences.
When the app starts layouts get created "counter" times.
Is this good practice?
It seems so strange that there isn't an easier way to save layout/activity states.
Edit:
That is correct. Big thanks for the examples.
Click to expand...
Click to collapse
I would use his snippets. They are good (as always). Decide which one to use by what I have given above:
Just for turning:
onSaveInstanceState and onRestoreSavedInstanceState
For turning and reopening:
Shared preferences

[Q] Problem with strings

Last day I opened my Strings.xml file with Notepad and added all my needed texts to it. Today I opened eclipse and wrote this code :
TextView title1 = (TextView) findViewById (R.string.myAddedTextName);
But the problem is that eclipse cannot find that. I restarted it but it didn't work again. I checked the strings.xml file by eclipse. Everything was corect but the R.String (gen) hasn't saved them. Now I can't accses the texts. Is there anyway I can access them?
Thanks in advance.
You can clean the project. That way it will build again. Just select Project > Clean... and then select your project.
I may be wrong but I don't think you can do this: TextView title1 = (TextView) findViewById (R.string.myAddedTextName);
TextView is a widget object. You are telling eclipse to find a TextView (by id) but are telling it to look for a stored string.
zalez said:
I may be wrong but I don't think you can do this: TextView title1 = (TextView) findViewById (R.string.myAddedTextName);
TextView is a widget object. You are telling eclipse to find a TextView (by id) but are telling it to look for a stored string.
Click to expand...
Click to collapse
That is right. Ids are integer values. I did not see that. :laugh:
Change it to
Code:
TextView title1 = (TextView) findViewById (R.id.myAddedId);
You set the id that way:
Code:
<Button ...
android:id="@+id/myAddedId"
... />
This is what I was thinking:
If title1 is indeed a TextView in your layout you would do this:
Code:
TextView title1 = (TextView) findViewById(R.id.myTextView);
title1.setText(R.string.myAddedTextName);
Thank you.
The strings are now OK.
Also the famous TextView = String which I wrote at the first post has no problem in eclipse.
torpedo mohammadi said:
Thank you.
The strings are now OK.
Also the famous TextView = String which I wrote at the first post has no problem in eclipse.
Click to expand...
Click to collapse
This is because R.id......
And R.string........ Point to int data type so compiler won't mind
Not to mention it will crash at runtime
Sent from my GT-S5302 using Tapatalk 2
So, suppose I have written a text in the strings.xml like :
<String name ="text">Hi there.</string>
Using which code can retreive the "Hi there."? I have something in my mind:
String s = getString(R.string.text);
Or
String s = getResources().getString(R.string.text);
Which one can give me the "Hi there."?
The first one. Not sure about the second one, but just use the first
Both of them will do the same job
Sent from my GT-S5302 using Tapatalk 2

Categories

Resources