Im trying to convert the results of timezone to text for use in textView. I am new ro android devloping, so i need some help.
Here is my current code:
Code:
TimeZone tz = TimeZone.getDefault();
TextView tv = new TextView(this);
tv.setText(tz);
but it gives me this error:
Code:
The method setText(CharSequence) in the type TextView is not applicable for the arguments (TimeZone)
.
I understand the error, but I do not know how to convert the Timezone to "Eastern Standard Time" or "Pacific Standard Time" or whatever the case may be.
remember im a noob.. so please make it easy and actually show me the code. (im also new to java)
Try this:
Code:
tv.setText(tz.getDisplayName());
Whenever you are stuck its always worth looking through the android reference for the classes you are working with. You'll almost always find a solution there
http://developer.android.com/reference/java/util/TimeZone.html
that fixed it.. Thank you so much!
Yeah but like i say I'm new at java and android. So i can't understand most of it.
alexander7567 said:
that fixed it.. Thank you so much!
Yeah but like i say I'm new at java and android. So i can't understand most of it.
Click to expand...
Click to collapse
No worries. Just continue making some practice apps. And you'll get better at it!
ok my next question is.. I want this to update every 30 seconds. I thought about using a Thread.sleep(10000);, but i realize this would not work because android would detect it being an infinite loop. So how could I create some kind of a thread i believe its called to poll the timezone every 10 seconds?
Where exactly are you trying to set the text view? Is it in a while loop?
no.. the textview simply shows the timezone. I live on the timezone line and i never know what time it really is.. So i was making a app and soon a widget that shows me what timezone I'm in. It's just a learning app for me.
So i need it to poll the timezone every 30 seconds and update the textview to the current time zone.
btw.. Looked at your app and i think its really neat that you can make a game like that.. Did you have to make all the graphics or what?
I am guessing you are making the text view in the onCreate method.
Do this:
Before your activity's onCreate method, make three class members like so
Code:
private TextView textView;
private TimeZone timeZone;
private Timer timer;
Then in the onCreate method do this:
Code:
timer = new Timer();
textView = new TextView(this);
timeZone = TimeZone.getDefault();
TimerTask timerTask = new TimerTask()
{
@Override
public void run()
{
textView.setText(timeZone.getDisplayName());
}
};
timer.schedule(timerTask, 0, 30000);
Then when you wish to stop the timer do this (in the onDestroy method maybe?)
Code:
timer.cancel();
I hope you are familiar with Java's Anonymous Inner Classes. If not, I'd highly recommend first reading and getting familiar with the various Java features and techniques before continuing Android.
---------- Post added at 02:51 AM ---------- Previous post was at 02:48 AM ----------
Thank you!
Yes I made all the graphics. Except for the coin . That was made by my cousin.
i did what u said, and then i put in textView.setTextColor(929) in the time to test and see if it is actually updating, and i found out it is never running the "task"... Know what could be going on?
Sorry about the old code. I did not test it before posting it.
Anyways I've tested the following code and it works fine for me. The timerTask is called every 30 seconds. You can verify this by looking at LogCat.
Code:
public class TestActivity extends Activity
{
private TimeZone timeZone;
private TextView textView;
private Timer timer;
private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textView = new TextView(this);
timeZone = TimeZone.getDefault();
timer = new Timer();
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
textView.setText(timeZone.getDisplayName());
}
};
TimerTask timerTask = new TimerTask()
{
@Override
public void run()
{
handler.sendEmptyMessage(0);
Log.d("TestActivity", "Timer Task Called");
}
};
timer.schedule(timerTask, 0, 30000);
setContentView(textView);
}
@Override
public void onDestroy()
{
super.onDestroy();
timer.cancel();
}
}
Let me know if this works for you
What you gave me works. Thank you! I have done a lot of googling on that but there is nothing more helpful than a personalized answer.
So i think i have one last question. when you used "setContentView(textView);", it sets the whole screen to that text. And i realize this is a really noob question. But i am also trying to display other things in this app. It hides everything else.
So here is my sad atempt to fix it::
Code:
package com.alexriggs.android.timezoneclock;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
public class TimeZoneClockActivity extends Activity
{
private TimeZone timeZone;
private TextView t;
private Timer timer;
private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
t=new TextView(this);
t=(TextView)findViewById(R.id.textView1);
timeZone = TimeZone.getDefault();
timer = new Timer();
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
t.setText(timeZone.getDisplayName());
}
};
TimerTask timerTask = new TimerTask()
{
@Override
public void run()
{
handler.sendEmptyMessage(0);
Log.d("TimeZoneClock", "Timer Task Called");
}
};
timer.schedule(timerTask, 0, 30000);
}
@Override
public void onDestroy()
{
super.onDestroy();
timer.cancel();
}
}
I show no compiler errors, but when i run the app i get a force close. I run it in debug mode and the error is around "t.setText(timeZone.getDisplayName());". I have tried googling but i cant seem to find the answer.
Sorry to keep bugging you, but i learn best by doing and not reading.
are there hardcode in this?
Well when you want to load up a view from an xml file you must do this:
Code:
setContentView(R.layout.main);
Where R.layout.main is the resource id of the xml file you wish to load. Only after that you can get the TextView (or any other component that is a part of that xml) using findViewById. And you don't need to do textView = new TextView(this); cause the TextView is already created when using xml, just grab a reference to it using findViewById.
So your code should look like this:
Code:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Or whatever your layout xml's name is
t=(TextView)findViewById(R.id.textView1);
timeZone = TimeZone.getDefault();
timer = new Timer();
// The rest remains the same
Try it and let me know.
---------- Post added at 12:18 AM ---------- Previous post was at 12:16 AM ----------
randommmm said:
are there hardcode in this?
Click to expand...
Click to collapse
Sorry, but I'm not sure I understand what you mean to ask.
wow.. That was so simple i now feel stupid lol.
Yes that fixed it. Thank you so much for your help.
Ok.. new question for that same program.. I am now attempting to create a widget for this that simply displays a text view that shows the timezone. My problem is it simply show 'Large Text". It is not updating the text view to the timezone. Here is my code in AppWidget.java.
Code:
package com.alexriggs.android.timezoneclock;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
public class AppWidget extends AppWidgetProvider {
private TimeZone timeZone;
private TextView t;
private Timer timer;
private Handler handler;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
t=new TextView(context);
timeZone = TimeZone.getDefault();
timer = new Timer();
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
t.setText(timeZone.getDisplayName());
}
};
TimerTask timerTask = new TimerTask()
{
@Override
public void run()
{
handler.sendEmptyMessage(0);
Log.d("TestActivity", "Timer Task Called");
}
};
timer.schedule(timerTask, 0, 30000);
}
}
BTW... Seems like this is the hardest language I have ever tried to learn.
I realize im missing setContentView(R.layout.widget), but everytime i set it i get "The method setContentView(int) is undefined for the type AppWidget". Could that be thte issue?
Hello again
I'm not really familiar with making widgets myself, so I cannot really say what you are doing wrong. I found a link to this tutorial on making a simple widget. I think you should try and follow it and see if you can get something out of it.
Let me know if it is helpful for you or not
http://www.vogella.com/articles/AndroidWidgets/article.html
It was a lot more helpful than most things out there.
But i still am not able to understand it the best.
What i am wanting to accomplish is simply make a text view widget that displays the timezone (I.E. EST, CST, PST) and make it refresh every 30 seconds.
It isn't very hard to do in a normal app, but a widget seems impossible to me.
Is there anyone that can help me with This?
I currently have a mediacontroller that I am using to control a video in a videoview. I am trying to get access to the seekbar/progressbar and am having no luck. I found somethign on google that I thought would work, and it does in fact seem to return back an object, but when I try to assign my own listener to the click event or even just set the visibility of the seekbar to invisible, it doesnt do anything. Any ideas as to what I am doing wrong?
Code:
int topContainerId = getResources().getIdentifier("mediacontroller_progress", "id", "android");
SeekBar seekbar = (SeekBar) mMediaCont.findViewById(topContainerId);
seekbar.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Log.d("In slider onclick", "WE HAVE LIFT OFF");
}
});
I'm looking at both a book and the beginning projects on developers.android.com and have a question. Google is utilizing the android:nClick to handle the click event which then points to a public method for execution....but the book is setting up click listeners by setting a View with the buttons ID and then executing the setOnClickListener. OnClickListenver has one method called OnClick which is then executed.
//book
public class MyGame extends Activity implements OnClickListener {
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
My question. Which is the better way to handle the Button Click? In looking at what google does in the XML, it seems a helluva lot cleaner....but curious on what the advantage is for the other way (and I cannot think of any). Search of the site didn't yield anything.
I dunno if the methods have advantages between them, however, I prefer the androidnClick method since the code looks clearer
You usually want to use the onClick method, as there is no benefit to implementing the OnClickListener (only more effort and possible performance loss).
If you want things like long clicks or other events, you will have to implement that (or define an inline listener), as there is no xml tag for those.
I think the OnClickListener is actually only there for consistency with these other classes
Hello! I am working on a Google Maps Android API project and am having a difficult time. I have several markers on my map, which is an indoor location. I was able to change the color of the markers, but would like to change the icons as well. I've looked over the documentation from google and am trying to implement it but it is not working and I cannot go any further until it does. Id like to use custom images, but for now I want to just see it work. Im trying to use this code:
Code:
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow))
. I'm getting an error in Android studio saying that it can't resolve 'arrow' symbol. my full code is here: It doesn't seem like I'm missing any imports, so I'm not sure why this doesn't work.
Code:
package com.aa.maps;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public Bitmap resizeMapIcons(String iconName, int width, int height){
Bitmap imageBitmap = BitmapFactory.decodeResource(getResources(),getResources().getIdentifier(iconName, "drawable", getPackageName()));
Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap, width, height, false);
return resizedBitmap;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Milwaukee, Wisconsin.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Add a marker in Milwaukee and move the camera
LatLng mpm = new LatLng(43.0408468, -87.921474);
LatLng planetarium = new LatLng(43.040622, -87.920798);
LatLng theater = new LatLng(43.040497, -87.920640);
LatLng marketplace = new LatLng(43.040779, -87.921717);
mMap.addMarker(new MarkerOptions().position(mpm).title("Milwaukee Public Museum").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))).showInfoWindow();
mMap.addMarker(new MarkerOptions().position(planetarium).title("The Daniel M. Soref National Geographic Planetarium").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
mMap.addMarker(new MarkerOptions().position(theater).title("The Daniel M. Soref National Geographic Theater").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
mMap.addMarker(new MarkerOptions().position(marketplace).title("Museum Marketplace"));
mMap.addMarker(new MarkerOptions().position(marketplace).title("Museum Marketplace").icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
}
}
I am currently trying to make a seekbar cycle through colors on progress(when moved). I have an xml file with some defined colors within it, this is what i have tried so far.
seekbarcolor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int seekbarprogress = 0;
@override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekbarprogress = progress;
int colorset = Color.argb(0xff, progress*0x10000, progress*0x100, progress)+0xff000000;
drawPaint.setColor(colorset);
}
This code generates and uses only 1 base color. I want to instead pull from an xml file or an array defined in the class. How do i do this?