How to handle click on ScrollView in Fragment? - java

How to handle click on ScrollView in Fragment?
<FrameLayout 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"
tools:context=".ExampleFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myScroll">
</ScrollView>
</FrameLayout>
And in Fragment I'm trying:
#Override
public void onStart() {
super.onStart();
ScrollView refresh = (ScrollView) getActivity().findViewById(R.id.myScroll);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(getActivity().getApplicationContext(), "TEST", Toast.LENGTH_LONG);
toast.show();
}
});
}
But after clicking nothing happens.
Any other idea to handle a click on a fragment? This does not have to be a ScrollView. I tried also for FragmentLayout but it returns a lot of bugs.

The reason for the onClickListener of ScrollView being ignored is explained here.
You could add an onTouchListener instead like this.
refresh.setOnTouchListener(new View.OnTouchListener() {
/**
* Called when a touch event is dispatched to a view. This allows listeners to
* get a chance to respond before the target view.
*
* #param v The view the touch event has been dispatched to.
* #param event The MotionEvent object containing full information about
* the event.
* #return True if the listener has consumed the event, false otherwise.
*/
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast toast = Toast.makeText(getActivity().getApplicationContext(), "TEST", Toast.LENGTH_LONG);
toast.show();
return false;
}
});

use this code in onCreateView
ScrollView refresh = (ScrollView) getActivity().findViewById(R.id.myScroll);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(getActivity().getApplicationContext(), "TEST", Toast.LENGTH_LONG);
toast.show();
}
});

Related

SplashScreen skip on Touch event

I'm developing an android app, it has a Splash Screen that runs for 2500ms.
I want to add the functionality for a User touch screen and skip this Activity.
I could made it with a button, but for pretty objective I just want to add a screen touch listener (Don't know how.)
My SplashScreen:
public class Splash extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 2500;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashactivity);
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}, SPLASH_TIME_OUT);
//Skip this intro
RelativeLayout root_layout = (RelativeLayout) findViewById(R.id.root_splash);
root_layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
return true;
}
});
}
}
My splashactivity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash_screen">
</RelativeLayout>
Add a full screen view named ll_root in R.layout.splashactivity,then in Splash Activity using findViewById and setOnTouchListener for ll_root.
Delete the splash screen. That's so 2007.
Add onTouchListener on Splash screen layout and call your class on the event.
Hope it will work for you
Thanks..

Google Map flickers under alert dialog due to being compressed

I have implemented a dialog box on my marker info box click listener and the code logic is working fine, but when I show the dialog box the map is pushed up and flickers in the back ground. I tried it on a Pixel emulator as well as Galaxy S8 and both show the map as flickering behind the alert dialog box.
Here is the image and you can not really see it flickering but the text is distorted and flickering. It looks like its being compressed between the keyboard and top. The word ...calculating is on the bottom of the screen with no dialog so not sure why its at the top.
Any help would be appreciated. I did check for similar questions and what I think I am after is the keyboard simply overlaying the image not pushing it up.
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
showEditTextDialog();
}
});
public void showEditTextDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View dialogView = layoutInflater.inflate(R.layout.mymap_marker_alert_dialog, null);
final EditText editText = (EditText) dialogView.findViewById(R.id.alert_dialog_editText);
// Keyboard
final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
// Auto show keyboard
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean isFocused) {
if (isFocused)
{
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
});
builder.setView(dialogView)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String regNum = editText.getText().toString();
Log.d("STACKOVERFLOW", "Registration number: " + regNum);
// Hide keyboard
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Hide keyboard
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
SO it was the Adview that was trying to show in the compress screen area above the keyboard. I had to find a way to remove the adview while the keyboard was visible and then return it again on the keyboard was dismissed. In my case I had to ensure to re-enable the adview in the Alert Dialog onclick methods and the full solution is as follows:
Set up your LinearLayout and note the #id for both the LinearLayout and adView.
<TextView
android:id="#+id/mydistanceToMarker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:gravity="fill"
android:text="Tap to change map type."
android:textAlignment="center"
android:textSize="36sp"
android:textColor="#color/colorBlack"
android:textStyle="bold" />
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/myAdMapView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
ads:adUnitId="#string/UNIT_ID"
ads:adSize="BANNER"/>
</LinearLayout>
Connect the variable to the LinearLayout
private LinearLayout parentLinearLayout;
and
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);
parentLinearLayout = (LinearLayout) findViewById(R.id.mydistanceLayout);
NOTE: I have a few LinearLayout layers in my xml file so this #id is for the LinearLayout that just contains the adView that I need to remove and re-add when needed, NOT the top level LinearLayout.
In my progarm logic that is not included and irrelevant, I call the Alert Dialog method
public void showEditTextDialog()
{
parentLinearLayout.removeView((View) adMapView);
Here I remove the add at the start of the Alert Dialog and since I force the keyboard to show immediately and I need the ad not to be visible.
Now at the OK and Cancel responses in the Alert Dialog I add the view back.
builder.setView(dialogView).setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
parentLinearLayout.addView((View) adMapView);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) { imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
dialog.cancel();
parentLinearLayout.addView((View) adMapView);
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
And this fixed the issue. Not sure if it was the best solution and happy to edit or fix the solution if there is a better way, but it solved my issue. Hope it helps someone in the same situation.

How to dim the background behind my pop up menu after clicking a button?

How do I get the background behind my pop up menu to dim after I click the button that opens the popup menu? Looked at other code samples but no luck! Would really appreciate any advice!
final Button Bel = (Button) findViewById(R.id.btnBel);
final ImageView Phone = (ImageView)findViewById(R.id.imgPhone);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
Bel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bel.setVisibility(View.GONE);
Phone.setVisibility(View.GONE);
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.popup_menu, null);
popupWindow = new PopupWindow(container, 1000, 850, true);
popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY, 221, 4000);
btnBelNuCall = (Button) container.findViewById(R.id.btnBelNu);
btnClosePopUp = (Button) container.findViewById(R.id.btnAnnuleren);
btnBelNuCall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:0900-33-44-55-6"));
startActivity(callIntent);
checkPermission();
}
});
btnClosePopUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
Bel.setVisibility(View.VISIBLE);
Phone.setVisibility(View.VISIBLE);
}
});
}
});
1. Create A Layout That Covers Everything In Your Main Layout
I don't know what your XML File looks like, but put something like this at the top of it:
<LinearLayout android:id="dim_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#70000000"
android:visibility="invisible"/>
Note: Depending on your XML file, you may need to alter this for it to be placed on top of all other components.
This will create a layout covering everything in your main layout with a background of color #70000000 which is black with an alpha level of 70. Changing the 70 at the beginning of the hex code will allow you to modify how transparent you want it to be.
2. Show Layout When Popup Is Activated
LinearLayout dim_layout = (LinearLayout) findViewById(R.id.dim_layout);
popupWindow = new PopupWindow(container, 1000, 850, true);
dim_layout.setVisibility(View.VISIBLE);
popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY, 221, 4000);
3. Hide Layout When Popup Is Deactivated
btnClosePopUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
Bel.setVisibility(View.VISIBLE);
Phone.setVisibility(View.VISIBLE);
dim_layout.setVisibility(View.INVISIBLE);
}
});
Give an Id to your root tag and in your class, give it the dim background color. For example in your layout activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#F4F4F4"
android:orientation="vertical"
android:id="#+id/main_screen">
in your class
mainView = findViewById(R.id.main_screen);
mainView.setBackgroundColor(Color.parseColor("#30000000"));

Android dialog with TextView As link

Hi guys I have a problem regarding the android dialog box.What I am trying to do is set few lines of description in a dialog box which I am able to do it fine but at last i need a link called "see more" which will redirect the user to other activity.I am very new to android and these is the first of some things what I am trying to do any help will be appreciated..my code onStart()
protected void onStart()
{
super.onStart();
/*final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.main);
dialog.setTitle("About Service One");
Button button = (Button) dialog.findViewById(R.id.button12);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
protected void onStart()
{
super.onStart();
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.main);
dialog.setTitle("About Service One");
Button button = (Button) dialog.findViewById(R.id.button12);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
TextView showMore= (TextView ) dialog.findViewById(R.id.tvShowMore);
showMore.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(getApplicationContext(),ShowMoreActivity.class);
startActivity(intent);
}
});
dialog.show();
}
And add in xml showmore TextView Which set text from string.xml
i.e <string name="show_more"><u>Show More </u></string> use this for set linkable text
main.xml
...........
<TextView
android:id="#+id/tvShowMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/show_more"
android:textAppearance="?android:attr/textAppearanceMedium"
>
</TextView>
............
Create a custom Dialod layout>> create a text view as show more >> set OnClick Listener on it as ..
You can set the click handler in xml with these attribute:
android:onClick="onClick"
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
dailog.xml
...
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show more"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
...
MyActivity.java
public class MyActivity extends Activity {
public void onClick(View v) {
// ... start show more activity here
}
}
OR... find the view using dialog and set OnClick Listener on it..
showmore = (TextView)dialog.findViewById(R.id.click);
showmore..setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ... start show more activity here
}
});
Try this:
<TextView
android:id="#+id/yourId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show more"
android:onClick="showMore"
android:clickable="true"/>
Inside your activity, get that textView :
TextView showMoreView=(TextView)findViewById(R.id.yourId);
Add this showMoreView inside your text in the dialog and
Inside your activity, define the methode "showMore"
public void showMore(View view)
{
Intent intent =new Intent(YourCurrrentActivity.this,NextActivity.class);
startActivity(intent);
}
This will take you to NextActivity on clicking the "show more" text.

How can I change the order of the RadioGroup widget appears in HelloFormStuff from Android Dev?

I'm going through the tutorial HelloFormStuff. I started having problems adding the RadioGroup widget. After moving some brackets around I finally got it to work.
When I tried to add the final (2) widgets, I found if I tried to add them in main.xml below the RadioGroup they wouldn't appear in the app. I guess I could just call it finished and move on, but I took the time to enter all the code (not ctrl C, ctrl P) and damn it, the widgets should show up where I tell them to! Why can't I add widgets below the RadioGroup?
public class HelloFormStuff extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});
final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
radio_red.setOnClickListener(radio_listener);
radio_blue.setOnClickListener(radio_listener);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
}
});
final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
togglebutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (togglebutton.isChecked()) {
Toast.makeText(HelloFormStuff.this, "Checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(HelloFormStuff.this, "Not checked", Toast.LENGTH_SHORT).show();
}
}
});
final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(HelloFormStuff.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
}
});
}
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};
}
You can try:
Toast.makeText(HelloFormStuff.this,
((RadioButton) v).getText(),
Toast.LENGTH_SHORT).show();
if it crashes, but I don't see any problem with your code.
Here is the main.xml, everything should show up:
<CheckBox android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check it out" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton android:id="#+id/radio_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
<ToggleButton android:id="#+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"/>
<RatingBar android:id="#+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"/>
I know this is REALLY old, but I just started learning Android and I ran into the same thing. Maybe this will help new-comers I found out that the reason you do not see them is because the value of layout_height for the RadioGroup should be wrap_content. The example says to make it fill_parent. But as you may know by now, the object's height will go to the bottom of the screen.
#Duy's main.xml is correct, but I just wanted to point out the exact reason.

Categories