Align images and text with GridView - android

I have the following design.
As you can see, the week days text and the "column title" images are not aligned to the GridView items.
Here is the complete xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="11"
tools:context="com.test.me.fragments.patientResume">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#drawable/gradient">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/aboveLights"
android:src="#mipmap/above_lights"
android:contentDescription="#string/above_lights" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/under_lights"
android:contentDescription="#string/under_lights" />
<com.test.dashboard.view.CircleImageView
android:id="#+id/centerImg"
android:layout_width="104dp"
android:layout_height="104dp"
android:layout_centerInParent="true"
android:background="#mipmap/ic_default_avatar" />
<com.test.dashboard.view.widget.RobotoLightTextView
android:id="#+id/PatientName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/aboveLights"
android:layout_alignTop="#id/aboveLights"
android:layout_alignRight="#id/aboveLights"
android:layout_alignBottom="#id/aboveLights"
android:layout_margin="1dp"
android:gravity = "center_horizontal"
android:textSize="20sp"
android:text="Mauricio Affonso"
android:textColor="#color/colorPrimaryDark" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp">
<com.test.dashboard.view.widget.RobotoLightTextView
android:id="#+id/took_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/took"
android:textColor="#color/colorPrimaryDark" />
<com.test.dashboard.view.widget.RobotoLightTextView
android:id="#+id/took_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/took_header"
android:gravity="center"
android:text="-"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp">
<com.test.dashboard.view.widget.RobotoLightTextView
android:id="#+id/missed_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/missed"
android:textColor="#color/colorPrimaryDark" />
<com.test.dashboard.view.widget.RobotoLightTextView
android:id="#+id/missed_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/missed_header"
android:layout_alignRight="#+id/missed_header"
android:layout_below="#+id/missed_header"
android:gravity="center"
android:text="-"
android:textColor="#color/colorPrimaryDark"
android:textSize="25sp" />
</RelativeLayout>
<com.test.me.wave.WavesView
android:id="#+id/waves_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:background="#ddf7ff">
<com.test.dashboard.view.widget.RobotoLightTextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/countPatient"
android:textAlignment="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#color/colorPrimaryDark"
android:text="1 OF 1"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/titleImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/backgroundLight">
<ImageView
android:id="#+id/morning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:scaleType="fitCenter"
android:src="#mipmap/ic_morning" />
<ImageView
android:id="#+id/noon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:src="#mipmap/ic_noon" />
<ImageView
android:id="#+id/evening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:src="#mipmap/ic_evening" />
<ImageView
android:id="#+id/bedtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:src="#mipmap/ic_bedtime" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:id="#+id/tableContainer"
android:background="#color/backgroundLight">
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/date_layout"
android:layout_margin="#dimen/activity_horizontal_margin"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" />
<com.test.me.grid.DateLayout
android:id="#+id/date_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/grid"
android:layout_marginTop="30dp"
android:id="#+id/Sun"
android:textColor="#color/colorPrimaryDark"
android:layout_marginLeft="7dp"
android:text="SUN" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/grid"
android:layout_marginTop="75dp"
android:textColor="#color/colorPrimaryDark"
android:layout_marginLeft="7dp"
android:id="#+id/Mon"
android:text="MON" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/grid"
android:layout_marginTop="120dp"
android:textColor="#color/colorPrimaryDark"
android:layout_marginLeft="7dp"
android:id="#+id/Tue"
android:text="TUE" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/grid"
android:layout_marginTop="167dp"
android:textColor="#color/colorPrimaryDark"
android:layout_marginLeft="7dp"
android:id="#+id/Wed"
android:text="WED" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/grid"
android:layout_marginTop="215dp"
android:textColor="#color/colorPrimaryDark"
android:layout_marginLeft="7dp"
android:id="#+id/Thu"
android:text="THU" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/grid"
android:layout_marginTop="260dp"
android:textColor="#color/colorPrimaryDark"
android:layout_marginLeft="7dp"
android:id="#+id/Fri"
android:text="FRI" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/grid"
android:layout_marginTop="310dp"
android:textColor="#color/colorPrimaryDark"
android:layout_marginLeft="7dp"
android:id="#+id/Sat"
android:text="SAT" />
</RelativeLayout>
</LinearLayout>
As you can see the content of RelativeLayout titleImages and the week days text should be aligned to the GridView items but i'm having different results on different phone resolutions. I even tried to dynamically change the positions:
int test[] = new int[2];
int space = grid.getHorizontalSpacing();
int height = grid.getMeasuredHeight() / 7;
int Width = grid.getColumnWidth() / 2;
int paddingTop = grid.getPaddingTop();
int paddingLeft = grid.getPaddingLeft();
Resources r = getResources();
float spacePx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, space + 10, r.getDisplayMetrics()) ;
float paddingTopPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, paddingTop - 7, r.getDisplayMetrics());
float paddingLeftPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, paddingLeft - 12, r.getDisplayMetrics());
grid.getLocationInWindow(test);
for(int i = 0; i < 7; i++){
Days[i].setY(paddingTopPx + (test[1] - grid.getMeasuredHeight()) + ((i +1)* (height)));
}
for(int i = 0; i < 4; i++){
icons[i].setX((paddingLeftPx + test[0]) + (i) * (spacePx + Width));
}

I checked with different layout but all seems not that perfect so the best way to make your view is use single gridview .Almost succeded but you have to work on that more logic to get exactly what you need
<GridView
android:id="#+id/grid_calender"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:numColumns="5"
android:verticalSpacing="0dp" />
day_view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp">
<TextView
android:id="#+id/tv_new"
android:layout_width="#dimen/text_height_width"
android:layout_height="#dimen/text_height_width"
android:layout_centerInParent="true"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/darker_gray" />
</RelativeLayout>
Create adapter with a better logic as needed its an edited code,Please ignore the commented codes Thanks
public class CustomCalendarAdapter extends BaseAdapter {
static final int FIRST_DAY_OF_WEEK = 0;
public ArrayList<Day> dayList = new ArrayList<>();
String[] days;
Context context;
List<Boolean> event_status;
// Calendar local_cal_30 = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
Calendar cal;
public CustomCalendarAdapter(Context context, ArrayList<Day> dayList, List<Boolean> _event_status, Calendar cal) {
this.context = context;
this.dayList = dayList;
this.event_status = _event_status;
this.cal = cal;
cal.set(Calendar.DAY_OF_MONTH, 1);
refreshDays();
}
#Override
public int getCount() {
return days.length;
}
#Override
public Object getItem(int position) {
return dayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
// public int getPrevMonth(){
// if(cal.get(Calendar.MONTH) == cal.getActualMinimum(Calendar.MONTH)){
// cal.set(Calendar.YEAR, cal.get(Calendar.YEAR-1));
// }else{
//
// }
// int month = cal.get(Calendar.MONTH);
// if(month == 0){
// return month = 11;
// }
//
// return month-1;
// }
public int getMonth() {
return cal.get(Calendar.MONTH);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (position % 5 == 0) {
v = vi.inflate(R.layout.day_view, null);
TextView day = (TextView) v.findViewById(R.id.tv_new);
if (position / 5 == 0) {
day.setText(R.string.sunday);
} else if (position / 5 == 1) {
day.setText(R.string.monday);
} else if (position / 5 == 2) {
day.setText(R.string.tuesday);
} else if (position / 5 == 3) {
day.setText(R.string.wednesday);
} else if (position / 5 == 4) {
day.setText(R.string.thursday);
} else if (position / 5 == 5) {
day.setText(R.string.friday);
} else if (position / 5 == 6) {
day.setText(R.string.saturday);
}
} else {
Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
Day dayObject = dayList.get(position);
v = vi.inflate(R.layout.day_view, null);
// RelativeLayout rl_outerView = (RelativeLayout) v.findViewById(R.id.rl);
// FrameLayout today = (FrameLayout) v.findViewById(R.id.today_frame);
// FrameLayout event = (FrameLayout) v.findViewById(R.id.event_frame);
// FrameLayout disable_frame = (FrameLayout) v.findViewById(R.id.disable_frame);
// FrameLayout selected_frame = (FrameLayout) v.findViewById(R.id.selected_frame);
TextView tv_date = (TextView) v.findViewById(R.id.tv_new);
Calendar local_cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
tv_date.setVisibility(View.VISIBLE);
// rl_outerView.setVisibility(View.VISIBLE);
// selected_frame.setVisibility(View.GONE);
if (dayObject.getDay() == 0) {
// rl_outerView.setVisibility(View.GONE);
} else {
tv_date.setVisibility(View.VISIBLE);
tv_date.setText(String.valueOf(dayObject.getDay()));//sets the date
}
//Only show 30 days from current date
// if ((dayObject.getDay()<local_cal.get(Calendar.DAY_OF_MONTH) &&
// dayObject.getMonth() == local_cal.get(Calendar.MONTH)) || ((dayObject.getMonth() == local_cal_30.get(Calendar.MONTH))&&
// dayObject.getDay()>local_cal_30.get(Calendar.DAY_OF_MONTH))) {
//Only show upcoming dates from current date
if ((dayObject.getDay() < local_cal.get(Calendar.DAY_OF_MONTH) &&
dayObject.getMonth() == local_cal.get(Calendar.MONTH))) {
//These dates come out of 30 days(Based on current date)
// disable_frame.setVisibility(View.VISIBLE);
} else {
//Rest days come here
// disable_frame.setVisibility(View.GONE);
dayObject.setDisable_date(true);
// event.setVisibility(View.GONE);
tv_date.setTextColor(Color.WHITE);
//Now compare the schedule date list with each date for matches
for (int i = 0; i < CustomGridCalendarView.schedule_list.size(); i++) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date scheduleDate = format.parse(CustomGridCalendarView.schedule_list.get(i).getScheduleDate());
cal = Calendar.getInstance();
cal.setTime(scheduleDate);
if (dayObject.getDay() == cal.get(Calendar.DAY_OF_MONTH) &&
dayObject.getMonth() == cal.get(Calendar.MONTH) &&
dayObject.getYear() == cal.get(Calendar.YEAR)
) {
//Show the event frame, here we got a match date
// event.setVisibility(View.VISIBLE);
// today.setVisibility(View.GONE);
tv_date.setTextColor(context.getResources().getColor(R.color.colorPrimary));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
if (dayObject.isEvent_status()) {
// event.setVisibility(View.GONE);
// selected_frame.setVisibility(View.VISIBLE);
// today.setVisibility(View.GONE);
tv_date.setTextColor(Color.WHITE);
} else {
// selected_frame.setVisibility(View.GONE);
tv_date.setTextColor(Color.WHITE);
}
//Check for current date
if (dayObject.getYear() == local_cal.get(Calendar.YEAR) && dayObject.getMonth() == local_cal.get(Calendar.MONTH)
&& dayObject.getDay() == local_cal.get(Calendar.DAY_OF_MONTH)) {
// today.setVisibility(View.VISIBLE);
// tv_date.setTextColor(Color.WHITE);
// event.setVisibility(View.GONE);
// tv_date.setTypeface(Typeface.DEFAULT_BOLD);
} else {
// today.setVisibility(View.GONE);
}
}
}
return v;
}
public void refreshDays() {
boolean before_current_date = true;
// clear items
dayList.clear();
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH) + 7;
int firstDay = (int) cal.get(Calendar.DAY_OF_WEEK);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
//TimeZone tz = TimeZone.getDefault();
// figure size of the array
if (firstDay == 0) {
days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6)];
} else {
days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1)];
}
int j = FIRST_DAY_OF_WEEK;
// populate empty days before first real day
if (firstDay > 0) {
for (j = 0; j < (firstDay - FIRST_DAY_OF_WEEK) + 7; j++) {
days[j] = "";
Day d = new Day(0, 0, 0, false);
dayList.add(d);
}
} else {
for (j = 0; j < (FIRST_DAY_OF_WEEK * 6) + 7; j++) {
days[j] = "";
Day d = new Day(0, 0, 0, false);
dayList.add(d);
}
j = FIRST_DAY_OF_WEEK * 6 + 1; // sunday => 1, monday => 7
}
// populate days
int dayNumber = 1;
if (j > 0 && dayList.size() > 0 && j != 1) {
dayList.remove(j - 1);
}
for (int x = 0, i = j - 1; i < days.length; i++) {
Day day;
if (event_status.size() > x) {
day = new Day(dayNumber, year, month, event_status.get(x));
} else {
day = new Day(dayNumber, year, month, false);
}
x++;
Calendar cTemp = Calendar.getInstance();
cTemp.set(year, month, dayNumber);
//int startDay = Time.getJulianDay(cTemp.getTimeInMillis(), TimeUnit.MILLISECONDS.toSeconds(tz.getOffset(cTemp.getTimeInMillis())));
days[i] = "" + dayNumber;
dayNumber++;
dayList.add(day);
}
}
}

Related

ViewPager not responding to touch in layout area created dynamically in Fragment

I'm trying to implement Viewpager with a calendar. I'm using the same three instances of the calendar Fragment for testing the ViewPager lets call the smooth motion of transferring one fragment to the next SWIPING. the swiping will not work in the area of the fragment that the layout was created dynamically in the fragment itself but the rest of page(above and below the date buttons)the swiping works great! here is some of the code the entire frag code would not fit so had to edit some declarations and button listener methods
// Main Activity
public class calenderview extends AppCompatActivity {
ArrayList<Fragment> fraglist;
ViewPager pager;
calenderviewpageradaptor adaptor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calenderview);
fraglist = new ArrayList<>();
fraglist.add(new Calview_frag()); fraglist.add(new Calview_frag()); fraglist.add(new Calview_frag());
adaptor = new calenderviewpageradaptor(getSupportFragmentManager(),fraglist);
pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(adaptor);
}
public class calenderviewpageradaptor extends FragmentStatePagerAdapter {
ArrayList<Fragment> adaptorlist;
public calenderviewpageradaptor(FragmentManager fm,ArrayList<Fragment> list){
super (fm);
this.adaptorlist = list;
}
#Override
public int getCount(){
return 3;
}
#Override
public Fragment getItem(int pos){
return adaptorlist.get(pos);
}
}
}
public class Calview_frag extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private LinearLayout.LayoutParams getdaysLayoutParams() {
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
buttonParams.weight = 1;
return buttonParams;
}
private void addDaysinCalendar(ViewGroup.LayoutParams buttonParams, DisplayMetrics metrics) {
int engDaysArrayCounter = 0;
LinearLayout.LayoutParams doitwithmarg = new LinearLayout.LayoutParams(120,120);
doitwithmarg.setMarginStart(cacmarg);
LinearLayout.LayoutParams doittoit = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
doittoit.weight=1;
for (int weekNumber = 0; weekNumber < 6; ++weekNumber) {
for (int dayInWeek = 0; dayInWeek < 7; ++dayInWeek) {
Button day = new Button(getContext());
day.setBackground(getResources().getDrawable(R.drawable.roundshapeclear));
day.setTextColor(getResources().getColor(R.color.GREY));
day.setBackgroundColor(Color.TRANSPARENT);
day.setTextSize((int) metrics.density *5 );
day.setSingleLine();
if(dayInWeek!=0){
day.setLayoutParams(doitwithmarg);
Space s = new Space(getContext());
s.setLayoutParams(doittoit);
weeks[weekNumber].addView(s);
days[engDaysArrayCounter] = day;
weeks[weekNumber].addView(day);}
else{
day.setLayoutParams(doitwithmarg);
days[engDaysArrayCounter] = day;
weeks[weekNumber].addView(day);}
++engDaysArrayCounter;
}
}
}
private void init(View view) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
calendar = Calendar.getInstance();
weekOneLayout = (LinearLayout)view.findViewById(R.id.calendar_week_1);
weekTwoLayout = (LinearLayout)view.findViewById(R.id.calendar_week_2);
weekThreeLayout = (LinearLayout)view.findViewById(R.id.calendar_week_3);
weekFourLayout = (LinearLayout)view.findViewById(R.id.calendar_week_4);
weekFiveLayout = (LinearLayout)view.findViewById(R.id.calendar_week_5);
weekSixLayout = (LinearLayout)view.findViewById(R.id.calendar_week_6);
currentYear = (TextView)view.findViewById(R.id.current_date);
currentMonth = (TextView)view.findViewById(R.id.current_month);
currentDateDay = chosenDateDay = calendar.get(Calendar.DAY_OF_MONTH);
if (userMonth != 0 && userYear != 0) {
currentDateMonth = chosenDateMonth = userMonth;
currentDateYear = chosenDateYear = userYear;
} else {
currentDateMonth = chosenDateMonth = calendar.get(Calendar.MONTH);
currentDateYear = chosenDateYear = calendar.get(Calendar.YEAR);
}
currentYear.setText("" + chosenDateYear);
currentMonth.setText(ENG_MONTH_NAMES[currentDateMonth]);
initializeDaysWeeks();
if (userButtonParams != null) {
defaultButtonParams = userButtonParams;
} else {
defaultButtonParams = getdaysLayoutParams();
}
addDaysinCalendar(defaultButtonParams,metrics);
initCalendarWithDate(chosenDateYear, chosenDateMonth, chosenDateDay,view);
}
private void initializeDaysWeeks() {
weeks = new LinearLayout[6];
days = new Button[6 * 7];
weeks[0] = weekOneLayout;
weeks[1] = weekTwoLayout;
weeks[2] = weekThreeLayout;
weeks[3] = weekFourLayout;
weeks[4] = weekFiveLayout;
weeks[5] = weekSixLayout;
}
private void initCalendarWithDate(int year, int month, int day, View v) {
if (calendar == null)
calendar = Calendar.getInstance();
calendar.set(year, month, day);
int daysInCurrentMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
chosenDateYear = year;
chosenDateMonth = month;
chosenDateDay = day;
calendar.set(year, month, 1);
int firstDayOfCurrentMonth = calendar.get(Calendar.DAY_OF_WEEK);
Log.d(tag,"FFFFFFiiiiiiiiiiirrrrrrrrrrsssssssssst day of month int"+firstDayOfCurrentMonth);
calendar.set(year, month, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Log.d(tag,"LLLLLLLLLLLLlaaaaasttttttttttt day of month int"+daysInCurrentMonth);
int dayNumber = 1;
int daysLeftInFirstWeek = 0;
firstDayOfCurrentMonth= firstDayOfCurrentMonth-1;
int indexOfDayAfterLastDayOfMonth = 0;
if (firstDayOfCurrentMonth != 0) {
daysLeftInFirstWeek = firstDayOfCurrentMonth;
indexOfDayAfterLastDayOfMonth = daysLeftInFirstWeek + daysInCurrentMonth;
> for (int i = firstDayOfCurrentMonth; i < firstDayOfCurrentMonth + daysInCurrentMonth; ++i) {
//checks all dates of calendar to set background and text colors
if (currentDateMonth == chosenDateMonth
&& currentDateYear == chosenDateYear
&& dayNumber == currentDateDay){
days[i].setBackground(getResources().getDrawable(R.drawable.roundshapegrey));
days[i].setTextColor(Color.WHITE);
} else {
days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));
days[i].setTextColor(Color.BLACK);
}
int[] dateArr = new int[3];
dateArr[0] = dayNumber;
dateArr[1] = chosenDateMonth;
dateArr[2] = chosenDateYear;
days[i].setTag(dateArr);
days[i].setText(String.valueOf(dayNumber));
days[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onDayClick(v);
}
});
++dayNumber;
}
>
for (int i = 7; i < 7 + daysInCurrentMonth; ++i) {
if (currentDateMonth == chosenDateMonth
&& currentDateYear == chosenDateYear
&& dayNumber == currentDateDay) {
days[i].setBackground(getResources().getDrawable(R.drawable.roundshape));
days[i].setTextColor(Color.WHITE);
} else {
days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));
days[i].setTextColor(Color.BLACK);
}
int[] dateArr = new int[3];
dateArr[0] = dayNumber;
dateArr[1] = chosenDateMonth;
dateArr[2] = chosenDateYear;
days[i].setTag(dateArr);
days[i].setText(String.valueOf(dayNumber));
days[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onDayClick(v);
}
});
++dayNumber;
}
}
//Displays previous and next month days
if (month > 0)
calendar.set(year, month - 1, 1);
else
calendar.set(year - 1, 11, 1);
int daysInPreviousMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = daysLeftInFirstWeek - 1; i >= 0; --i) {
int[] dateArr = new int[3];
if (chosenDateMonth > 0) {
if (currentDateMonth == chosenDateMonth - 1
&& currentDateYear == chosenDateYear
&& daysInPreviousMonth == currentDateDay) {
} else {
days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));
}
dateArr[0] = daysInPreviousMonth;
dateArr[1] = chosenDateMonth - 1;
dateArr[2] = chosenDateYear;
} else {
if (currentDateMonth == 11
&& currentDateYear == chosenDateYear - 1
&& daysInPreviousMonth == currentDateDay) {
} else {
days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));
}
dateArr[0] = daysInPreviousMonth;
dateArr[1] = 11;
dateArr[2] = chosenDateYear - 1;
}
days[i].setTag(dateArr);
days[i].setTextColor(getResources().getColor(R.color.GREY));;
days[i].setText("");
days[i].setOnClickListener(null);
}
int nextMonthDaysCounter = 1;
for (int i = indexOfDayAfterLastDayOfMonth; i < days.length; ++i) {
int[] dateArr = new int[3];
if (chosenDateMonth < 11) {
if (currentDateMonth == chosenDateMonth + 1
&& currentDateYear == chosenDateYear
&& nextMonthDaysCounter == currentDateDay) {
// days[i].setBackground(getResources().getDrawable(R.drawable.roundshapegrey));
days[i].setTextColor(Color.WHITE);
} else {
days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));
}
dateArr[0] = nextMonthDaysCounter;
dateArr[1] = chosenDateMonth + 1;
dateArr[2] = chosenDateYear;
} else {
if (currentDateMonth == 0
&& currentDateYear == chosenDateYear + 1
&& nextMonthDaysCounter == currentDateDay) {
// selectedDayButton.setBackground(getResources().getDrawable(R.drawable.roundshapegrey));
selectedDayButton.setTextColor(Color.WHITE);
// days[i].setBackground(getResources().getDrawable(R.drawable.roundshapegrey));
days[i].setTextColor(Color.WHITE);
} else {
days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));
}
dateArr[0] = nextMonthDaysCounter;
dateArr[1] = 0;
dateArr[2] = chosenDateYear + 1;
}
days[i].setTag(dateArr);
days[i].setText("");
days[i].setOnClickListener(null);
}
calendar.set(chosenDateYear, chosenDateMonth, chosenDateDay);
currentYear.setText(" " + chosenDateYear);
currentMonth.setText(ENG_MONTH_NAMES[chosenDateMonth]);
}
public Calview_frag() {//required empty constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layoutview = inflater.inflate(R.layout.fragment_calview,container,false);
but = (ImageButton)layoutview.findViewById(R.id.getitbutton);
fbut = (ImageButton)layoutview.findViewById(R.id.getit2button);
display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenheight = size.y;
screenwidth = size.x;
Log.d(tag,"sssssssssssccreen size "+screenwidth);
if(screenwidth<1300)
cacmarg = screenwidth/50;
else
cacmarg = screenwidth/25;
init(layoutview);
return layoutview;
}
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/getitbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_alignParentLeft="true"
android:src="#drawable/backbutton"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/current_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/bluegrey"
android:textSize="28sp" />
<TextView
android:id="#+id/current_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:textColor="#color/bluegrey"
android:textSize="28sp" />
</LinearLayout>
<ImageButton
android:id="#+id/getit2button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/forwardbutton"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="7">
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:text="#string/sunday"
android:textColor="#color/bluegrey"
android:textSize="16sp" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:text="#string/monday"
android:textColor="#color/bluegrey"
android:textSize="16sp" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:text="#string/tuesday"
android:textColor="#color/bluegrey"
android:textSize="16sp" />
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:text="#string/wednesday"
android:textColor="#color/bluegrey"
android:textSize="16sp" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:text="#string/thursday"
android:textColor="#color/bluegrey"
android:textSize="16sp" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:text="#string/friday"
android:textColor="#color/bluegrey"
android:textSize="16sp" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:text="#string/saturday"
android:textColor="#color/bluegrey"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/calendar_week_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="7" />
<LinearLayout
android:id="#+id/calendar_week_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="7" />
<LinearLayout
android:id="#+id/calendar_week_3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="7" />
<LinearLayout
android:id="#+id/calendar_week_4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="7" />
<LinearLayout
android:id="#+id/calendar_week_5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="7" />
<LinearLayout
android:id="#+id/calendar_week_6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="7" /> </LinearLayout>
</ScrollView>
</LinearLayout>
are you passing the instance of the Fragment to the adapter? No I think. You should pass the list of fragments to the adapter along with fragment manager and should also change the calendarViewPager accordingly.
List<Fragment> fragments =new ArrayList<>();
fragments.add(Calview_frag());
fragments.add(Calview_frag());
fragments.add(Calview_frag());
calenderviewpageradaptor adaptor =
new calenderviewpageradaptor(getSupportFragmentManager(),fragments);

First image appearing small in size and on top left corner in a viewpager when rotate the device

Hi in my application i am using the Viewpager with PagerAdapter. it contains n number of images. but when i rotate the device when viewpager is on first image then the image shift to top left corner and appears very small. please have a look in the attached image.
code which i am using
public class CustomViewPager extends ViewPager {
private final long SWITCH_TIME_INTERVAL = 5000;
public CustomViewPager(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomViewPager(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
postDelayed(mSwither, SWITCH_TIME_INTERVAL);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height)
height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* this Runnable is use for automatically switching the view pager item.
* #author
*/
private Runnable mSwither = new Runnable() {
#Override
public void run() {
if( CustomViewPager.this.getAdapter() != null )
{
int count = CustomViewPager.this.getCurrentItem();
if( count == (CustomViewPager.this.getAdapter().getCount() - 1) )
{
count = 0;
}else
{
count++;
}
//Log.d(this.getClass().getName(), "Curent Page " + count + "");
CustomViewPager.this.setCurrentItem(count, true);
}
CustomViewPager.this.postDelayed(this, SWITCH_TIME_INTERVAL);
}
};
#Override
public boolean onTouchEvent(MotionEvent arg0) {
switch (arg0.getAction()) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP :
postDelayed(mSwither, SWITCH_TIME_INTERVAL);
break;
default:
removeCallbacks(mSwither);
break;
}
return super.onTouchEvent(arg0);
} }
//PagerAdapter onInstantiateItem code..
#Override
public Object instantiateItem(ViewGroup container, final int position) {
mImageFetcher.setLoadingImage(R.drawable.placeholder_tabportrait);
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.test_fragment1, container, false);
description = (TextView) itemView.findViewById(R.id.description);
title = (TextView) itemView.findViewById(R.id.car_title);
published = (TextView) itemView.findViewById(R.id.date_data);
author = (TextView) itemView.findViewById(R.id.author);
jumbo_tron_image = (ImageView) itemView.findViewById(R.id.jumbo_imgae);
TrayItem trayItem = getData(position);
if (null != trayItem) {
if (trayItem.getType() != null) {
if (trayItem.getImages() != null) {
mImageFetcher.loadImage(trayItem.getImages().getWidget(),
jumbo_tron_image);
}
return itemView;
}
//xml code.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:background="#color/white">
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="vertical"
>
<ImageView
android:id="#+id/jumbo_imgae"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="300dp"
/>
</FrameLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/container_height"
android:layout_below="#+id/main_container">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:layout_weight="0.5"
android:textColor="#color/black"
android:id="#+id/car_title"
android:maxLines="2"
android:ellipsize="end"
android:textSize="20sp"
android:text=" "/>
<ImageView
android:id="#+id/iv_add_test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:paddingTop="10dp"
android:src="#drawable/add_icon"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/car_title"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout2"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/linearLayout3"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/date_data"
android:layout_width="90dp"
android:layout_height="18dp"
android:text=""
android:textSize="13sp"
android:layout_gravity="left"
android:gravity="center"
android:textColor="#color/white"
android:background="#drawable/date_bg"/>
<TextView
android:id="#+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="15sp"
android:layout_marginLeft="10dp"
android:textColor="#color/black"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout> </RelativeLayout>

Overlying View with Opacity on top of ImageView

Currently I have a layout xml file where I bind many views. Here I am facing a problem. The problem is I can not overlay a view on an (heart image)Imageview. In layout xml file I can set but I can not overlay the view on that imageview.Please help me to figure our.I want the Black Opacity view on the Heart Imageview.
Here is the xmlsource codes. I have marked the important codes with *.
<LinearLayout
android:id="#+id/listlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="top"
android:orientation="vertical" >
<ListView
android:id="#+id/datalist"
style="#style/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<RelativeLayout
android:id="#+id/layout2"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#F18EA4"
android:gravity="bottom" >
<TextView
android:id="#+id/income_entry_container"
style="#style/LightYellow"
android:layout_width="70dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left|center"
android:text="#string/confirm_income" />
<TextView
android:id="#+id/expense"
style="#style/LightYellow"
android:layout_width="70dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/income_entry_container"
android:gravity="left|center"
android:text="#string/confirm_expense" />
<TextView
android:id="#+id/confirm_income_item_TextView"
style="#style/LightYellow"
android:layout_width="70dp"
android:layout_height="20dp"
android:layout_above="#+id/expense"
android:layout_toRightOf="#+id/income_entry_container"
android:gravity="right|center"
android:text="#string/confrim_income_item" />
<TextView
android:id="#+id/confirm_expense_item_TextView"
style="#style/LightYellow"
android:layout_width="70dp"
android:layout_height="20dp"
android:layout_alignLeft="#+id/confirm_income_item_TextView"
android:layout_below="#+id/confirm_income_item_TextView"
android:gravity="right|center"
android:text="#string/confirm_expense_item" />
<TextView
android:id="#+id/confirm_income_amount_TextView"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right|center"
android:text="#string/confirm_income_amount" />
<TextView
android:id="#+id/confrim_expnese_amount_TextView"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/confirm_income_item_TextView"
android:gravity="right|center"
android:text="#string/confirm_expnese_amount" />
<ImageButton
android:id="#+id/up_down_imageButton"
style="#style/UpDownButtonSelector"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="170dp"
android:layout_marginTop="10dp"
android:contentDescription="#string/up_down_imageButton" />
<TextView
android:id="#+id/save_percentage"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="#+id/balance_amount_TextView"
android:layout_toRightOf="#+id/heart_ImageView"
android:gravity="left|center"
android:textSize="14sp" />
<TextView
android:id="#+id/lbl_balance"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/confrim_expnese_amount_TextView"
android:gravity="left|center"
android:text="#string/usable_amount"
android:textSize="12sp" />
**<ImageView
android:id="#+id/heart_ImageView"
style="#style/LightYellow"
android:layout_width="260dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/lbl_balance"
android:contentDescription="#string/heart_content" />**
<TextView
android:id="#+id/balance_amount_TextView"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignBaseline="#+id/lbl_balance_amount"
android:layout_alignBottom="#+id/lbl_balance_amount"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/lbl_balance_amount"
android:gravity="right|center"
android:text="#string/balance_amount"
android:textSize="14sp" />
<TextView
android:id="#+id/lbl_balance_amount"
style="#style/LightYellow"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="#+id/confrim_expnese_amount_TextView"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/lbl_balance"
android:gravity="right|center"
android:text="#string/lbl_balance_amount"
android:textSize="14sp" />
**<View
android:id="#+id/overlay"
android:layout_width="260dp"
android:layout_height="20dp"
android:layout_below="#+id/lbl_balance" />**
</RelativeLayout>
</RelativeLayout>
Here is the source codes. I have marked the important codes with *.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_buta_confirm, container, false);
listRelativeLayout = (RelativeLayout) v
.findViewById(R.id.fragment_list);
int top = ((ButaColleTabBarActivity) getActivity())
.getActionBarHeight() + 2;
listRelativeLayout.setPadding(0, top, 0, 120);
listLinearLayout = (LinearLayout) v.findViewById(R.id.listlayout);
listLinearLayout.setPadding(0, 0, 0, 160);
ie_adapter = new Income_Expense_Adapter(getActivity(),
android.R.layout.simple_list_item_1, butaColleList);
view = (ListView) v.findViewById(R.id.datalist);
view.setAdapter(ie_adapter);
view.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ButaColle butaColle = butaColleList.get(position);
FragmentManager fragmentManager = getFragmentManager();
ButaColleDetailIncomeExpenseFragment butaDetail = ButaColleDetailIncomeExpenseFragment
.newInstance(butaColle);
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.realTabContent, butaDetail);
fragmentTransaction.addToBackStack("SearchFragment");
fragmentTransaction.commit();
}
});
view.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
view.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
ie_adapter.removeSelection();
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_delete_context, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_delete_data:
SparseBooleanArray selected = ie_adapter.getSelectedIds();
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
ButaColle butaitem = ie_adapter.getItem(selected
.keyAt(i));
dbhelper.deleteHistoryRecords(butaitem);
ie_adapter.remove(butaitem);
}
}
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
final int checkedCount = view.getCheckedItemCount();
mode.setTitle(checkedCount + " Selected");
ie_adapter.toggleSelection(position);
}
});
updownImageButton = (ImageButton) v
.findViewById(R.id.up_down_imageButton);
updownImageButton.setBackgroundResource(R.drawable.close01);
updownImageButton.setTag(R.drawable.close01);
updownImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (updownImageButton.getTag().equals((R.drawable.close01))) {
updownImageButton.setBackgroundResource(R.drawable.open01);
updownImageButton.setTag(R.drawable.open01);
int top = ((ButaColleTabBarActivity) getActivity())
.getActionBarHeight() + 2;
listRelativeLayout.setPadding(0, top, 0, 50);
listLinearLayout.setPadding(0, 0, 0, 160);
} else if (updownImageButton.getTag().equals(R.drawable.open01)) {
updownImageButton.setBackgroundResource(R.drawable.close01);
updownImageButton.setTag(R.drawable.close01);
int top = ((ButaColleTabBarActivity) getActivity())
.getActionBarHeight() + 2;
listRelativeLayout.setPadding(0, top, 0, 120);
listLinearLayout.setPadding(0, 0, 0, 160);
}
}
});
int itemCount = 0;
String itemlbl = "item";
incomeItemcount_TextView = (TextView) v
.findViewById(R.id.confirm_income_item_TextView);
itemCount = dbhelper.getItemCount(1);
if (itemCount > 1)
itemlbl = "items";
incomeItemcount_TextView.setText(String.valueOf(itemCount) + itemlbl);
itemCount = 0;
itemlbl = "item";
expenseItemcount_TextView = (TextView) v
.findViewById(R.id.confirm_expense_item_TextView);
itemCount = dbhelper.getItemCount(0);
if (itemCount > 1)
itemlbl = "items";
expenseItemcount_TextView.setText(String.valueOf(itemCount) + itemlbl);
double balance = 0.0;
double incomeAmount = 0.0;
double expenseAmount = 0.0;
incomeAmount_TextView = (TextView) v
.findViewById(R.id.confirm_income_amount_TextView);
incomeAmount = dbhelper.getTotalAmount(1);
incomeAmount_TextView.setText(String.valueOf(incomeAmount) + "$");
expenseAmount_TextView = (TextView) v
.findViewById(R.id.confrim_expnese_amount_TextView);
expenseAmount = dbhelper.getTotalAmount(0);
expenseAmount_TextView.setText(String.valueOf(expenseAmount) + "$");
balance = incomeAmount - expenseAmount;
balance_TextView = (TextView) v
.findViewById(R.id.balance_amount_TextView);
balance_TextView.setText(String.valueOf(balance) + "$");
int percentage = 0;
percentage = (int) ((balance / incomeAmount) * 100);
if (balance > 0) {
heartImageView = (ImageView) v.findViewById(R.id.heart_ImageView);
heartImageView.setImageResource(R.drawable.graph);
percentage_TextView = (TextView) v
.findViewById(R.id.save_percentage);
percentage_TextView.setText(String.valueOf(percentage) + "%");
} else {
heartImageView = (ImageView) v.findViewById(R.id.heart_ImageView);
heartImageView.setImageResource(0);
percentage_TextView = (TextView) v.findViewById(0);
percentage = 0;
}
**View overlay = (View) v.findViewById(R.id.overlay);
int opacity = 200; // from 0 to 255
overlay.setBackgroundColor(opacity * 0x1000000); // black with a variable alpha
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(300, 36);
overlay.setLayoutParams(params);
overlay.invalidate();**
return v;
}
Please Help me to figure our.Thank you!
Try Changing
**<View
android:id="#+id/overlay"
android:layout_width="260dp"
android:layout_height="20dp"
android:layout_below="#+id/lbl_balance" />**
to
**<View
android:id="#+id/overlay"
android:layout_width="260dp"
android:layout_height="20dp"
android:layout_alignBottom="#+id/heart_ImageView" />**
Changes made: layout alignment and the id of to whom it was aligned
You can try adding rules to the params with the addRule method
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
I'm not near my PC to test, but you may be able to set the view below lbl_balance with the addRule method
Or, try removing the layout params in the code all together. The xml inflates the view, so it is already created. All you need to do in code is set the background color. Which you can also do in the xml with
android:background="#80000000"

Delay in for loop-android

I'm new to android programming and I have a simple question. I have a random number, nr and I generate this number of ImageViews in the same location. Now, I want to apply an animation to each of these images, but not in the same time.
Let's say I want the first image to start moving at sec 0, then image2 at sec 2 and so on.
I have the images in an array, imb_items[].
for(int j=0; j<nr;j++){
imb_items[j].startAnimation(translateAnim);
????? how can I put here the delay?
}
I'm trying for a long time to do this, but I just can't do it with handlers or threads...I think I don't get the concept.
I would be very thankful if somebody would help me understand and give me a sample code that will work in my specific case.
thank you!
public class MyActivity extends ActionBarActivity implements OnClickListener {
private TextView mCalculatorDisplay;
private Boolean userIsInTheMiddleOfTypingANumber = false;
private CalculatorBrain mCalculatorBrain;
private static final String DIGITS = "0123456789.";
DecimalFormat df = new DecimalFormat("############");
Animation anim_translate;
public double valoare, produs, scor = 0;
int printfirst, printsecond;
Handler mHandler = new Handler();
int[] idMultiple = new int[]{6, 5, 3, 4, 2};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
//Img.setVisibility(View.VISIBLE);
//android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Animation translateAnim = AnimationUtils.loadAnimation(MyActivity.this, R.anim.translate);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
int dpHeight = (int)(displaymetrics.heightPixels / displaymetrics.density );
int dpWidth = (int)(displaymetrics.widthPixels / displaymetrics.density );
ImageView imb1=(ImageView)findViewById(R.id.ImageView1 );
Toast.makeText(getApplicationContext(), "Spiridusii lui Mos Craciun pregatesc jucariile pentru copii.",
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Ei au observat ca unele jucarii sunt incomplete. Ajuta-i sa comande piesele lipsa!",
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), height+"inaltime"+width,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), dpHeight+"inaltime"+dpWidth,
Toast.LENGTH_LONG).show();
RelativeLayout Layout=(RelativeLayout)findViewById(R.id.rel_banda);
ImageView banda=(ImageView) findViewById(R.id.banda);
mCalculatorBrain = new CalculatorBrain();
mCalculatorDisplay = (TextView) findViewById(R.id.Raspuns_user);
df.setMinimumFractionDigits(0);
df.setMinimumIntegerDigits(1);
df.setMaximumIntegerDigits(2);
findViewById(R.id.b0).setOnClickListener(this);
findViewById(R.id.b1).setOnClickListener(this);
findViewById(R.id.b2).setOnClickListener(this);
findViewById(R.id.b3).setOnClickListener(this);
findViewById(R.id.b4).setOnClickListener(this);
findViewById(R.id.b5).setOnClickListener(this);
findViewById(R.id.b6).setOnClickListener(this);
findViewById(R.id.b7).setOnClickListener(this);
findViewById(R.id.b8).setOnClickListener(this);
findViewById(R.id.b9).setOnClickListener(this);
findViewById(R.id.buttonClear).setOnClickListener(this);
Runnable runnable = new Runnable() {
#Override
public void run() {
{
valoare = Double.parseDouble(String.valueOf(mCalculatorDisplay.getText()));
genereaza_imagini();
check_answer();
}
}
};
mHandler.post(runnable);
//genereaza_imagini dinamic de la una la 9 imagini de un anumit tip
}
//max poate fi 5 sau 9
public void genereaza_imagini() {
final TextView et1, et2;
et1 = (TextView) findViewById(R.id.printfirst);
et2 = (TextView) findViewById(R.id.printsecond);
int nr1 = random(5);
printsecond = random(9);
printfirst = idMultiple[nr1 - 1];
produs = printsecond * printfirst;
et1.setTextSize(13);
et2.setTextSize(13);
et1.setText("nr piese lipsa:" + printfirst);
et2.setText("nr jucarii:" + printsecond);
et1.setVisibility(View.VISIBLE);
et2.setVisibility(View.VISIBLE);
ImageView imb1 = (ImageView) findViewById(R.id.ImageView1);
ImageView imb2 = (ImageView) findViewById(R.id.ImageView2);
ImageView imb3 = (ImageView) findViewById(R.id.ImageView3);
ImageView imb4 = (ImageView) findViewById(R.id.ImageView4);
ImageView imb5 = (ImageView) findViewById(R.id.ImageView5);
ImageView imb6 = (ImageView) findViewById(R.id.ImageView6);
ImageView imb7 = (ImageView) findViewById(R.id.ImageView7);
ImageView imb8 = (ImageView) findViewById(R.id.ImageView8);
ImageView imb9 = (ImageView) findViewById(R.id.ImageView9);
final ImageView[] imb_items = new ImageView[]{imb1, imb2, imb3, imb4, imb5, imb6, imb7, imb8, imb9};
for (int j = 0; j < 9; j++) {
RelativeLayout.LayoutParams lp;
lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
int dpHeight = (int) (displaymetrics.heightPixels / displaymetrics.density);
int dpWidth = (int) (displaymetrics.widthPixels / displaymetrics.density);
lp.leftMargin = dpWidth / 2 - dpWidth / 10;
lp.topMargin = dpHeight / 4;
imb_items[j].setLayoutParams(lp);
imb_items[j].getLayoutParams().height = 30;
imb_items[j].getLayoutParams().width = 30;
imb_items[j].setImageResource(0);
imb_items[j].setVisibility(View.GONE);
}
final int i = printsecond; // cate imagini din cele 9
if (nr1 == 1) {
Toast.makeText(getApplicationContext(), "Omizilor le lipsesc 6 picioare.",
Toast.LENGTH_SHORT).show();
for (int j = 0; j < i; j++) {
imb_items[j].setImageResource(R.drawable.obj1);
imb_items[j].setVisibility(View.VISIBLE);
}
}
if (nr1 == 2) {
Toast.makeText(getApplicationContext(), "Florilor le lipsesc 5 petale. ",
Toast.LENGTH_SHORT).show();
for (int j = 0; j < i; j++) {
imb_items[j].setImageResource(R.drawable.obj2);
imb_items[j].setVisibility(View.VISIBLE);
}
}
if (nr1 == 3) {
Toast.makeText(getApplicationContext(), "Papusilor le lipsesc 3 nasturi. ",
Toast.LENGTH_SHORT).show();
for (int j = 0; j < i; j++) {
imb_items[j].setImageResource(R.drawable.obj3);
imb_items[j].setVisibility(View.VISIBLE);
}
}
if (nr1 == 4) {
Toast.makeText(getApplicationContext(), "Masinilor le lipsesc 4 roti. ",
Toast.LENGTH_SHORT).show();
for (int j = 0; j < i; j++) {
imb_items[j].setImageResource(R.drawable.obj4);
imb_items[j].setVisibility(View.VISIBLE);
}
}
if (nr1 == 5) {
Toast.makeText(getApplicationContext(), "Robotilor le lipsesc 2 antene. ",
Toast.LENGTH_SHORT).show();
for (int j = 0; j < i; j++) {
imb_items[j].setImageResource(R.drawable.obj5);
imb_items[j].setVisibility(View.VISIBLE);
}
}
final Animation translateAnim = AnimationUtils.loadAnimation(MyActivity.this, R.anim.translate);
Thread thread = new Thread() {
public void run() {
while (true) {
try {
for(int j=0; j<i;j++){
imb_items[j].startAnimation(translateAnim); ???????????????????????????????????????????
Thread.sleep(2000);
}
} catch (InterruptedException e) {
Log.v( "tag", "local Thread error");
}
}
}
};
thread.start();
}
public int random(int max) {
Random r = new Random();
int random = r.nextInt(max) + 1;
return random;
}
public void check_answer() {
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
valoare = Double.parseDouble(String.valueOf(mCalculatorDisplay.getText()));
verifica();
}
});
}
//functie care afiseaza la onCreate, cand apas pe buton 2: nr1, nr2, raspuns corect nr1*nr2
public void verifica() {
final TextView et1, et2;
et1 = (TextView) findViewById(R.id.printfirst);
et2 = (TextView) findViewById(R.id.printsecond);
TextView Scor = (TextView) findViewById(R.id.Scorul);
if (produs == valoare) {
Toast.makeText(getApplicationContext(), "Raspunsul este corect! :)",
Toast.LENGTH_SHORT).show();
Log.v("Produsul este corect=", String.valueOf(produs));
scor = scor + 50;
Scor.setText("Scor:" + scor);
} else {
Toast.makeText(getApplicationContext(), "Raspunsul este gresit! :(",
Toast.LENGTH_SHORT).show();
Log.v("Produsul nu este corect=", String.valueOf(produs));
scor = scor - 50;
Scor.setText("Scor:" + scor);
}
// ca sa actualizeze automat text view-ul raspuns user la 0.0
if (userIsInTheMiddleOfTypingANumber) {
mCalculatorBrain.setOperand(Double.parseDouble(mCalculatorDisplay.getText().toString()));
userIsInTheMiddleOfTypingANumber = false;
}
mCalculatorBrain.performOperation("C");
mCalculatorDisplay.setText(df.format(mCalculatorBrain.getResult()));
genereaza_imagini();
}
// mCalculatorDisplay.setText(df.format(mCalculatorBrain.getResult()));
// }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
String buttonPressed = ((Button) v).getText().toString();
if (DIGITS.contains(buttonPressed)) {
// digit was pressed
if (userIsInTheMiddleOfTypingANumber) {
if (buttonPressed.equals(".") && mCalculatorDisplay.getText().toString().contains(".")) {
// ERROR PREVENTION
// Eliminate entering multiple decimals
} else {
mCalculatorDisplay.append(buttonPressed);
}
} else {
if (buttonPressed.equals(".")) {
// ERROR PREVENTION
// This will avoid error if only the decimal is hit before an operator, by placing a leading zero
// before the decimal
mCalculatorDisplay.setText(0 + buttonPressed);
} else {
mCalculatorDisplay.setText(buttonPressed);
}
userIsInTheMiddleOfTypingANumber = true;
}
} else {
// operation was pressed
if (userIsInTheMiddleOfTypingANumber) {
mCalculatorBrain.setOperand(Double.parseDouble(mCalculatorDisplay.getText().toString()));
userIsInTheMiddleOfTypingANumber = false;
}
mCalculatorBrain.performOperation(buttonPressed);
mCalculatorDisplay.setText(df.format(mCalculatorBrain.getResult()));
}
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save variables on screen orientation change
outState.putDouble("OPERAND", mCalculatorBrain.getResult());
outState.putDouble("MEMORY", mCalculatorBrain.getMemory());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore variables on screen orientation change
mCalculatorBrain.setOperand(savedInstanceState.getDouble("OPERAND"));
mCalculatorBrain.setMemory(savedInstanceState.getDouble("MEMORY"));
mCalculatorDisplay.setText(df.format(mCalculatorBrain.getResult()));
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/banda_bk"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
tools:context=".MyActivity">
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/rel_banda">
<ImageView
android:id="#+id/banda"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/banda" />
<ImageView
android:id="#+id/ImageView1"
android:layout_marginRight="92dp"
android:layout_marginBottom="52dp"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_toLeftOf="#+id/linearLayout4"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/ImageView2"
android:layout_alignLeft="#+id/ImageView1"
android:layout_alignStart="#+id/ImageView1"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/ImageView3"
android:layout_marginRight="41dp"
android:layout_toLeftOf="#+id/ImageView2"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/ImageView4"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/ImageView5"
android:layout_marginTop="25dp"
android:layout_below="#+id/ImageView4"
android:layout_alignLeft="#+id/ImageView4"
android:layout_alignStart="#+id/ImageView4"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/ImageView6"
android:layout_alignTop="#+id/ImageView5"
android:layout_alignLeft="#+id/ImageView3"
android:layout_alignStart="#+id/ImageView3"
android:layout_marginTop="14dp"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/ImageView7"
android:layout_alignTop="#+id/ImageView6"
android:layout_toRightOf="#+id/ImageView6"
android:layout_marginLeft="22dp"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/ImageView8"
android:layout_alignTop="#+id/ImageView7"
android:layout_toRightOf="#+id/ImageView2"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/ImageView9"
android:layout_alignTop="#+id/ImageView8"
android:layout_toRightOf="#+id/ImageView8"
android:layout_marginLeft="26dp"
android:visibility="gone"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:id="#+id/row1"
android:layout_width="600dp"
android:layout_height="wrap_content"
android:gravity="bottom">
<Button
android:id="#+id/b0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".15"
android:text="#string/b0"
android:textSize="12sp" />
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".15"
android:text="#string/b1"
android:textSize="12sp"
android:visibility="visible" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".15"
android:text="#string/b2"
android:textSize="12sp"
android:visibility="visible" />
<Button
android:id="#+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".15"
android:text="#string/b3"
android:textSize="12sp"
android:visibility="visible" />
<Button
android:id="#+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".15"
android:text="#string/b4"
android:textSize="12sp"
android:visibility="visible" />
<Button
android:id="#+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".15"
android:text="#string/b5"
android:textSize="12sp"
android:visibility="visible" />
<Button
android:id="#+id/b6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".15"
android:text="#string/b6"
android:textSize="12sp"
android:visibility="visible" />
<Button
android:id="#+id/b7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".15"
android:text="#string/b7"
android:textSize="12sp"
android:visibility="visible" />
<Button
android:id="#+id/b8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".15"
android:text="#string/b8"
android:textSize="12sp"
android:visibility="visible" />
<Button
android:id="#+id/b9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".15"
android:text="#string/b9"
android:textSize="12sp"
android:visibility="visible" />
<Button
android:id="#+id/buttonClear"
android:layout_width="33dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="#string/buttonClear"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="33dp">
<TextView
android:id="#+id/printfirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:id="#+id/printsecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:id="#+id/Raspuns_user"
android:layout_width="100dp"
android:layout_weight=".15"
android:maxLines="1"
android:text="0"
android:textColor="#color/textbody"
android:textSize="15sp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="60dp"
android:layout_height="30dp"
android:textSize="10sp"
android:text="Check" />
<TextView
android:id="#+id/Scorul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scor"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/textbody"
android:textSize="10sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Ajutati spiridusii sa afle numarul total de piese lipsa. "
android:id="#+id/textView"
android:textSize="13dp"
android:textColor="#color/textbody"
android:layout_alignParentTop="true" />
</RelativeLayout>
Try this:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// YOUR CODE HERE
}
}, 2000);
Where 2000 is 2 seconds delay/sleep in main thread.
You can use thread concept and in loop you can use sleep function for delay and you can write your code in it
thread = new Thread() {
public void run() {
while (true) {
try {
YOUR FOR LOOP CODE HERE with this line "Thread.sleep(2000);"
} catch (InterruptedException e) {
Log.e(TAG, "local Thread error", e);
}
}
}
};
thread.start();
For create delay, you can use this code:
try {
Thread.sleep(m); // m is millisecond, if m = 1000 ms then delay = 1 s
}
catch (InterruptedException e) {
e.printStackTrace();
}
If I use this
for(int j=0; j<i;j++) {
final int finalJ = j;
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
imb_items[finalJ].startAnimation(translateAnim);
Log.i("tag", "This'll run 5000 milliseconds later");
}
},
5000 * j
);
}
the problem is that when when the second image starts moving, the first one stops and disappears. I would like all the images to continue their movement.
This is the final solution:
for(int j=0; j<i;j++) {
translateAnim[j].setStartOffset(5000*j);
imb_items[j].startAnimation(translateAnim[j]);
}

how to make Footer buttons appears while dragging screen down in android like in facebook app

I wanna make footer buttons in android one way is to simply make buttons and align them to bottom but I want the footer like in Facebook android app whenever we drag screen down three buttons appears for status , photo , checkin.
How to do this ??
To get a list like the following image, create a layout.xml as follows after the sample image
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/header" >
<LinearLayout
android:id="#+id/BtnSlide"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="#drawable/button_bg_drawable" >
<ImageView
android:id="#+id/imageView0"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center_vertical|left"
android:background="#drawable/button_bg_drawable"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:src="#drawable/back_btn_small" />
</LinearLayout>
<EditText
android:id="#+id/headerText"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toRightOf="#id/BtnSlide"
android:background="#android:drawable/editbox_background_normal"
android:editable="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="normal" />
<AutoCompleteTextView
android:id="#+id/filterNewProject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:layout_toRightOf="#id/BtnSlide"
android:background="#drawable/bg_input_blue"
android:completionThreshold="1"
android:hint="Search for a locality, developer or project"
android:textColor="#color/black"
android:textSize="14sp"
android:visibility="gone" />
<Button
android:id="#+id/clearAutoCompleteList"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/custom_button_clear"
android:paddingLeft="20dp"
android:visibility="gone" />
<Button
android:id="#+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#drawable/seaerch_glass" />
<View
android:id="#+id/sep_header"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#id/tabBar"
android:background="#d5d5d5"
android:visibility="visible" />
</RelativeLayout>
<include
android:id="#+id/footerLayout"
layout="#layout/post_requirement_footer"
android:visibility="gone" />
<ListView
android:id="#+id/projectsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout"
android:divider="#color/white"
android:dividerHeight="1.5dp" >
</ListView>
<RelativeLayout
android:id="#+id/zeroResultsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/footerLayout"
android:layout_below="#id/headerLayout"
android:visibility="gone" >
<ImageView
android:id="#+id/emptyIllustration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/no_results_illustration" />
</RelativeLayout>
<com.housing.utils.QuickReturnRelativeLayoutFooter
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:background="#color/transparent" >
<RelativeLayout
android:id="#+id/bottomListViewContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#drawable/footer_bg" >
<ImageButton
android:id="#+id/filterButtonFooter"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/button_bg_drawable"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/filter_icon" />
<ImageButton
android:id="#+id/subscribeButtonFooter"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/button_bg_drawable"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/subscribe_iphone"
android:visibility="visible" />
<TextView
android:id="#+id/resultsText"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ellipsize="end"
android:paddingTop="10dp"
android:scrollHorizontally="false"
android:singleLine="false"
android:text=""
android:textColor="#color/black"
android:textSize="15dp"
android:visibility="visible" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/resultsText"
android:paddingRight="5dp"
android:paddingTop="15dp"
android:src="#drawable/filter_normal"
android:visibility="gone" />
</RelativeLayout>
</com.housing.utils.QuickReturnRelativeLayoutFooter>
</RelativeLayout>
Here is the Class com.housing.utils.QuickReturnRelativeLayoutFooter
PS : Replace com.housing.utils with your package name
package com.housing.utils;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.RelativeLayout;
public class QuickReturnRelativeLayoutFooter extends RelativeLayout implements
OnScrollListener {
private class ChildDescriptor {
public int index;
public int total;
public int drawable;
public ChildDescriptor(int index) {
this.index = index;
}
public int getApproximateScrollPosition() {
return index * total + (total - drawable);
}
}
public int MAX_HEIGHT_DP =60;
public int MIN_HEIGHT_DP = 0;
public static final int SCROLL_DIRECTION_INVALID = 0;
public static final int SCROLL_DIRECTION_UP = 1;
public static final int SCROLL_DIRECTION_DOWN = 2;
private int direction = SCROLL_DIRECTION_INVALID;
private ChildDescriptor lastchild;
private OnScrollListener onscrolllistener;
private int maxheight;
private int minheight;
public QuickReturnRelativeLayoutFooter(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
calculateMinMax();
}
public QuickReturnRelativeLayoutFooter(Context context, AttributeSet attrs) {
super(context, attrs);
calculateMinMax();
}
public QuickReturnRelativeLayoutFooter(Context context) {
super(context);
calculateMinMax();
}
private void calculateMinMax() {
DisplayMetrics metrics = getResources().getDisplayMetrics();
maxheight = (int) (metrics.density * (float) MAX_HEIGHT_DP);
minheight = (int) (metrics.density * (float) MIN_HEIGHT_DP);
}
private void adjustHeight(int howmuch, int max, int min) {
if ((howmuch < 0) && (direction != SCROLL_DIRECTION_UP)) {
direction = SCROLL_DIRECTION_UP;
return;
} else if ((howmuch > 20) && (direction != SCROLL_DIRECTION_DOWN)) {
direction = SCROLL_DIRECTION_DOWN;
return;
}
int current = getHeight();
current += howmuch;
if (current < min) {
current = min;
} else if (current > max) {
current = max;
}
RelativeLayout.LayoutParams f = (RelativeLayout.LayoutParams) getLayoutParams();
if (f.height != current) {
f.height = current;
setLayoutParams(f);
}
if (direction == SCROLL_DIRECTION_UP
&& Math.abs(f.topMargin) <= current) {
// if (f.topMargin != howmuch) {
//
// f.topMargin = howmuch + f.topMargin;
//
// if (f.topMargin > 0) {
// f.topMargin = -f.topMargin - 10;
// }
//
// }
// mBottomListViewContainer.setVisibility(View.GONE);
f.bottomMargin = -100;
setLayoutParams(f);
} else if (direction == SCROLL_DIRECTION_DOWN) {
if (f.bottomMargin != 0) {
f.bottomMargin = 0;
setLayoutParams(f);
}
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
ChildDescriptor currentchild = getFirstChildItemDescriptor(view,
firstVisibleItem);
try {
adjustHeight(lastchild.getApproximateScrollPosition()
- currentchild.getApproximateScrollPosition(), maxheight,
minheight);
lastchild = currentchild;
} catch (NullPointerException e) {
lastchild = currentchild;
} catch (Exception e) {
}
if (onscrolllistener != null) {
onscrolllistener.onScroll(view, firstVisibleItem, visibleItemCount,
totalItemCount);
}
}
private ChildDescriptor getFirstChildItemDescriptor(AbsListView view,
int index) {
ChildDescriptor h = new ChildDescriptor(index);
try {
Rect r = new Rect();
View child = view.getChildAt(0);
child.getDrawingRect(r);
h.total = r.height();
view.getChildVisibleRect(child, r, null);
h.drawable = r.height();
return h;
} catch (Exception e) {
}
return null;
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (onscrolllistener != null) {
onscrolllistener.onScrollStateChanged(view, scrollState);
}
}
public void attach(AbsListView view) {
view.setOnScrollListener(this);
}
public void setOnScrollListener(OnScrollListener l) {
onscrolllistener = l;
}
}
I Have made it as a widget and Now finally to add this to your List View use
frame = (QuickReturnRelativeLayoutFooter) newProjectsView
.findViewById(R.id.frame);
frame.attach(projectsList);
where projectList is your List View
example
ListView projectList =(ListView)findViewById(R.id.projectList);
and Voila Cheers Completed Smooth as Heaven .......

Categories

Resources