I am building an music player. Whenever the user will click on a ImageView, a dialog will come. The dialog will have a switch in it. I wanted to say that whenever the user switches on the switch to on, I wanted to change the ContentView of MainActivity.java to a custom layout file. How can I do that.
Any suggestions would be accepted..
Thanks
I would add a callback to your Dialog that you simply invoke to set your content, something like
class MyDialog(val switchChangedCallback: (Boolean) -> Unit) : DialogFragment() ...
When you change the switch, invoke the callback and handle the result in the Activity:
val dialog = MyDialog(switchChangedCallback = { isOn ->
if (isOn) {
setContentView(R.layout.abc)
} else {
setContentView(R.layout.def)
}
})
dialog.show(supportFragmentManager, MyDialog::class.java.name)
You may need to check that the callback survives app rotation!
You can try below code...
Dialogue :
Dialog dialogue = new Dialog(MainActivity.this);
dialogue.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogue.setCancelable(false);
dialogue.setContentView(R.layout.dialogue);
Switch mySwitch = (Switch) dialogue.findViewById(R.id.MYSWITCH);
Button btnClose = (Button) dialogue.findViewById(R.id.CLOSEBTN);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
setContentView(R.layout.layout1);
}
else{
setContentView(R.layout.layout2);
}
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogue.dismiss();
}
});
dialogue.show();
dialogue.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="16dp"
android:orientation="vertical"
android:layout_height="match_parent">
<Switch
android:id="#+id/MYSWITCH"
android:text="Change Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/CLOSEBTN"
android:text="Close"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
as your question says you need a switch inside dialogue , so we need to create a custom dialogue layout and apply it to dialogue !
Related
Hello my goal is to make my custom view (ViewCircleOfInterest.java) receiving onClick. Now every time I click onClickListener on ViewCircleOfInterest.java is not called.
My interesting part of layout looks like that:
my_layout.xml
<FrameLayout
android:id="#+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rl"
android:layout_alignParentTop="true"
android:background="#bbb">
<abc.x.y.ViewCircleOfInterest
android:id="#+id/view_circle_of_interest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
I have FrameLayout on my layout and I added view (SurfaceView - CameraPreview.java) to it like that
MyActivity.java
rootView = findViewById(android.R.id.content).getRootView();
viewCircleOfInterest = rootView.findViewById(R.id.view_circle_of_interest);
viewCircleOfInterest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(NewWayMeasurementActivity.this, "circle a", Toast.LENGTH_SHORT).show();
}
});
(...)
((FrameLayout) rootView.findViewById(R.id.camera_view)).addView(cameraPreview);
(...)
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
On CameraPreview.java in my init() method I do like that:
(...)
viewCircleOfInterest = ((NewWayMeasurementActivity)activity).viewCircleOfInterest;
viewCircleOfInterest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "circle aa", Toast.LENGTH_SHORT).show();
}
});
I also set onClickListener on my ViewCircleOfInterest.java
public ViewCircleOfInterest(SurfaceView surfaceView) {
super(surfaceView.getContext());
this.surfaceView = surfaceView;
setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(surfaceView.getContext(), "circle aaa", Toast.LENGTH_SHORT).show();
}
But unfortunately non of this method works, I still cannot receive onclicks from my custom view. How to solve that? I hope my question and code is understandable.
Make these changes in your code and check if it works.
my_layout.xml
<FrameLayout
android:id="#+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/rl"
android:layout_alignParentTop="true"
android:background="#bbb">
<abc.x.y.ViewCircleOfInterest
android:id="#+id/view_circle_of_interest"
android:layout_width="wrap_content"
android:clickable="true"
android:layout_height="wrap_content"/>
</FrameLayout>
MyActivity.java
((FrameLayout)rootView.findViewById(R.id.camera_view)).addView(cameraPreview,0);
Here we are adding cameraPreview at 0 index so your custom view is on top now and that should receive clicks.
I have a BottomSheetBehaviour in my layout, and it works absolutely fine. The problem is, that whenever user clicks on back button, it becomes invisible(or gone). How can I prevent it to be non-dissmissable?
create a layout for your bottom sheet as:
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="16dp"
app:behavior_peekHeight="260dp"//change according to your need
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
//your design here
</LinearLayout>
</androidx.core.widget.NestedScrollView>
include this layout in your main layout file
<include layout="#layout/layout_name" />
now you can use your bottom sheet in your activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
View bottomSheet = findViewById(R.id.bottomSheet);
//add behaviour as
BottomSheetBehavior<View> bottomSheetBehavior =BottomSheetBehavior.from(bottomSheet);
//access your views inside bottomSheet as
TextView textView = bottomSheet.findViewById(R.id.textViewId);
}
Just override onCancel() or onDismiss().
#Override
public void onCancel(DialogInterface dialog) {
// super.onCancel(dialog); // comment this out
Toast.makeText(MainApp.get(), "CANCEL REQUESTED", Toast.LENGTH_SHORT).show();
}
Or override onBackPressed in onCreateDialog function when you're crating the dialog
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), getTheme()){
#Override
public void onBackPressed() {
Toast.makeText(MainApp.get(), "CANCEL REQUESTED", Toast.LENGTH_SHORT).show();
}
};
}
I'm trying to display a persistent message at the bottom of my application(through out all activities) when there's no internet connection, and the message disappears once there's a network connection
I thought about using and application dialog, which I was able to do, but then I realised the background was not clickable, I need users to still have access to navigate pages/menu
something as seen in the picture below
What I tried with a custom alert dialog
NoInternetDialog.java
public class NoConnectionDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.no_internet_connection_dialog);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
// Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
// dialogButton.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// dialog.dismiss();
// }
// });
dialog.show();
}
}
the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/zxing_viewfinder_laser"
android:elevation="4dp"
android:orientation="vertical"
android:padding="#dimen/_8sdp"
app:elevation="4dp">
<TextView
android:id="#+id/text_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/no_internet_connection"
android:textColor="#color/colorWhite"
android:textSize="#dimen/dimen_16" />
</LinearLayout>
and how I call it
NoConnectionDialog alert = new NoConnectionDialog();
alert.showDialog(this, "No Internet Connection");
What do you guys suggest?
Don't use a Dialog, just add that layout to your Activity's layout and set the visibility based on whether the user has an internet connection. This will avoid all of the issues you'll run into trying to do this with a Dialog. If you have multiple Activitys, you could use a common superclass for them that includes this functionality.
I am using fragment, I would like to display the imageView I've got in thumbnail on my Fragment in another view or Dialog ? or something for displaying :)
I would like when we tap on the ImageView, the new view displays, and when we tap on Button, that returns on the main Fragment.
I have implemented my onClickListener, and that works but I don't know how to pass data or whatever for displaying ImageView in full screen...
Here is my code for onClickListener :
mImageReport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (zoomOut) {
zoomOut = false;
} else {
zoomOut = true;
}
}
});
No need to create new window for displaying full screen image. You can use android default gallery image viewer. Just set full image path or image URI to "mImageReport" as a tag, inside its onClick you can retrieve it. Use following line of code to display full screen image as
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
you can set full screen dialog to show image like below
final Dialog nagDialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.preview_image);
Button btnClose = (Button)nagDialog.findViewById(R.id.btnIvClose);
ImageView ivPreview = (ImageView)nagDialog.findViewById(R.id.iv_preview_image);
ivPreview.setBackgroundDrawable(dd);
btnClose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
nagDialog.dismiss();
}
});
nagDialog.show();
xml code for layout containing image view and close button only:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/iv_preview_image" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/close"
android:id="#+id/btnIvClose" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
I need to set this layout visible when i click a button, my java is like:
Layout propLayout = (Layout) findViewById(R.id.properLayout);
public void propsBtn(View view) {
propLayout.setVisiblity(View.Visible);
}
I know I'm totally wrong with the layout line! I'll be very grateful if someone could show me how to set it right :)
This is my XML:
<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"
android:background="#color/mainBackGround"
tools:context="com.myapplication2.app.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
...contents...
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="#+id/properLayout">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:padding="8dp">
...contents...
</RelativeLayout>
</LinearLayout>
DerGolem gave the right answer but he then deleted it, so I report it here now:
//First we set visibility value of interested layout either to gone, visible or invisible
android:visibility="gone"
Then under onCreate write like:
//specify the button that has to handle visibility
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);
//And the layout we want to change is visibility
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);
properties.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (propLayout.getVisibility() == View.VISIBLE){
propLayout.setVisibility(View.INVISIBLE);
} else {
propLayout.setVisibility(View.VISIBLE);
}
}
});
The another way of toggle, with less lines, if someone likes
// button to click
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);
// and LinearLayout to toggle
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);
properties.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
propLayout.setVisibility((propLayout.getVisibility() == View.VISIBLE)
? View.INVISIBLE
: View.VISIBLE);
}
});
You will need to set an OnClickListener() on the layout you want to toggle. Then inside the listener you check to see if the layout is visible or not. If it is, then you set its visibility to View.INVISIBLE. Otherwise, you set it to View.VISIBLE.
Try this:
int counter = 0;
TextVeiw tv = (TextView) findViewById(R.id.textView);
Button button = (Button ) findViewById(R.id.but);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
if(counter%2==0){
tv.setVisibility(View.Visible);//show
} else {
tv.setVisibility(View.Gone);//hide
}
}
});