How to refresh a view component run time in android - android

I want to change the text of Text view pragmatically in my android application, here is my code to set the string to the text view
TextView dateTime;
dateTime = (TextView) findViewById(R.tasksheetlist.txtdatetime);
private void updateDateValue(String date)
{
String text = dateTime.getText().toString();
text = text+"\n"+date;
dateTime.setText(text);
}
and this is how the function called some where
Date date = new Date();
String dateStr = Helper.ConvertDateStringFromDate(date, "DD MMM");
updateDateValue(dateStr);
This code called but does not reflected on the view.
I think I need to refresh the layout but do not know how?
Please help me in this??

I think this article will help you. http://android-developers.blogspot.com/2007/11/stitch-in-time.html Basically you want to use the Handler class provided in the SDK. You shouldn't need to call invalidate like because setText does that for you. Hope this helps!

Related

Is it possible to customize the CalendarView widget to display events and customize the colour (Gradient colour)?

I am currently developing an app and I am stuck in implementing the calendar part of the app. Now I am trying to achieve something similar to this:
I did not find any functions on the original Calendar View which would let me display event indicator under days, add gradient colour instead of solid etc.
I wanted to implement this design to my app:
I tried using Compact Calendar View by SundeepK however I did not manage to implement an event adapter to go with this calendar, I always received an error on passing context, as the custom calendar does not use context.
This code below I tried to implement the List view to be filled with the events in the month. So on month load or scroll, events would be loaded, passed to the adapter and the list view would be filled with new events. However, whatever I did I always got an error that I can't cast android.Context to the custom calendar Context.
CalendarActivity.java
compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
#Override
public void onDayClick(Date dateClicked) {
List<Event> bookingsFromMap = compactCalendarView.getEvents(dateClicked);
EventListAdapter adapter = new EventListAdapter(context, bookingsFromMap);
ListView listView = (ListView) findViewById(R.id.calendar_event_list);
listView.setAdapter(adapter);
Log.d(TAG, "inside onclick " + simple_date_format.format(dateClicked));
if (bookingsFromMap != null) {
Log.d(TAG, bookingsFromMap.toString());
mutableBookings.clear();
for (Event booking : bookingsFromMap) {
mutableBookings.add((String) booking.getData());
Log.d(TAG, "Events in this day found!");
}
} else {
Log.println(Log.VERBOSE,TAG, "\n NO EVENTS FOUND \n");
}
}
EventListAdapter.java
public class EventListAdapter extends ArrayAdapter<Event> {
public EventListAdapter(Context context, List<Event> feeds) {
super(context,0,feeds);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Event feed = getItem(position);
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.event_feed_item_row, parent, false);
}
TextView eventName = (TextView) convertView.findViewById(R.id.event_list_dataText);
TextView eventDay = (TextView) convertView.findViewById(R.id.event_list_dayTextView);
TextView eventMonth = (TextView) convertView.findViewById(R.id.event_list_monthTextView);
SimpleDateFormat simple_month = new SimpleDateFormat("Mmm", Locale.US);
SimpleDateFormat simple_day = new SimpleDateFormat("dd", Locale.US);
eventName.setText((String) feed.getData());
eventMonth.setText(simple_month.format(feed.getTimeInMillis()));
eventDay.setText(simple_day.format(feed.getTimeInMillis()));
return convertView;
}
}
Now from the screenshots here:
Calendar background can only be solid when I wanted to have a gradient as a background, the same as the selected date is a solid colour.
The event list item looks like this(Trying to find a way to add the events dynamically by month):
The ideal version should look something like this:
Any suggestions would be appreciated.
To Do that in easy and fast way you just need to follow this git hub library which is much option to do your required functions.
Link (https://github.com/mahimrocky/EventCalender)
Bottom side will create in Recyler view as wel by using Database
I have found one project which create calendar based on your requirements by modify little bit
Use this : Sample Project
Your First need is make gradient background for calendar only need to set res/fraglay.xml gradient drawable resource to root layout background
Your Second need is make selected date gradient background you do it by modify drawable/selectedback.xml file
Your third need is bottom recyclerview it already available in above project if you want to modify event name layout edit layout/view_item based on your requirements
In above project you can set event by init method of GooglecalenderView you can set whatever event you want if one date multiple event is available in that case also it work fine
While Using CompactCalendarView You can Add Events to the calendar and on click you can get the events of the selected date You can Use this code to implement ..
Long miliseconds = GettingMiliSeconds(formatteddate);
Event newevent = AddEvents(miliseconds, "" + "Text You wants to add in your event ");
calendarView.addEvent(newevent);
public Event AddEvents(Long milliseconds,String Description)
{
Event event = new Event(Color.BLUE,milliseconds,""+Description);
return event;
}
public Long GettingMiliSeconds(String Date)
{
long timeInMilliseconds = 0;
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date mDate = sdf.parse(Date);
timeInMilliseconds = mDate.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return timeInMilliseconds;
}

Complex return in Ternary Operator?

I am trying to return a specifically styled date after checking the state of an element but struggling with the exact way to write this.
I have this code for a textview in my XML:
android:text='#{String.format(item.status.toLowerCase().contains("check") ? ("Scheduled for %s", item.ScheduledDate) : ("Published on %s", item.PublishedDate))}'
but it is expecting a +<>=- rather than the ,
Can someone please help me encapsulate this properly?
Unfortunately, you will have to do that from within whichever Java class sets the content view to the xml file in question.
Like so:
TextView tv = (TextView) findViewById(R.id.textview);
String text = (item.status.toLowerCase().contains("check") ? String.format("Scheduled for %1$s", item.ScheduledDate) : String.format("Published on %1$s", item.PublishedDate);
tv.setText(text);

Create a new random number each time an activity is opened

I'm doing a simple addition game on android studio. Every time the "addition" activity is opened I was hoping to generate two random numbers in two text boxes. However I can't get it to work and the text box appears blank every time I run the app and open the activity. Here's my code for one of the text boxes.
public void textview2(View View) {
Random addition1 = new Random();
int additionint1 = addition1.nextInt(100)+1;
TextView additionText1 = (TextView) findViewById(R.id.textView2);
String additionString1 = String.valueOf(addition1);
additionText1.setText(additionString1);
}
Change
String additionString1 = String.valueOf(addition1);
to
String additionString1 = String.valueOf(additionint1);
You are missing something here I believe,
In onCreate method, you should do something like this..
Random addition1 = new Random();
int additionint1 = addition1.nextInt(100)+1;
TextView additionText1 = (TextView) findViewById(R.id.textView2);
String additionString1 = String.valueOf(additionint1);
additionText1.setText(additionString1);
but the point is, it should be in onCreate so that when your activity is created, the number is generated and .setText for your required textView is called...
you can also consider the same if you want to handle other activity states..
PS: Note that you can have this code folded in function and called in overrided method onCreate
also, note String.valueOf(additionint1);

setText - cannot resolve method

Pretty sure this is a stupid question but I can't figure this out.
I am trying to get an integer returned by a datepicker to a string. This code works where day is the integer of interest
dateButton = (Button) findViewById(R.id.dateButton);
dateButton.setText((Integer.toString(day));
This code gives me the error that cannot resolve method setText
String yearString = "";
yearString.setText(Integer.toString(year));
I don't understand why I cant convert the int to a string unless I use a view?
Is this, what you want to do.
int year = 2014;
String yearString = Integer.toString(year);
Because stText mthod is only for setting text on certain views on android likeTextView, EditText, Button.
You can set integer value by following ways if day is an integer value,
dateButton.setText(day+"");
or by
dateButton.setText(String.valueOf(day));
or
dateButton.setText(Integer.toString(day));
dateButton = (Button) findViewById(R.id.dateButton);
dateButton.settext(Interger.Valueof(day));
you have to instantiate the object before you call it view I am new to this also
So this
((TextView) findViewById(R.id.now_playing_text)).setText(trackTitle)
Becomes
TextView Title = (TextView) findViewById(R.id.now_playing_text);
Title.setText(trackTitle);
setText must be applied on an a class that contains in it or it supper classes the setText method.
Instead of
dateButton.setText((Integer.toString(day));
Try this
dateButton.setText(day+"");

How to get EditText value and display it on screen through TextView?

I want to get the user input for the EditText view and display it on the screen through TextView when the Button is clicked. I also, want to know what modifications can be done on the string.xml file to do this.
I didn't get the second question, maybe you can elaborate...but for your first query.
String content = edtEditText.getText().toString(); //gets you the contents of edit text
tvTextView.setText(content); //displays it in a textview..
I'm just beginner to help you for getting edittext value to textview. Try out this code -
EditText edit = (EditText)findViewById(R.id.editext1);
TextView tview = (TextView)findViewById(R.id.textview1);
String result = edit.getText().toString();
tview.setText(result);
This will get the text which is in EditText Hope this helps you.
EditText ein=(EditText)findViewById(R.id.edittext1);
TextView t=new TextView(this);
t.setText("Your Text is="+ein.getText());
setContentView(t);
bb.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
String s1=tt.getText().toString();
tv.setText(s1);
}
}
);
First get the text from edit text view
edittext.getText().toString()
and Store the obtained text in a string, say value.
value = edittext.getText().toString()
Then set value as the text for textview.
textview.setText(value)
yesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
eiteText=(EditText)findViewById(R.id.nameET);
String result=eiteText.getText().toString();
Log.d("TAG",result);
}
});
Easiest way to get text from the user:
EditText Variable1 = findViewById(R.id.enter_name);
String Variable2 = Variable1.getText().toString();
in "String.xml" you can notice any String or value you want to use, here are two examples:
<string name="app_name">My Calculator App
</string>
<color name="color_menu_home">#ffcccccc</color>
Used for the layout.xml: android:text="#string/app_name"
The advantage: you can use them as often you want, you only need to link them in your Layout-xml, and you can change the String-Content easily in the strings.xml, without searching in your source-code for the right position.
Important for changing language, you only need to replace the strings.xml - file
Use the following code when clicked on the button :
String value = edittext.getText().toString().trim(); //get text from editText
textView.setText(value); //setText in a textview
Hope to be useful to you.
Try this->
EditText text = (EditText) findViewById(R.id.text_input);
Editable name = text.getText();
Editable is the return data type of getText() method it will handle both
string and integer values
First get the value from edit text in a String variable
String value = edttxt.getText().toString();
Then set that value to textView
txtview.setText(value);
Where edttxt refers to edit text field in XML file
and txtview refers to textfield in XML file to show the value

Categories

Resources