I want to make a bottom dialog which will display a text I provide and I can hide and show it whenever I want. This dialog will be shown in all activities. For example, if the app is not connected to the server right now I will show this bottom dialog saying "No connection" and this dialog will be displayed in any activity which is on the screen. How to make this dialog, I tried to make it in XML but I needed to write its show/hide methods in every activity which is a tedious work.
Here is an image which shows the bottom dialog which I am trying to make.
Create a custom dialog box that is easy to show on all activities. A dialog box can be customized as the style in your picture.
Here is an example, requestFeature() must be called before adding content, Other settings need to be after setContentView().
public class YOUR_DIALOG extends Dialog {
private String mText;
public YOUR_DIALOG(Context context, String text) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mText = text;
}
#Override
public void onStart() {
super.onStart();
Window dialogWindow = getWindow();
dialogWindow.getAttributes().width = android.widget.ListPopupWindow.MATCH_PARENT;
dialogWindow.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
dialogWindow.setBackgroundDrawable(new ColorDrawable(0xffff7320));
dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
// Everything else remains the same, as is the case with the normal dialog box.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_dialog_layout);
textview = findViewById(...);
textview.setText(mText);
}
}
And the layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textview"
android:textAppearance="#android:style/TextAppearance.Material.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="10dp"
android:drawablePadding="10dp"
android:drawableLeft="#mipmap/ic_launcher"
android:textStyle="bold"
android:textColor="#fff"/>
</LinearLayout>
In your activity
new YOUR_DIALOG(this,"Dialog").show();
Related
I'm making an android app and i want to put a settings button on every layout in the app. When i press the settings button, a custom dialog pops up and i can access the app settings.
The problem i'm having is that i want to refer to 1 method in some class (doesn't matter to me which one). I'm already using the include in my XML of my layouts like this:
<include android:id="#+id/settingsButton"
layout="#layout/settingsbuttonlayout"/>
The settingsbuttonlayout.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/root_vg">
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp" app:srcCompat="#drawable/settingsicon"
android:id="#+id/settings_dialog"
android:cropToPadding="true"
android:adjustViewBounds="false" android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.133"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.123"
android:clickable="true"
android:focusable="true"
android:background="#drawable/customdialog" android:onClick= "showSettings"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You can see that there is an onclick defined in this layout. However (for as far as i know) this means i need the same "showSettings" method in every layout class. How can i work around this so i should only write the "showSettings" method once and can refer to it?
This is the showSettings method:
public void showSettings(View v){
Dialog dialog = new Dialog(this, R.style.DialogStyle);
dialog.setContentView(R.layout.settings_dialog);
dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground);
Button btnClose = dialog.findViewById(R.id.close_settings);
btnClose.setOnClickListener(view -> dialog.dismiss());
dialog.show();}
PS: I'm pretty new into making apps and GUI's. I didn't learn it yet in school and i'm just figuring out everything myself so sorry if this is some straightforward or stupid question :)
you can remove the onClick attribute from your setttings_dialog which calls the showSettings, next create a Utility.java file in which you can make your function as static
public static void showSettings(View v){
Dialog dialog = new Dialog(this, R.style.DialogStyle);
dialog.setContentView(R.layout.settings_dialog);
dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground);
Button btnClose = dialog.findViewById(R.id.close_settings);
btnClose.setOnClickListener(view -> dialog.dismiss());
dialog.show();}
now in whichever class you want to call this method just write
Button settingsButton = (Button) findViewById(R.id.settingsButton);
settingsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Utility.showSettings(v);
}
});
After searching some more I found the following:
From whichever class i wanted to open the dialog i have to write this:
SettingsDialog.showSettings(this);
In my SettingsDialog class i have the following:
public class SettingsDialog {
static void showSettings(Context context) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.settings_dialog);
Dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground);
}
dialog.show();}
}
I am just getting into android apps, and I have yet to find a tutorial that explains in detail of how to do anything.Can someone show me the code on how to create two buttons (sign in and sign up) in android ?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginButton=(Button)findViewById(R.id.button);
button.setOnClickListener(LogInListener);
signUpButton=(Button)findViewById(R.id.button2);
button2.setOnClickListener(SignUpListener);
}
private OnClickListener LogInListener=new OnClickListener()
{
public void onClick(View v)
{
}
}
Is this the correct way to implement? thanks
activity_main.xml
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
android:id="#+id/button"
android:layout_marginTop="61dp"
android:layout_below="#+id/textView3"
android:layout_toStartOf="#+id/button2"
android:layout_toLeftOf="#+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign UP"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_alignLeft="#+id/editText2"
android:layout_alignStart="#+id/editText2"
android:layout_marginLeft="48dp"
android:layout_marginStart="48dp" />
EDIT:
Now that you have edited your question, you just need to do one more thing to declare your Buttons as instance variables. Declare them outside of all methods (onCreate) but inside the mainActivity.
PRE EDIT:
I'll show you what your main activity (Java class) and what your layout (XML file) should look like:
Main Activity:
public class MainActivity extends AppCompatActivity {
Button signIn, signUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signIn = (Button) findViewById(R.id.'idOfButtonFromXMLLayout');
signUp = (Button) findViewById(R.id.'idOfButtonFromXMLLayout');
//Looking at my XML code, the signIn id would be R.id.signInButton
}
The findViewById method is inherited from the AppCompatActivity class, all activities extend the AppCompatActivity class. Older versions of android just extended the Activity class.
The findViewById method takes an int parameter more specifically an id.
The reason a cast is required is because the findViewById method as you would assume returns a type of View, this is then casted to a button.
XML Layout File:
<RelativeLayout
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:padding="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="#+id/signInButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign In"
<!-- Complete Layout Details--> />
<Button
android:id="#+id/signUpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signUpText"
<!-- Complete Layout Details--> />
</RelativeLayout>
In the code above I have represented the text of the buttons in two ways...
1) Hard-coded string "Sign In"
2) String resource "#string/signUpText
It is good practice to change your hard-coded strings to the latter format.
If you're new at Android Development some things are just confusing. I would create buttons by doing this:
Define Button in your XML File.
Add Listener to your Button.
Don't forget to add id attribute to your Button.
i would do it this way.
LAYOUT XML FILE
<Button
android:id="#+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button One" />
<Button
android:id="#+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.onClickListener {
private Button buttonOne;
private Button buttonTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOne = (Button) findViewById(R.id.buttonOne); // id located in your xml file
buttonOne.setOnClickListener(this);
buttonTwo = (Button) findViewById(R.id.buttonTwo);
buttonTwo.setOnCliclListener(this);
}
private void onClick(View v){
switch(v.getId()) {
case r.id.buttonOne: {
// action when buttonOne is clicked
break;
}
case r.id.buttonTwo: {
// action when buttonTwo is clicked
break;
}
}
}
To create a button, you have to code in your xml file, or drag it in your Design view which will do that for you
Which will auto-magicly do this for you, with auto generated values.
-to name your button edit android: text
-edit android: id to edit the "key" that connects your button design in xml to java
If you want to use your button for things like onclicklisteners and such, you will need to "import" it into your java code, like so. The android: id ="#/<>value"and findViewById(R.id.) should be the same (though its not in the photos)
Now simply do this again for every button you want and change the values to your needs. Hope I helped.
Im having trouble regarding dialogs, so Ive been re-reading the android docs several times over, and am still unsure about the following things and would really appreciate if anyone can answer my questions...
Before i ask my questions ill show my code...
CustomDialog (Straight copy from android dev. site)
public class FireMissilesDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_createlocation, null))
.setTitle(R.string.dialog_createlocationtitle)
// Add action buttons
.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
FireMissilesDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}`
and here is the layout for the dialog(dialog_createlocation.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/EditTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="#string/name"
android:maxLines="1"/>
<EditText
android:id="#+id/EditTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="#string/address"
android:maxLines="2"/>
Questions:/n
In my main activity, I want to get the text from the two EditText in the dialog. Although Ive seen some SO questions about this but im so overwelmed and cant seem to understand the answers./n
2.Is it necessary for me to create this dialog in its own class?-can i just create it in my main activity(- without creating an inner class)?/n
3.Im confused with why to create a custom dialog, it has to extend a fragment-why not just an activity?/n
4.I create an instance of the above dialog in my main activity (which is not a fragment) and i got some issues doing this:
public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new FireMissilesDialogFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
}
Thanks!
In my main activity, I want to get the text from the two EditText in
the dialog. Although Ive seen some SO questions about this but im so
overwelmed and cant seem to understand the answers.
EditText editTextName = dialog.getDialog().findViewById(R.id.EditTextName);
String name = editTextName.getText().toString();
Is it necessary for me to create this dialog in its own class?-can i just create it in my main activity(- without creating an inner
class)?
Yes, you can. AlertDialog just give you already present structure for your dialog. But to make your own just use Dialog Class.
3.Im confused with why to create a custom dialog, it has to extend a fragment-why not just an activity?
Its not necessary to use only Fragment for Dialog. as per second answer.
4.I create an instance of the above dialog in my main activity (which is not a fragment) and i got some issues doing this:
Post stacktrace or error log for this.
This Question Is Pretty Silly to ask but out, but i Would like to know how it Works?
I had a Relative Layout with 2 ImageViews as Child having separate clickListner instances. One above Another, of same Size and Attributes.
Overlaps Each other. Both having Different images.
Question is When i click on one image both ImageView Click listners are Called.
Or if i disable the Click on ImageView Top, The ImageView Below Still Works, I was Clicking on Image View Above though. How it is I'ts Getting callback from both.
I Just Want to know How it works? not The code, i do not have any issue writing code for clickListners Whether only one Working or Both.
<RelativeLayout
----
---
>
<ImageView
---
---<!--Child 1-->
<ImageView
---
---<!--Child 2-->
<RelativeLayout/>
Taken from here:
if you are working with just one clicklistener, you can do:
View.OnClickListener myOnlyhandler = new View.OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.b1:
// it was the first button
break;
case R.id.b2:
// it was the second button
break;
}
}
}
Use ImageButton
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Activity class
public class MainActivity extends Activity {
ImageButton imgButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButtonListener();
}
public void addButtonListener() {
imgButton = (ImageButton) findViewById(R.id.imageButton);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"ImageButton is working!", Toast.LENGTH_SHORT).show();
}
});
}
}
Give this to your parent layout
android:context="Yourclasshere"
then give this to your image view
android:onclick="onclick"
and then implement the on click listener or make the method like Vitly A make above
So far I know how to change the Background's color via html codes. Now I am Trying to change the background of the Layout of my main activity to different images using a button click. If possible using only one single button.
Thank you!
The following code is from this link Setting background image in java from Omi0301.
//assuming your Layout is named linearlayout1:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout1);
ll.setBackgroundResource(R.drawable.sample);
All you do is create a layout variable and set its background resource with the image that you would like it to be.
try to use "setImageResource" instead the "setBackgroundResourse"
Try below code
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="64dp"
android:layout_marginTop="71dp"
android:text="changeColor" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button button;
LinearLayout mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout=(LinearLayout)findViewById(R.id.myLayout);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
mainLayout.setBackgroundResource(R.drawable.newImage);
}
});
}
}