making a custom calendar like layout programmatically - java

I am developing an app which basically should download a list of events from a website and then views them in a in a calendar like layout. I already covered the first part. But I don't have an idea how to make a layout which would be a grid (tume on one axis and date on second axis) and the events in form of listview would be placed on the grid based on the start and end time. I just need a hint how to start this. The problem is that i have to add the events depending on user choices.

This will create calender like grid programtically..
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
/* INFLATE MAIN TABLE LAYOUT */
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.table_root, null);
TableLayout tbl = (TableLayout) root.findViewById(R.id.tblLayout);
this.setContentView(root);
ViewGroup row = (ViewGroup) inflater.inflate(R.layout.table_row, root, false);
/* GENERIC LOOP FOR CREATING TABLE */
int count = 1;
for (int day = 0; day < calendarCount; day++)
{
row.addView(day);
if (count <= weekCount) {
root.addView(row);
row = (ViewGroup) inflater.inflate(R.layout.popup_row, root, false);
}
count++;
}
Also you can see..
http://w2davids.wordpress.com/android-simple-calendar/

Related

Views not visible after adding them to relative layout programmatically

I am creating an activity to show timetable. I have a list of lectures, and the respective day and duration of each lecture. I am trying to inflate a compound view from a layout xml file, and then adding the inflated view to one of the relative layouts in an arraylist, where one element of the arraylist represents one column or a day.
days is an arraylist of relativelayouts. The below function is a member of the activity, and is the logic to add the lectures. According to the length of the lecture, I adjust the height with ViewGroup LayoutParams for the card inside the view, and then add the view in relative layout with relative layout params. But the views do not show up.
Edit : The Code is working fine, The arraylist of the model class was a problem.
private void addTimeTableViews(){
for (LectureModel lecture : lectures) {
LayoutInflater inflater = LayoutInflater.from(this);
int currentDay = lecture.getDayOfTheWeek();
RelativeLayout layout = days.get(currentDay);
View view = inflater.inflate(R.layout.layout_tt_box, layout);
int boxHeight = getTimeDifference(lecture.getStartTime(),lecture.getEndTime());
//to adjust height of the card according to length of lecture
MaterialCardView card = view.findViewById(R.id.tt_box_card);
int cardWidth = card.getLayoutParams().width;
ViewGroup.LayoutParams cardParams = new ViewGroup.LayoutParams(cardWidth, boxHeight - 4);
card.setLayoutParams(cardParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.topMargin = getTimeInMinutes(lecture.getStartTime());
layout.addView(view, params);
}
}

Resize all recyclerview cells on itemTouchhelper during drag/reorder

I have a horizontal recyclerview which implements ItemTouchHelper callback for dragging and reordering cells. When a cell is being moved i want to shrink all of the cells widths so they all appear on screen. In onItemSelected() i can successfully change the size of the cell currently being moved, and revert back in onItemClear.
However, i want to resize all cells and not just the current cell. What is the best approach for this?
I tried creating a function in my adapter class and calling it to resize via notifyDataSetChanged() however it was removing the current cell being moved.
Is there a way to do this as part of my ItemTouchHelperCallback - creating a similar function as onItemSelected but updating all other cells?
I am not sure if I completely understand what you are trying to do, but I think this might help:
int childs = recyclerView.getChildCount();
for (int i = 0; i < childs; i++) {
View child = recyclerView.getChildAt(i);
if (child != null) {
RecyclerView.ViewHolder vholder = recyclerView.getChildViewHolder(child);
if (vholder != null) {
ViewHolder holder = (ViewHolder) vholder;
if (holder.view != null) {
LayoutParams params = (LayoutParams)holder.view.getLayoutParams();
params.width = <new_width>;
params.height = <new_height>;
holder.view.setLayoutParams(params);
}
}
}
}
Happy coding !!

List view item background color

Hello how can i set the item liner Layout background color from this array that it have just 3 color , position one color one , position two color two , position three color three and then position one color one and so on..
the position of the list view
int[] androidColors = context.getResources().getIntArray(R.array.randomColor);
viewHolder.linearLayout.setBackgroundColor(androidColors[position]);
You can try to create your own custom adapter and implement getView function like this :
public View getView (int position, View convertView, ViewGroup parent){
if( convertView == null ){
//We must create a View:
convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
//Here we can do changes to the convertView, such as set a text on a TextView or set the color of every single item of your view.
//or an image on an ImageView.
return convertView;
}
try to have a look to this post:
Change background colour of current listview item in adapter getView method

Menu option to clear all <EditText>

I am designing an app in Android Studio, but I am having trouble creating a clear option in the menu of my app.
Can someone show me how to add a menu option that will clear all the <EditText> items in my app I am creating?
You can try with below code and change the id of "your_group" with your main layout id.
ViewGroup group = (ViewGroup)findViewById(R.id.your_group);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");//here it will be clear all the EditText field
}
}
Please let me know if have any doubts.

How to multiply column by the number in ListView, Android

I select data from a database.
Data in one column, you must convert to kilograms, I need to multiply by a factor of one column. The data in column sets_weight in the pound, and it is necessary to display in kilograms, with without modifying to the database
How to do it correctly. Any ideas! Thanks!
onSets = db.getSets(exesIdsColExes, toprog_dif);
listSets.setAdapter(new SimpleCursorAdapter(this,
R.layout.itemsets, onSets,
new String[] {"sets_ids", "sets_weight", "sets_ones"},
new int[] {R.id.itemsets_ids, R.id.itemsets_weight, R.id.itemsets_ones}) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
// Here we get the textview and set the color
Typeface fontTitleProg = Typeface.createFromAsset(getAssets(), "AGHELVETICA.TTF");
TextView itemsets_ids = (TextView) row.findViewById(R.id.itemsets_ids);
itemsets_ids.setTypeface(fontTitleProg, 1);
itemsets_ids.setGravity(0x05);
TextView itemsets_weight = (TextView) row.findViewById(R.id.itemsets_weight);
itemsets_weight.setTypeface(fontTitleProg, 1);
itemsets_weight.setGravity(0x01);
TextView itemsets_ones = (TextView) row.findViewById(R.id.itemsets_ones);
itemsets_ones.setTypeface(fontTitleProg, 1);
itemsets_ones.setGravity(0x01);
return row;
}
});
I've never done any android development, but the documentation of SimpleCursorAdapter says:
First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked.
So you just need to pass a custom instance of SimpleCursorAdapter which will, if the column index is 1 (second column), get the value in pounds from the cursor, multiply it to transform pounds into kg, set the result as the text of the view, and return true. For the other columns, return false.

Categories

Resources