Created
July 29, 2014 19:02
-
-
Save sketchpunk/942db47f81e4e4bbd100 to your computer and use it in GitHub Desktop.
Android Rating and Comment PopupWindow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> | |
<EditText android:id="@+id/txtText" android:hint="Enter a comment and select a rating" | |
android:layout_width="fill_parent" android:layout_height="fill_parent" | |
android:inputType="textMultiLine" android:ems="10" | |
android:gravity="top|left" android:padding="10dp" | |
android:background="#00000000"/> | |
<LinearLayout android:orientation="horizontal" android:layout_gravity="bottom|left" | |
android:layout_marginBottom="4dp" android:layout_marginLeft="7dp" | |
android:layout_width="wrap_content" android:layout_height="wrap_content"> | |
<ImageView android:id="@+id/ivStar01" android:src="@android:drawable/btn_star_big_off" android:layout_width="wrap_content" android:layout_height="wrap_content"/> | |
<ImageView android:id="@+id/ivStar02" android:src="@android:drawable/btn_star_big_off" android:layout_width="wrap_content" android:layout_height="wrap_content"/> | |
<ImageView android:id="@+id/ivStar03" android:src="@android:drawable/btn_star_big_off" android:layout_width="wrap_content" android:layout_height="wrap_content"/> | |
<ImageView android:id="@+id/ivStar04" android:src="@android:drawable/btn_star_big_off" android:layout_width="wrap_content" android:layout_height="wrap_content"/> | |
<ImageView android:id="@+id/ivStar05" android:src="@android:drawable/btn_star_big_off" android:layout_width="wrap_content" android:layout_height="wrap_content"/> | |
</LinearLayout> | |
<Button android:id="@+id/btnSubmit" | |
android:text="Send" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="0dp" | |
android:layout_marginRight="7dp" | |
android:layout_gravity="bottom|right" | |
android:background="@android:color/transparent" | |
android:drawableRight="@android:drawable/ic_menu_send"/> | |
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sketchpunk; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import android.widget.PopupWindow; | |
import android.widget.TextView; | |
import com.sketchpunk.R; | |
import com.sketchpunk.shared.ui.Toasty; | |
public class PopWindowRating{ | |
public static class UITag{ | |
public TextView textView = null; | |
public ImageView[] aryStars = null; | |
public PopupWindow popWindow = null; | |
public int rating = 0; | |
}//func | |
public static PopupWindow create(Activity act){ | |
LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
View layout = inflater.inflate(R.layout.popwin_rating,null); | |
View decor = act.getWindow().getDecorView(); | |
int h = decor.getHeight(); | |
int w = decor.getWidth(); | |
//------------------------------------------------------ | |
PopupWindow pwin = new PopupWindow(layout,Math.min(400,w-20), h-20, true); | |
pwin.setBackgroundDrawable(act.getResources().getDrawable(R.drawable.ufi_popover)); | |
pwin.setAnimationStyle(R.style.popWin_RighSlide); | |
//pwin.showAtLocation(layout, Gravity.CENTER, 0, 0); | |
//------------------------------------------------------ | |
UITag tag = new UITag(); | |
tag.popWindow = pwin; | |
tag.textView = (TextView) layout.findViewById(R.id.txtText); | |
tag.aryStars = new ImageView[]{ | |
(ImageView) layout.findViewById(R.id.ivStar01), | |
(ImageView) layout.findViewById(R.id.ivStar02), | |
(ImageView) layout.findViewById(R.id.ivStar03), | |
(ImageView) layout.findViewById(R.id.ivStar04), | |
(ImageView) layout.findViewById(R.id.ivStar05) | |
}; | |
View.OnClickListener rateListener = new View.OnClickListener(){ | |
@Override public void onClick(View view){ | |
Object[] ary = (Object[])view.getTag(); | |
UITag tag = (UITag)ary[0]; | |
tag.rating = (int)ary[1]+1; | |
for(int i=0; i < tag.aryStars.length; i++){ | |
tag.aryStars[i].setImageResource( (i < tag.rating)? android.R.drawable.star_big_on : android.R.drawable.star_big_off ); | |
}//for | |
}//func | |
}; | |
int pos = 0; | |
for(ImageView iv : tag.aryStars){ | |
iv.setOnClickListener(rateListener); | |
iv.setTag(new Object[]{tag,pos}); | |
pos++; | |
}//for | |
//------------------------------------------------------ | |
Button btn = (Button) layout.findViewById(R.id.btnSubmit); | |
btn.setTag(tag); | |
btn.setOnClickListener(new View.OnClickListener() { | |
@Override public void onClick(View view){ | |
UITag tag = (UITag)view.getTag(); | |
String errMsg = null, txt = tag.textView.getText().toString(); | |
//......................... | |
if(txt.isEmpty()) errMsg = "Please enter a comment"; | |
if(tag.rating == 0) errMsg = "Please select a rating"; | |
if(errMsg != null){ | |
Toasty.center(errMsg, true); | |
return; | |
}//if | |
//......................... | |
System.out.println("Rating " + Integer.toString(tag.rating)); | |
System.out.println("Commtent " + txt); | |
tag.popWindow.dismiss(); | |
}//func | |
}); | |
return pwin; | |
}//func | |
}//cls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment