How can I make an EditText in a ContextMenu - java

I have a ListView of values and I want to make each value editable by pressing on it and opening a context menu. How can I add an EditText field to the context menu that appears when clicking on an item in a ListView? If you have a better idea for said issue, feel free to suggest it. Thanks in advance to anyone who answers!

You don't have to use a context menu for that. You can use AlertDialog.
Check out this answer here.
https://stackoverflow.com/a/45352961/8200290
Just create a different layout with an edit view.
Here is you solution.
You are going to have to edit things to make it fit into your application!
MainActivity class
public class MainActivity extends AppCompatActivity {
List<String> list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("Android");
list.add("iPhone");
list.add("Windows");
list.add("Blackberry");
list.add("Mac");
list.add("Laptop");
list.add("LCD");
list.add("Dell");
ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_view_item, list);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked: " + list.get(position), Toast.LENGTH_SHORT).show();
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
View alertView = getLayoutInflater().inflate(R.layout.custom_alert, null);
//Set the view
alert.setView(alertView);
//Show alert
final AlertDialog alertDialog = alert.show();
//Can not close the alert by touching outside.
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//Set the edit text for the item clicked
EditText editText = (EditText) alertView.findViewById(R.id.editText);
editText.setText(list.get(position));
ImageView closeButton = (ImageView) alertView.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
}
});
listView.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:layout_height="match_parent"
tools:context="com.vzw.www.listviewalert.MainActivity">
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
list_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
custom_alert.xml
-> This will show your EDIT text. The box at the bottom closes the alert
<?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="match_parent"
android:padding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/alertContainer"
android:background="#drawable/custom_alert_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/rowOne">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<ImageView
android:id="#+id/closeButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_weight="0.33"
android:background="#cdcdcd" />
</RelativeLayout>
#drawable/custom_alert_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#ffffff"/>
<corners
android:radius="5dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
You get the following:

Related

layout inflater its not work i want in onlick functionality

I am not able to to understanding Layout Inflater its not work in correct way please help me one. Layout Infleter if i click that that Images it's not going text page in that i wrote hi hello
public class Add_viewActivity extends AppCompatActivity {
private ImageView cartbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_view);
cartbtn = findViewById(R.id.cartbtn);
cartbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.text_resource, null);
TextView textid = view.findViewById(R.id.textid);
}
});
}
}
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Add_viewActivity">
<ImageView
android:id="#+id/cartbtn"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentRight="true"
android:background="#null"
android:src="#drawable/ic_cart_gray"
tools:layout_editor_absoluteX="137dp"
tools:layout_editor_absoluteY="274dp"
tools:ignore="MissingConstraints" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hi hello"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Layout inflator cannot be inflated in on parent's onCreate(). You can only do it in fragment and call it from the parent's onCreate ().

why my other view doesn't move up when the snackbar show after using coordinator layout?

so here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
and here is my java file:
public class MainActivity extends AppCompatActivity {
Button myButton;
Snackbar mySnackbar;
CoordinatorLayout myCoordinatorLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.button);
myCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.myCoordinatorLayout);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mySnackbar = Snackbar.make(myCoordinatorLayout,"Test Snakcbar",BaseTransientBottomBar.LENGTH_INDEFINITE);
mySnackbar.setDuration(8000);
mySnackbar.setAction("OK", new View.OnClickListener() {
#Override
public void onClick(View v) {
mySnackbar.dismiss();
}
});
mySnackbar.show();
}
});
}
}
but my button still stick in the bottom and doesn't move up when the snackbar show. I want my button move up like in this video:
https://developer.android.com/images/training/snackbar/snackbar_button_move.mp4
what should I do ?
You don't need the LinearLayout. Also, you don't need to put constraints, since its coordinator layout and not ConstraintLayout.
Try this instead:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:text="Button" />
</android.support.design.widget.CoordinatorLayout>

How to Create a button with another button from a popupwindow

I am trying to create an application which when a button from a popup window is clicked a new button will be created in another layout of the same activity.
Here is the code for the popupwindow.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">
<Button
android:id="#+id/start_goal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start_goal" />
</RelativeLayout>
Here is the code for the activitymain.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.eugene.trackeruptest3.MainActivity"
android:background="#color/darkgrey"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="355dp">
<Button
android:id="#+id/setupgoal"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:text="#string/setup_goal"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonContainer">
</RelativeLayout>
</LinearLayout>
Here is the java code for the popupwindow
public class creategoal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.popupwindow);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.6));
}
}
And here is the java code for the MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button setupgoal = (Button) findViewById(R.id.setupgoal);
setupgoal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,creategoal.class));
}
});
}
}
Have you tried using visibility:gone on your XML and setting it with view.setVisibility(Visibility.VISIBLE); on your onClick method?

On setting OnItemLongClickListener to my listview, no action is performed when I click on the item. How should I solve this?

I am taking data from my database using and want to set that as my list item. Upon clicking and holding an item, I want a dialog to open prompting the user to select one of the given options. My code is as follows:
My MainActivity.java class
public class MainActivity extends AppCompatActivity {
ListView todoList;
DbHelper dbHelper;
ArrayAdapter<String> myAdapter;
Button rename, delete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new DbHelper(this);
todoList=(ListView)findViewById(R.id.to_do);
loadTaskList();
todoList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
Dialog dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.item_dialog);
dialog.setTitle("Select");
rename = dialog.findViewById(R.id.rename_task);
delete = dialog.findViewById(R.id.delete_task);
dialog.show();
return false;
}
});
}
private void loadTaskList() {
ArrayList<String> taskList=dbHelper.getTask();
if(myAdapter==null){
myAdapter=new ArrayAdapter<String>(this,R.layout.todo_item,R.id.task_title,taskList);
todoList.setAdapter(myAdapter);
}else {
myAdapter.clear();
myAdapter.addAll(taskList);
myAdapter.notifyDataSetChanged();
}
}
My activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="com.example.mylist.MainActivity">
<ListView
android:id="#+id/to_do"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"/>
</LinearLayout>
My Layout for the list item:
<?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"
android:clickable="true">
<TextView
android:id="#+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Task"
android:textSize="20sp"
android:layout_centerVertical="true"
android:padding="5dp"/>
<ImageButton
android:id="#+id/task_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_delete_black_24dp" />
<ImageButton
android:id="#+id/task_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_done_black_24dp"
android:layout_toLeftOf="#id/task_delete"/>
</RelativeLayout>

IllegalargumentException while making a custom toast in android

I am very new to android. I am making a custom toast but by app is crashing just after the toast appears in the screen.
This is my mainactivity.java -
public class MainActivity extends Activity {
int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dosome(View V) {
Log.d("message","a message");
if(V.getId() == R.id.launchmap) {
Toast toast = new Toast(this);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
LayoutInflater inflater = getLayoutInflater();
View appear = inflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.root),true);
toast.setView(appear);
toast.show();
}
}
}
And these are my xml files.
toast_layout.xml -
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/images" />
activity_main.xml -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/root"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/launchmarket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="dosome"
android:text="launchmarket" />
<Button
android:id="#+id/launchmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="dosome"
android:text="launchmap" />
</LinearLayout>
However, in the inflater.inflate, if i pass the 3rd parameter as false, everything works. Why so?
Thing is toast is not supposed to be child of some layout in your view, its an independent layout. This is the layout of your toast (toast_layout.xml in your case).
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/images" />
You need to wrap this with a layout lets say a LinearLayout and give id toast_layout_root for that particular linearLayout. Like so
<?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"
android:id="#+id/toast_layout_root">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/images" />
</LinearLayout>
Then do this while making your Toast
View appear = inflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.toast_layout_root),true);
What you are doing currently is trying attach toast to the linearlayout that is in your activity. Toasts have to be independent.

Categories