How is that possible? Thanks! Should I put my View (RelativeLayout) on-top another View and make it invisible and when the user clicks the button it sets it to visible? Or is there another easier way that opens a Dialog(?) in the middle of the screen?
Thanks!
You need a layout to put the child view onto. Safest is Linear Layout since adding multiple child is easy
Fisrt create a xml file in layouts.
for eg in layout
add a file named say iv.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/iv_iv"
android:layout_width="200dp"
android:layout_height="120dp"
android:padding="2dp"
android:scaleType="fitCenter"
android:src="#drawable/person_default_page" />
In the layout where you want to add create a linear layout with some it say #+id/Row_id
Then in the code when you need to add the view
LinearLayout ll = = (LinearLayout) findViewById(R.id.Row_id);
ll.removeAllViews();
View element = LayoutInflater.from(this).inflate(R.layout.iv, ll, false);
ImageView image_iv = (ImageView) element.findViewById(R.id.ri_iv);
ll.addView(element);
Yes you can make it with an alert dialog. If you want to customize dialog use this
-First create an xml for your dialog
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name" />
<EditText
android:id="#+id/username"
android:inputType="textEmailAddress"
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/username" />
<EditText
android:id="#+id/password"
android:inputType="textPassword"
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/password"/>
and override this method
#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_signin, null))
// Add action buttons
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
More info on Devlopper android
Related
I used youtuber Coding in Flow's method to create a custom dialog. I've been trying all day to make the dialog's background transparent. I've used every single method I've found online. Non worked.
Here's how it goes:
First here's the Dialog layout layout_dialog.xml :
<RelativeLayout
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="match_parent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#drawable/dialog_background">
<!-- The contents of the Dialog go here -->
</RelativeLayout>
<ImageView
android:id="#+id/iconImageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:background="?attr/actionBarItemBackground"
android:scaleType="fitCenter"
app:srcCompat="#android:mipmap/sym_def_app_icon" />
</RelativeLayout>
Here's the Dialog class:
public class DialogBrightness extends AppCompatDialogFragment {
//declare whatever variables here
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog, null);
builder.setView(view)
.setTitle("Login")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//get whatever values
listener.apply(//values);
}
});
//findViewById for your dialog contents here
return builder.create();
}
public interface DialogBrightnessListener {
void apply(//values);
}
}
And here's the Dialog being called from the Main activity:
DialogBrightness dialogBrightness = new DialogBrightness();
dialogBrightness.show(getSupportFragmentManager(), "Brightness Dialog");
This is how the Dialog appears:
I'm trying to make the top white part invisible. Nothing works!
Try this:
put the code below in the onCreateDialog:
// set the dialog background to transparent
getDialog().getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));
// remove background dim
getDialog().getWindow().setDimAmount(0);
You can design the layout like following. There is an extra layout, but in case of dialogs, it will help
Try this:
set the background color of the parent layout to:
android:background="#android:color/transparent"
like this:
<RelativeLayout
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="match_parent"
android:background="#android:color/transparent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#drawable/dialog_background">
<!-- The contents of the Dialog go here -->
</RelativeLayout>
<ImageView
android:id="#+id/iconImageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:background="?attr/actionBarItemBackground"
android:scaleType="fitCenter"
app:srcCompat="#android:mipmap/sym_def_app_icon" />
</RelativeLayout>
I need to get the access to TextView inside dialog to change its properties. My code looks like this:
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.dialog_execute, null);
builder.setMessage("Execute").setView(view)
.setPositiveButton("Execute", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
filename = view.findViewById(R.id.filename);
[...]
When i run this i get:
java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView
the xml is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="#+id/dialogXfilename"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="notepad.exe"/>
[...]
</android.support.constraint.ConstraintLayout>
I tried changing the layout type but the app still crashes when the code is executed
I just realised thet the
android:id="#+id/dialogXfilename"
was in the layout not the text view, my bad
I made this dialog for my app that shows up allows you to delete the items from db. It used to be a textView and a button which called the function, all in a linear layout, which worked good. Now I changed to a Relative Layout so I could add another button on the same line as the other, I also added an editText.
My problem is that since I added those objects I am not able to see the buttons anymore. the editText and textView are visible.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dialog_deletefood"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.paulcosma.app2.SecondActivity">
<TextView
android:text="TextView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lblFoodDetails"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp" />
<Button
android:text="Şterge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnDeleteEatenFood"
android:layout_alignBaseline="#+id/btnModifyEatenFood"
android:layout_alignBottom="#+id/btnModifyEatenFood"
android:layout_toStartOf="#+id/lblFoodDetails"
android:visibility="visible" />
<Button
android:layout_marginTop="100dp"
android:text="Modifică"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnModifyEatenFood"
android:layout_below="#+id/lblFoodDetails"
android:layout_toEndOf="#+id/lblFoodDetails"
android:visibility="visible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/txtEditValue"
android:maxLength="6"
style="#style/Widget.AppCompat.AutoCompleteTextView"
android:maxLines="1"
android:textAppearance="#style/TextAppearance.AppCompat"
android:fontFamily="sans-serif"
android:textSize="14sp"
android:textAlignment="center"
android:textColorLink="#android:color/holo_blue_light"
android:inputType="numberDecimal"
android:layout_marginTop="21dp"
android:layout_below="#+id/lblFoodDetails"
android:layout_centerHorizontal="true" />
</RelativeLayout>
This dialog is supposed to show on listView item click. Below is the code I used to show it.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, String> hmap = (HashMap<String, String>)parent.getItemAtPosition(position);
final String _name = hmap.get("food");
final int _id = foodDB.getFoodId(_name);
AlertDialog.Builder mBuiler = new AlertDialog.Builder(SecondActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_deletefood,null);
final TextView lblFoodDetails = (TextView) mView.findViewById(R.id.lblFoodDetails);
final Button btnDeleteEatenFood = (Button) mView.findViewById(R.id.btnDeleteEatenFood);
lblFoodDetails.setText("Aţi ales produsul " + _name + ". Aici puteţi adăuga sau scădea cantitatea printr-o anumită valoare sau puteţi şterge apariţia produsului.");
mBuiler.setView(mView);
final AlertDialog dialog = mBuiler.create();
btnDeleteEatenFood.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
foodDB.deleteEatenFood(_id);
Toast.makeText(SecondActivity.this,_name+" a fost sters",Toast.LENGTH_SHORT).show();
dialog.dismiss();
updateFoodList(foodDB.getAllEatenFood());
}
});
dialog.show();
}
This layout file renders the buttons in the Android layout designer as described (see below)
The problem may reside in code.
The problem was that both buttons where placed based on textView's size. When it changed, buttons got placed out of the screen
Bug:
android:layout_toEndOf="#+id/lblFoodDetails"
android:layout_toEStartOf="#+id/lblFoodDetails"
I'm new to android developing...
I've created a listview using Strings and Array Adapter in setting.java:
public class setting extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting_layout);
String[] settingOptions = new String[]{getString(R.string.settingInterface), getString(R.string.settingLanguages), getString(R.string.settingInfo)};
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, android.R.id.text1, settingOptions);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
and this is my setting_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
setting_layout Activity(listView)
I want to create a popup menu for Languages item that when I click on it,shows some other languages translations.
I've created a string.xml in other languages but I don't know how to use it in popup menu.
1:How can I make a popup menu shown when I click on Languages item?
2:How to put other languages in popup menu?
Thanks in advance
for pop up you can create a dialog using dialog builder, it can be a custom one(xml layout), or can be created dynamically (programmatically using java)
check out this code for a pop up view :)
for example I have this function that I call inside the onClick:
private void showFlavorDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
View promptView;
LayoutInflater layoutInflater = LayoutInflater.from(myActivity.this); //this gets the custom layout inflater
promptView = layoutInflater.inflate(R.layout.dialog_flavors_ar, null); // here you put the custom xml leyout name
final EditText editText = (EditText) promptView.findViewById(R.id.editText_note); //my layout has an edit text and button in it and I want to use it, so call the findviewbyid
Button add_note = (Button) promptView.findViewById(R.id.button_note);
editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); //this removes full screen keyboard, so the keyboard doesnt take the whole screen.
add_note.setOnClickListener(new View.OnClickListener() { //onClick for the button
#Override
public void onClick(View v) {
otherNote_b = true;
flavor = editText.getText().toString();
new httpSetFlavor().execute();
alertDialogFL.cancel();
}
});
if (selectedflavorNameEn.size() > 1) {
flavorsGrid = (GridView) promptView.findViewById(R.id.gridView_flavors); // I also have a gridview in the layout
flavorsGridAdapter adapter = new flavorsGridAdapter(OrderActivityAr.this,
selectedflavorNameEn);
flavorsGrid.setAdapter(adapter); // this is a custom adapter for the grid which you wont need
}
alertDialogBuilder.setTitle(R.string.flavor_ar); //title of the popup window
alertDialogBuilder.setView(promptView); //set view of the popup to your custom view
alertDialogBuilder.setCancelable(true); //this makes the popup cancelable on back button pressed
alertDialogBuilder.setPositiveButton(R.string.delete_flavor_ar, new DialogInterface.OnClickListener() { //this is a button for the popup if you want one
public void onClick(DialogInterface dialog, int id) {
deleteFlavor = true;
new httpSetFlavor().execute();
}
});
alertDialogBuilder.setNegativeButton(R.string.back_ar, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialogFL = alertDialogBuilder.create(); //here you create the dialog using its builder
alertDialogFL.show(); // show it on screen
}
and this is my custom layout xml file, so you can just put any layout you want in it and use it later :)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<GridView
android:layout_width="wrap_content"
android:layout_height="150dp"
android:id="#+id/gridView_flavors"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:numColumns="3"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/gridView_flavors"
android:layout_marginTop="5dp">
<Button
style="?android:attr/buttonStyleSmall"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:singleLine="true"
android:layout_height="wrap_content"
android:text="#string/add_note_ar"
android:id="#+id/button_note"
android:layout_below="#+id/gridView_flavors"
android:textSize="20dp"
android:background="#drawable/button_plain_green"
android:textColor="#000"
android:layout_weight="0" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText_note"
android:layout_below="#+id/gridView_flavors"
android:layout_toRightOf="#+id/other_text"
android:layout_marginTop="2dp"
android:layout_toLeftOf="#+id/button_note"
android:layout_toStartOf="#+id/button_note"
android:layout_weight="1"
android:gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/other_note_ar"
android:id="#+id/other_text"
android:layout_below="#+id/gridView_flavors"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/editText_note"
android:gravity="center"
android:textSize="20dp"
android:layout_weight="0"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<com.andexert.library.RippleView
android:layout_width="wrap_content"
android:id="#+id/ripple_button"
android:layout_weight="0"
android:layout_height="wrap_content"></com.andexert.library.RippleView>
</TableRow>
</RelativeLayout>
this is the look of the layout:
and in your case you can use listview in the custom layout as well :) but with custom list item for example a radio button.
as for displaying other languages saved in xml
well you should have them saved in res/strings.xml
you can use the strings in an array of strings like this:
String strArray[]={
getResources().getString(R.string.string1),
getResources().getString(R.string.string2),
getResources().getString(R.string.string3),
getResources().getString(R.string.string4)
};
The app I am programming has a menu in which the user can pick a new building to build. The user will press "Buy" I would like this to create a new instance of the imagebutton that corresponds to that particular building. My current problem is that I only understand how to do this by setting up these in the xml file and setting them invisible until they buy it. I would like to dynamically create these buildings when they are bought. Is there a way to dynamically create an instance of an image button is this way? I don't really think including my source code is necessary here since I just need an example that works (most examples I have seen just don't work out). But if you would like to see some source code let me know.
Any help is appreciated. Thanks.
Code inside buy building button:
newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setImageResource(R.drawable.mainhut);
newColonyHut.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(newColonyHut);
Here is the xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="0dp"
android:layout_marginRight="20dp" >
<Button
android:id="#+id/buyColonyHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_below="#+id/colonyHutPrice" />
</LinearLayout>
You can create ImageView and dynamically add to layout you specified.
sample:
in oncreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(your_image_here);
image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
test.addView(image);
}
});
the activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >
<LinearLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/layer"
android:text="Button" />
</LinearLayout>
As you can see ImageView is created when the button is clicked in the layout and then add it to the LinearLayout dynamically.
The LayoutParams is where you set the height and width of your ImageView respected the its parent which is the LinearLayout