change background of views on hover inside dialog - android

im trying to build share dialog.
in this share dialog I have facebook,whatsapp,mail and more.
im trying the make the background of every view change on touch to indicate the toch.
so far no problem.
the problem is that I also want to let the user move is finger into other option and when he do that the previous background return to his original color and the new view background changes.
i just cant get the hover event at all, and I couldn't trigger other onTouchEvent as long as the first one is still alive.
this is my code so far:
public class customDialogFragment1 extends DialogFragment {
public customDialogFragment1() {
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
postShareUrl=getActivity().getResources().getString(R.string.servicePostShareUrl);
id=getArguments().getString("body");
postTitle=getArguments().getString("subject");
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.share_title_layout, null);
// Set title divider color
TextView txtTitle= (TextView) v.findViewById(R.id.share_title);
Typeface tf = Typeface.createFromAsset(getActivity().getResources().getAssets(),
"fonts/OpenSansHebrew-Bold.ttf");
txtTitle.setTypeface(tf);
View layout=inflater.inflate(R.layout.custom_share_layout_inner,null);
builder.setView(layout);
ImageView facebook= (ImageView) layout.findViewById(R.id.imgFaceebook);
ImageView whatsapp= (ImageView) layout.findViewById(R.id.imgWhatsapp);
ImageView more= (ImageView) layout.findViewById(R.id.imgMore);
ImageView mail= (ImageView) layout.findViewById(R.id.imgMail);
facebook.setOnClickListener(imageClickListener);
whatsapp.setOnClickListener(imageClickListener);
more.setOnClickListener(imageClickListener);
mail.setOnClickListener(imageClickListener);
List activities = getActivity().getPackageManager().queryIntentActivities(sendIntent, 0);
Context context=(Activity)getActivity();
for(int i=0;i<activities.size();i++) {
ResolveInfo appPacageName = (ResolveInfo) activities.get(i);
Log.i("pacageName", appPacageName.toString());
if (appPacageName.toString().contains("com.facebook.composer")) {
shareCheckList[0] = appPacageName;
} else if (appPacageName.toString().contains("whatsapp")) {
shareCheckList[1] = appPacageName;
} else if (appPacageName.toString().contains("mail")) {
shareCheckList[2] = appPacageName;
}
}
dialog.setContentView(layout);
Window window = dialog.getWindow();
window.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
window.setLayout(MainActivity.screenWidth-90,350);
return dialog;
}
and this is my ontouch listener :
private View.OnClickListener imageClickListener=new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT,getArguments().getString("subject"));
intent.putExtra(Intent.EXTRA_TEXT,getArguments().getString("body"));
switch (v.getId()){
case R.id.imgFaceebook:
if(!shareCheckList[0].toString().isEmpty()){
intent.setClassName(shareCheckList[0].activityInfo.packageName, shareCheckList[0].activityInfo.name);
((Activity)getActivity()).startActivity(intent);
break;
case R.id.imgWhatsapp:
intent.setClassName(shareCheckList[1].activityInfo.packageName, shareCheckList[1].activityInfo.name);
((Activity)getActivity()).startActivity(intent);
}
break;
case R.id.imgMail:
try{
intent.setClassName(shareCheckList[2].activityInfo.packageName, shareCheckList[2].activityInfo.name);
((Activity)getActivity()).startActivity(intent);
break;
case R.id.imgMore:
CustomDialogFragment2 cdf=new CustomDialogFragment2();
Bundle bundle = new Bundle();
bundle.putString("body",id);
bundle.putString("subject", postTitle);
cdf.setArguments(bundle);
cdf.show(getActivity().getFragmentManager(), "customDialogFragment2");
break;
}
}
};

If I understood right, you should use state list:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Just create xml drawable file for every ImageView, and set for each state item with source you need.

Related

Undo Button for a WordSearch app

I'm new to android and I'm having a hard time finding solutions to my problem on my app.
My app is a wordsearch game that uses tapping on the tiles as an input. This is the code for the onClick() of the dynamic textviews on the tablelayout:
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
w.setVisibility(View.VISIBLE);
//change the color of tapped textview
text.setTextColor(Color.WHITE);
text.setBackgroundColor(Color.parseColor(mColors[randomNum]));
String b = text.getText().toString();
uTxt.setText(""+uTxt.getText().toString() + b);
//check if answer is in the word grid
if(checkAns(uTxt, list))
{
w.setVisibility(View.GONE);
wC.setText(String.valueOf(Integer.parseInt(wC.getText()+"")-1));
if(Integer.parseInt(wC.getText()+"") == 0){
int newM = minutes*60 + seconds;
dataHelper.insertData(pNameC.getText().toString(), newM, currentDateandTime, Category.leve);
t.cancel();
Context mContext = getApplicationContext();
Activity mActivity = GameScreen.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.gameover,null);
// Initialize a new instance of popup window
PopupWindow mPopupWindow = new PopupWindow(
customView,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
Typeface font = Typeface.createFromAsset(getAssets(), "raw2.ttf");
TextView cattxt = (TextView)customView.findViewById(R.id.catTxt);
String ctg = ti.getText().toString();
cattxt.setTypeface(font);
cattxt.setText(ctg);
Button yesB = (Button) customView.findViewById(R.id.maglaro2);
yesB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GameScreen.this, Category.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
GameScreen.this.finish();
}
});
Button noB = (Button) customView.findViewById(R.id.hindi);
noB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(GameScreen.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
GameScreen.this.finish();
}
});
mPopupWindow.showAtLocation(table, Gravity.CENTER,0,0);
}
uTxt.setText("");
}
}
});
Now my problem is I want an UNDO Button that will delete the last character on the uTxt and will change back the color of the last touched textView
Does anyone have any ideas on how to do that?
If yes leave a comment, answer, or suggestion below. TIA!
Typical solution for this problem is the usage of the command pattern (excellent for undo redo functionality).
See https://en.wikipedia.org/wiki/Command_pattern

Checking a Button exists in Android

Need some advice again.
I am 99% done with this module I'm working on, although I'm stuck at the last hurdle. I'm dynamically publishing a button on the screen at runtime. This button will take it to a new activity view when pressed. I got that working perfectly.
However, I have another button which randomly changes the view so it effectively needs to reset itself, if that makes sense. What's happening is each time the button is clicked (the dynamic one) it then adds another button to the stack. Effectively I have buttons each time I click running down the screen. Is my logic wrong or is there a way to check and prevent the button to show each time? i.e. Just once...Below is the code.
public void ButtonOnClick(View v) {
Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;
switch (randomListIndex) {
case 0:
//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);
button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");
Button myButton = new Button(this);
myButton.setText("Press Me");
LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(myButton);
final Button myButton1 = myButton;
myButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);
}
});
break;
}
}
EDIT: How about this? Add a new class field member (a class variable, not a method's local variable) that indicates whether the button was actually added in the layout.
private boolean buttonShown = false; /* Here changed */
public void ButtonOnClick(View v) {
Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;
switch (randomListIndex) {
case 0:
//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);
button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");
if (buttonShown == false) { /* Here changed */
Button myButton = new Button(this);
myButton.setText("Press Me");
LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(myButton);
final Button myButton1 = myButton;
myButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);
}
});
buttonShown = true; /* Here changed */
} /* Here changed */
break;
}
}
Edited again: Instead of local variable myButton, I used class field member pressMeButton.
private Button pressMeButton;
public void ButtonOnClick(View v) {
Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;
switch (randomListIndex) {
case 0:
//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);
button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");
if (pressMeButton == null) {
pressMeButton = new Button(this);
pressMeButton.setText("Press Me");
LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(pressMeButton);
}
/* If the pressMeButton is already in the layout, all you need to do is just changing the onClickListener. */
pressMeButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);
}
});
break;
}
}
Your code is fine but the way it is it will always add a button to the screen. Whats the desired behavior your looking for? If its a conditional thing than you need to account for that in the onclick call.
EDIT:
So what you need to do then is to create some kind of flag or id for the button and set it as the button's view.tag like this:
Button b = new Button(context);
b.setTag(flagTag);
Then when you want to check if the button exists you check it like:
if((LinearLayout) findViewById(R.id.nextPageContainer))
.findViewById(tagFlag)==null){
Button b = new Button(context);
b.setTag(flagTag);
}else{
//do nothing or something :p
}

Dismiss dialog in android

I have a imageView in onCreate method where I am using method showList. This is code:
action_bar_log_in_icon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
allResults.clear();
allResults.add(action_bar_log_in_icon);
allResults.add(action_bar_data_privacy_icon);
allResults.add(action_bar_vCard_icon);
allResults.add(action_bar_email_icon);
showList();
}
});
and code method showList:
private void showList() {
final ListDialog listDialog = new ListDialog(this);
listDialog.setCanceledOnTouchOutside(true);
final ListView lv = (ListView) listDialog
.findViewById(R.id.spinnerlist);
if(lAdapter == null){
lAdapter = new ListAdapter(getBaseContext(),
R.layout.spinner_item,
(ArrayList<ImageView>) allResults);
}
lv.setAdapter(lAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch(position){
case 0:
intent = new Intent(ActionBarMain2.this, LoginActivity.class);
startActivity(intent);
break;
case 1:
intent = new Intent(ActionBarMain2.this, LoginActivity.class);
startActivity(intent);
break;
case 2:
intent = new Intent(ActionBarMain2.this, vCard_view.class);
startActivity(intent);
break;
case 3:
intent = new Intent(ActionBarMain2.this, EmailView.class);
startActivity(intent);
break;
}
listDialog.dismiss();
}
});
WindowManager.LayoutParams WMLP = listDialog.getWindow()
.getAttributes();
WMLP.gravity = Gravity.TOP | Gravity.RIGHT;
WMLP.y = 50;
WMLP.width = 100;
listDialog.getWindow().setAttributes(WMLP);
listDialog.show();
}
This solution work and if I click on imageview my list is showing, but problem is when I click again on the same imageview. List is still visible. How I can do when I click first time my list is showing and when I click again my list dismiss etc?
What if you create your ListDialog just once in your Activity's onCreate not showing it yet.Then upon clicking your ImageView you can do if(dialog.isVisible()) { dialog.show(); } else { dialog.dismiss(); }
set some flag for capturing the state of the dialog. Then set condition on that flag..
If its visible then set dismissDialog(dialogName);
I find solution my problem. This dialog cover my imageView, so I placed dialog a little lower. Thank you for advices.

Android PopupWindow button disappears after click

I'm having a very frustrating problem with PopupWindow on Android.
I've implemented my own class that inherits PopupWindow and implements
OnClickListener.
After adding button backgrounds with custom selectors problems start.
This background keeps disappearing after clicking the button (starting new activity and dismissing the popup).
It doesn't disappear after "focus and click", only after "quick click".
Any idea/suggestion would be very appriciated!
public class TestPopup extends PopupWindow implements OnClickListener
protected LayoutInflater inflater;
protected Activity caller;
protected View popup;
protected View layout;
public TestPopup(Activity activity) {
super(activity);
popup = inflater.inflate(R.layout.popup, (ViewGroup) caller.findViewById(R.id.contentLayout));
layout = popup.findViewById(R.id.layout);
popup.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Display display = caller.getWindowManager().getDefaultDisplay();
setHeight(display.getHeight());
setWidth(display.getWidth());
setFocusable(true);
setContentView(popup);
// fix to allow Popup to be clickable!
setBackgroundDrawable(new BitmapDrawable());
popup.setOnClickListener(this);
popup.findViewById(R.id.addButton).setOnClickListener(this);
popup.findViewById(R.id.deleteButton).setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = null;
if (v.getId() == R.id.addButton) {
intent = new Intent(caller, AddActivity.class);
intent.putExtra(AddActivity.ACTION_ADD, true);
} else if (v.getId() == R.id.deleteButton) {
intent = new Intent(caller, AddActivity.class);
intent.putExtra(AddActivity.ACTION_DELETE, true);
}
if (intent != null) {
caller.startActivity(intent);
}
TestPopup.this.dismiss();
}
One solution is to call popup.invalidate(); before dismissing the popup.

Set imageview from array reference?

I'll keep this simple. I want to set an ImageView in an AlertDialog to an image in an array of Drawables The image that I would like to set to the ImageView can be retrieved by accessing the mImages[position].
Here is a brief explanation (complete explanation below):
In short- I need a way to pass an image from my main activity to a dialog then on Cancel dismiss the dialog and on confirm set the system wallpaper (to the image passed from the main activity) then finish(); the activity.
Here's a complete explanation:
The user is presented with a Gallery and an ImageView above the Gallery that shows a larger preview of the image that has focus in the Gallery.
The images displayed in the Gallery are setup using:
// setup wallpaper array
private void findWallpapers() {
mThumbs = new ArrayList<Integer>(24);
mImages = new ArrayList<Integer>(24);
final Resources resources = getResources();
final String packageName = getApplication().getPackageName();
addWallpapers(resources, packageName, R.array.wallpapers);
}
// setup array defining all wallpapers & define thumbnails
private void addWallpapers(Resources resources, String packageName, int list) {
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
final int thumbRes = resources.getIdentifier(extra + "_small",
"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
mImages.add(res);
}
}
}
}
After "Set Wallpaper" Button is pressed, an AlertDialog should open with another preview of the image that had focus in the Gallery. The AlertDialog will contain a TextView with instructions, the preview of the image we are proposing to set as the wallpaper, an "Okay" Button and a "Cancel" Button. Pressing the "Okay" Button will set the image from the ImageView preview as the system wallpaper via an InputStream.
Thanks again!
private void selectWallpaper(int position) {
Toast.makeText(getBaseContext(), "Select Wallpaper", Toast.LENGTH_SHORT)
.show();
if (mIsWallpaperSet) {
return;
}
mIsWallpaperSet = true;
Context context = this;
// CharSequence text = "Wallpaper Set!";
// int duration = Toast.LENGTH_LONG;
InputStream stream = getResources().openRawResource(
mImages.get(position));
final Dialog accept = new Dialog(context);
accept.setContentView(R.layout.confirm);
accept.setTitle("Please Confirm");
TextView instructions = (TextView) accept.findViewById(R.id.textView1);
instructions.setText("Would you like to set this as your wallpaper?");
ImageView wallpreview = (ImageView) accept
.findViewById(R.id.imageView1);
wallpreview.createFromStream(stream, "test");
// SETUP cancel (no btn) listener
Button cancelbtn = (Button) accept.findViewById(R.id.button2);
cancelbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
accept.dismiss();
}
});
// SETUP Yes Btn listener
Button okbtn = (Button) accept.findViewById(R.id.button1);
okbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// dismiss being used as placeholder, actually setting wallpaper
// will be added
accept.dismiss();
}
});
accept.show();
}
{
;
}
You can use the layout inflator to inflate your alert dialog layout and assign it to the View of the alert dialog in your main activity. You can use findViewById() to set the onClickListeners listeners for those buttons. That will ensure that your onClickListener can see mImages.
final ArrayList<Integer> mImages=new ArrayList<Integer>();
final ImageView v=findViewById(R.id.imageView);
AlertDialog d=new AlertDialog.Builder(Main.this).create();
d.setView(getLayoutInflater().inflate(R.layout.dialogView,findViewById(R.id.ROOT_LAYOUT_IN_XML_ID),false));
((ImageView)d.findViewById(R.id.YOUR_VIEW_ID)).setImageResource(mImages.get(?));
((Button)d.findViewById(R.id.cancelButton)).setOnClickListener(new OnClickListener()
{
d.dismiss();
});
((Button)d.findViewById(R.id.acceptButton)).setOnClickListener(new OnClickListener()
{
v.setImageResource(mImages.get(?));
d.dismiss();
});

Categories

Resources