Dynamically Remove EditText Field in LinearLayout - java

My problem is that once my editText fields are created, I don't know how to remove them one by one with the other button in my app. I don't really understand the android:id element as it relates to setID() in my java code. I guess my main question is, how do I figure out how to target dynamically created editText views with their id's if I don't know their id's. Here is my code, any help is really appreciated:
Java:
package com.zdowning.decisionmaker;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;
import android.widget.Toast;
import java.lang.*;
import static com.zdowning.decisionmaker.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
public int numberOfLines = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer();
}
});
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Add_Line();
}
});
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setId(numberOfLines++);
et.setText("Enter Answer #" + numberOfLines++);
ll.addView(et);
numberOfLines++;
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void Remove_Line() {
}
public void getAnswer() {
String[] options = new String[numberOfLines];
EditText text = (EditText)findViewById(R.id.editText2);
options[0] = text.getText().toString();
text = (EditText)findViewById(R.id.editText3);
options[1] = text.getText().toString();
text = (EditText)findViewById(R.id.editText4);
options[2] = text.getText().toString();
int number = (int)(Math.random() * 3);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
XML:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d4cfcd">
tools:context="com.zdowning.decisionmaker.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Your Question Here"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
<LinearLayout
android:id="#+id/linearLayoutDecisions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #2" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #1" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/add_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="+"
android:textColor="#android:color/background_light"
android:textSize="18sp"/>
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="#+id/remove_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="-"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_margin="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="DECIDE!"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_centerInParent="true" />
</RelativeLayout>
<TextView
android:id="#+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20dp"
android:textColor="#android:color/black"
android:textSize="36sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

There is no relationship between android:id and your setID.
Check this answer to unsderstand more about that.
Anyway, one approach to achieve to remove a generated line could be based on the children index of your LinearLayout like this:
private LinearLayout mEditTextContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
...
}
public void addLine() {
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
mEditTextContainer.addView(et);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void removeLine() {
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}

Related

Android Studio RecyclerView: Why always ( No adapter attached; skipping layout )

I am a student who currently taking mobile application development. Although my app can run but it doesnt show any activity instead of just give me W/RecyclerView: No adapter attached; skipping layout. I dont know how to solve the issue, so I was hoping can get some guidlines from experts.
Here is my code
MainActivity.java
package my.edu.utar.practicalassignment;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private Toolbar tool;
private TextView textView;
private RecyclerView recyclerView;
private Button AddBtn;
RecyclerView.LayoutManager layoutManager;
private FirebaseAuth auth;
private DatabaseReference ref;
private String userID = "";
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tool = findViewById(R.id.toolbar);
setSupportActionBar(tool);
//getSupportActionBar().setTitle("Today's Spending");
textView=findViewById(R.id.totalCashBack);
recyclerView =findViewById(R.id.recycle_view);
// recyclerView =(RecyclerView)findViewById(R.id.recycleView);
// layoutManager = new LinearLayoutManager(this);
// recyclerView.setLayoutManager(layoutManager);
AddBtn = findViewById(R.id.add_new);
auth = FirebaseAuth.getInstance();
userID = auth.getCurrentUser().getUid();
ref = FirebaseDatabase.getInstance().getReference().child("expenses").child(userID);
// if(userID !=null){
// try{
// ref = FirebaseDatabase.getInstance().getReference().child("expenses").child(userID);
//
// }catch (NullPointerException ex){
// ex.printStackTrace();
// }
// }
progressDialog = new ProgressDialog(this);
AddBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addItem();
}
});
}
private void addItem() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater layout = LayoutInflater.from(this);
View view = layout.inflate(R.layout.input_layout, null);
alert.setView(view);
final AlertDialog dialog = alert.create();
dialog.setCancelable(false);
final Spinner spinnerItem = view.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.category));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerItem.setAdapter(adapter);
final EditText amount = view.findViewById(R.id.insert_amount);
final EditText notes = view.findViewById(R.id.insert_note);
final Button saveButton = view.findViewById(R.id.add_button);
final Button cancelButton = view.findViewById(R.id.cancel_button);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String amountGet = amount.getText().toString();
String notesGet = notes.getText().toString();
String categoryGet = spinnerItem.getSelectedItem().toString();
Drawable icon = getResources().getDrawable(R.drawable.error);
if(amountGet.isEmpty()){
amount.setError("Please enter an amount!!!", icon );
return;
}
if(notesGet.isEmpty()){
notes.setError("Please enter a notes!!!", icon );
return;
}
if(categoryGet.equals("Select a Category")){
Toast.makeText(MainActivity.this, "Please select a valid category", Toast.LENGTH_SHORT).show();
}
else{
progressDialog.setMessage("Had added the information into database");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
String id = ref.push().getKey();
DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Calendar calendar = Calendar.getInstance();
String date = format.format(calendar.getTime());
Data inform = new Data(categoryGet, date, id, notesGet, Integer.parseInt(amountGet));
ref.child(id).setValue(inform).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(MainActivity.this, "Information added successfully", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Fail to add the information", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
}
dialog.dismiss();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
}
MainActivity.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"
android:background="#000"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff00"
android:elevation="4dp" />
<TextView
android:id="#+id/totalCashBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:text=" Total Cash Back = RM 0"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="556dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
<Button
android:id="#+id/add_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:backgroundTint="#ffff00"
android:text="Add"
app:fabSize="normal" />
</LinearLayout>
InputLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="10dp"
app:cardElevation="10dp"
android:layout_margin="10dp"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="What did you had spend on ?"
android:gravity="center"
android:textColor="#fff"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#ffff00"
android:entries="#array/category"/>
<EditText
android:id="#+id/insert_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please enter a amount:"
android:textColor="#fff"
android:inputType="number"
android:textColorHint="#fff"/>
<EditText
android:id="#+id/insert_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please enter a note:"
android:textColor="#fff"
android:textColorHint="#fff"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="#+id/cancel_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#000"
android:textStyle="bold"
android:background="#fff"
android:layout_margin="2dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="#+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Transaction"
android:textAllCaps="false"
android:textColor="#000"
android:textStyle="bold"
android:background="#ffff00"
android:layout_margin="2dp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
strings.xml
<resources>
<string name="app_name">PracticalAssignment</string>
<string-array name="category">
<item>Select a category</item>
<item>Petrol Spend</item>
<item>Groceries Spend</item>
<item>eWallet Transaction</item>
<item>Other Eligible Spend</item>
</string-array>
</resources>
You have to create an adapter class and add with your recycler view.
The basic snippet to set adapter to recycler view is like bellow
YourAdater adapter = YourAdater();
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
for more detail that how to create a RecyclerView AdapterClass you can see this or this

How can i changing text button by user input

in my exercise in which there are a grid of 3x3 buttons some with numbers others without, I should put those without numbers a number from 0-9, however, put by the user then changing the text of the button. How can I do? I tried something but it doesn't work even when I search on the internet it didn't give me the result I wanted.
This is the code
public class MainActivity extends AppCompatActivity {
private Button btn;
private EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button_X00);
edit = (EditText) findViewById(R.id.edit_dialog);
btn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
showDialog(edit.getText().toString());
return true;
}
});
}
private void showDialog(String str) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("input text");
View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
final EditText edit_dialog = (EditText) view.findViewById(R.id.edit_dialog);
edit_dialog.setText(str);
builder.setView(view);
builder.setNegativeButton("cancel",null);
builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
btn.setText(edit_dialog.getText().toString());
}
});
builder.show();
}
}
THIS IS THE XML CODE
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_view_p8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="GRIGLIAMOD10"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="418dp"
android:layout_height="586dp"
android:layout_marginTop="49dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/linearLayout2">
<androidx.gridlayout.widget.GridLayout
android:id="#+id/tabellaX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="60dp">
<EditText
android:id="#+id/edit_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button_X22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="2" />
<Button
android:id="#+id/button_X00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="0"
android:focusable="false"/>
<Button
android:id="#+id/button_X01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="" />
<Button
android:id="#+id/button_X12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="1" />
<Button
android:id="#+id/button_X02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="0" />
<Button
android:id="#+id/button_X20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="2" />
<Button
android:id="#+id/button_X21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="" />
<Button
android:id="#+id/button_X10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="1" />
<Button
android:id="#+id/button_X11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="1"
app:layout_row="1" />
</androidx.gridlayout.widget.GridLayout>
</LinearLayout>
So here is one possible solution.
First in your XML you can avoid using both Linear layouts since you are using a Constraint layout. So the XML looks as follow.
<?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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_view_p8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="GRIGLIAMOD10"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.gridlayout.widget.GridLayout
android:id="#+id/tabellaX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_view_p8">
<Button
android:id="#+id/button_X00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="0" />
<Button
android:id="#+id/button_X01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
/>
<Button
android:id="#+id/button_X12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="1" />
<Button
android:id="#+id/button_X02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="0" />
<Button
android:id="#+id/button_X20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="2" />
<Button
android:id="#+id/button_X21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
/>
<Button
android:id="#+id/button_X22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="2" />
<Button
android:id="#+id/button_X10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="1" />
<Button
android:id="#+id/button_X11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="1"
app:layout_row="1" />
</androidx.gridlayout.widget.GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I also changed the Java file and I left some comments in case you want a different behavior from your app.
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.gridlayout.widget.GridLayout;
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get all the buttons from the grid - be careful if you add another type of Touchable
ArrayList<View> gridButtons = ((GridLayout) findViewById(R.id.tabellaX)).getTouchables();
for (View v : gridButtons) {
((Button) v).setOnLongClickListener(this);
}
}
#Override
public boolean onLongClick(View v) {
// show the dialog ONLY if the button isn't set
if (((Button) v).getText().toString().isEmpty())
showDialog(v);
return true;
}
private void showDialog(final View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Input a number");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// get only the first inserted number(i.e. input: 45; take only the 4)
((Button) v).setText(input.getText().toString().substring(0, 1));
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
}
EDIT
In the comment section below the answer you asked me if I can edit the behavior of the App.
If you click on the first button where there is the number, for example "4", this must be added to all the other buttons that have a number, if the second button has a number 3, this becomes 7. But if for example 5 to 6 is added this must not become 11 but 1 must be of module 10.
package hr.hello.dm.test;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.gridlayout.widget.GridLayout;
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener, View.OnClickListener {
private ArrayList<View> gridButtons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get all the buttons from the grid - be careful if you add another type of Touchable
gridButtons = ((GridLayout) findViewById(R.id.tabellaX)).getTouchables();
for (View v : gridButtons) {
((Button) v).setOnLongClickListener(this);
((Button) v).setOnClickListener(this);
}
}
#Override
public void onClick(View v) {
if(((Button) v).((Button) v).getText().toString().isEmpty())
return;
int value = Integer.parseInt(((Button) v).getText().toString());
for (View btn : gridButtons) {
if(v.getId() != btn.getId() && !((Button) btn).getText().toString().isEmpty()) {
int btnValue = Integer.parseInt(((Button) btn).getText().toString());
((Button) btn).setText(String.valueOf((value + btnValue)%10));
}
}
}
#Override
public boolean onLongClick(View v) {
if (((Button) v).getText().toString().isEmpty())
showDialog(v);
return true;
}
private void showDialog(final View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Input a number");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
int value = Integer.parseInt(input.getText().toString());
((Button) v).setText(String.valueOf(value));
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
}
As far as i understand what you are trying to do, codes below may help you.
public class MainActivity extends AppCompatActivity{
Button[] buttons;
private final String TAG = this.getClass().getSimpleName();
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttons = new Button[9];
buttons[0] = (Button) findViewById(R.id.button_X00);
buttons[1] = (Button) findViewById(R.id.button_X10);
buttons[2] = (Button) findViewById(R.id.button_X20);
buttons[3] = (Button) findViewById(R.id.button_X01);
buttons[4] = (Button) findViewById(R.id.button_X11);
buttons[5] = (Button) findViewById(R.id.button_X21);
buttons[6] = (Button) findViewById(R.id.button_X02);
buttons[7] = (Button) findViewById(R.id.button_X12);
buttons[8] = (Button) findViewById(R.id.button_X22);
for (int a = 0; a<buttons.length;a++){
showDialog(buttons[a]);
}
}
private void showDialog(final Button btn) {
btn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("input text");
View view = View.inflate(MainActivity.this,R.layout.dialog_activity, null);
builder.setView(view);
final EditText edit_dialog = view.findViewById(R.id.edit_dialog);
builder.setNegativeButton("cancel",null);
builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try {
btn.setText(edit_dialog.getText().toString());
}catch (Exception e){
Log.e(TAG, e.getMessage() + " " );
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
}
});
}
}
activity_main:
<?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=".MainActivity">
<androidx.gridlayout.widget.GridLayout
android:id="#+id/tabellaX"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="60dp">
<Button
android:id="#+id/button_X22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="2"
app:layout_row="2" />
<Button
android:id="#+id/button_X00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="0"
app:layout_row="0"
android:focusable="false"/>
<Button
android:id="#+id/button_X01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true" />
<Button
android:id="#+id/button_X12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="2"
app:layout_row="1" />
<Button
android:id="#+id/button_X02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="2"
app:layout_row="0" />
<Button
android:id="#+id/button_X20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="0"
app:layout_row="2" />
<Button
android:id="#+id/button_X21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="1"
app:layout_row="2"/>
<Button
android:id="#+id/button_X10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="0"
app:layout_row="1" />
<Button
android:id="#+id/button_X11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="1"
app:layout_row="1" />
</androidx.gridlayout.widget.GridLayout>
</LinearLayout>
and here is dialog_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_view_p8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="GRIGLIAMOD10"
android:textSize="30sp" />
<EditText
android:id="#+id/edit_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>

Animation Problems in Android

I've got few problems in using animation in android studios:
I'm trying to translate certain images at an angle. Like in the image below I want to move that attachment pin like icons to all the ends of the corner. How can I do this?
I was trying to scale up the image of the gear image in the center every time its tapped and scale down when tapped again. However, the image starts losing its quality. How can I prevent the quality from getting deteriorated?
Here's the code:
MainActivity.java
package com.rootonelabs.vicky.pulse;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import com.gigamole.library.PulseView;
public class MainActivity extends AppCompatActivity {
PulseView pulseView;
Button btnStart, btnStop;
ImageView gear;
ImageView icon1,icon2,icon3,icon4,icon5,icon6;
int chkClick = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pulseView = (PulseView)findViewById(R.id.pv);
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pulseView.startPulse();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pulseView.finishPulse();
}
});
gear = (ImageView)findViewById(R.id.gear);
gear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gear.animate().scaleX(1.5f).scaleY(1.5f).setDuration(500);
animateDiagonalPan();
icon1 = findViewById(R.id.icon1);
icon1.animate().translationXBy(120).translationYBy(120).setDuration(500);
if(chkClick==0)
{
pulseView.animate().alpha(0).setDuration(500);
gear.animate().scaleX(1.5f).scaleY(1.5f).setDuration(500);
pulseView.finishPulse();
chkClick = 1;
}else{
pulseView.animate().alpha(1f).setDuration(500);
gear.animate().scaleX(1f).scaleY(1f).setDuration(500);
pulseView.startPulse();
chkClick = 0;
}
}
});
}
}
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"
android:background="#f39c12"
tools:context="com.rootonelabs.vicky.pulse.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnStart"
android:layout_weight="1"
android:text="START"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnStop"
android:layout_weight="1"
android:text="STOP"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="#+id/gear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/men3"/>
<ImageView
android:id="#+id/icon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_video"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_archive"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_attachment_black_24dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_video"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_archive"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_attachment_black_24dp"/>
<com.gigamole.library.PulseView
android:id="#+id/pv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:pv_alpha="70"
app:pv_color="#FFF"
app:pv_icon="#drawable/men2"
app:pv_icon_width="100dp"
app:pv_icon_height="100dp"
app:pv_interpolator="#android:anim/linear_interpolator"
app:pv_measure="height"
app:pv_spawn_period="1000" />
</RelativeLayout>

How to do enable a button and change its colour when textfield has some value?

I have tried many answers already given to this, but nothing seems to work properly. And the answers are only for changing state of the button, not colour and text. What am I missing?
I am a very VERY new learner to both Android and programming. And this is my first question on Stack Overflow. Hope it's as per guidelines.
I have a login page which looks like below (img1) [disabled][1]. If textfield has any value, the button should get enabled (img2) [enabled][2]. The Java and XML files are given below.
package io.kaapi.kaapimobileassistant.Activities;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;
import io.kaapi.kaapimobileassistant.Misc.StorageManager;
import io.kaapi.kaapimobileassistant.R;
public class LoginActivity extends AppCompatActivity {
private final static String TAG = "LoginActivity";
private Button login_button;
private TextInputLayout login_activation_layout;
private EditText login_activation_code;
private LinearLayout login_signup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_button = (Button) findViewById(R.id.login_button);
login_activation_layout = (TextInputLayout) findViewById(R.id.login_acitivation_layout);
login_activation_code = (EditText) findViewById(R.id.login_activation_code);
login_signup = (LinearLayout) findViewById(R.id.login_signup);
login_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "Login pressed");
String activation_code_layout = login_activation_layout.getEditText().getText().toString();
Log.v(TAG, "Layout "+activation_code_layout);
String activation_code = login_activation_code.getText().toString();
Log.v(TAG, "Code "+activation_code);
if(activation_code.equalsIgnoreCase("")){
Log.v(TAG, "It's blank");
login_activation_layout.setError("Please enter an activation code");
} else {
Log.v(TAG, "Call login API, validate and show errors or login");
//StorageManager.write(LoginActivity.this, null, "client_domain", "http://ankit50.kaapi.io");
//StorageManager.write(LoginActivity.this, null, "client_logo", "http://cdn.kaapi.io/static");
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}
});
login_activation_code.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
enableSubmitIfReady();
}
#Override
public void afterTextChanged(Editable s) {
}
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setEnabled(false);
} else {
login_button.setEnabled(true);
}
}
});
login_signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse("https://business.kaapi.io");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
The XML file is below
<?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"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="center_vertical|center_horizontal"
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="io.kaapi.kaapimobileassistant.Activities.LoginActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="71dp"
android:layout_marginRight="71dp"
android:layout_marginTop="66dp"
android:src="#drawable/kaapi_logo_login"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:textStyle="bold"
android:gravity="center"
android:textColor="#color/colorTitle"
android:text="Activate Mobile Assistant"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="36dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:src="#drawable/ic_info_outline_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:textSize="14sp"
android:textStyle="italic"
android:layout_marginLeft="5dp"
android:text="The code was sent to you in sign up email and your web dashboard." />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/login_acitivation_layout"
android:layout_width="match_parent"
android:layout_marginTop="29dp"
android:layout_height="wrap_content">
<EditText
android:id="#+id/login_activation_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:maxLength="20"
android:hint="Activation code"
android:maxLines="1"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
style="#style/Widget.AppCompat.Button"
android:text="Activate"/>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="New to Kaapi? " />
<LinearLayout
android:id="#+id/login_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|top"
android:text="Sign up first"
android:textColor="#color/colorPrimary"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
try this
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setClickable(false);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_light_green));// change color here so it's look like button disable
} else {
login_button.setClickable(true);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_dark_green));
}
}

Update and remove item in ListView

when make edit to array list by popup windows or remove item by long click
it just update and remove last value
arraylist and adapter and listview not updated together
this is my code:
CustomListViewAapter.java
package com.zoom.plumbingcalculation;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
public class CustomListViewAdapter extends BaseAdapter{
public static final String colFrom="From";
public static final String colTo="To";
public static final String colCFM="CFM";
public static final String colDia="Dia";
public static final String colLength="Length";
public static final String colTotLoss="Loss";
public static final String colDP="DP";
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFrom;
TextView txtTo;
TextView txtCFM;
TextView txtDia;
TextView txtLength;
TextView txtTotLoss;
TextView txtDP;
public CustomListViewAdapter(Activity activity, ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView=inflater.inflate(R.layout.custom_list_view, null);
txtFrom=(TextView) convertView.findViewById(R.id.From);
txtTo=(TextView) convertView.findViewById(R.id.To);
txtCFM=(TextView) convertView.findViewById(R.id.CFM);
txtDia=(TextView) convertView.findViewById(R.id.Dia);
txtLength=(TextView) convertView.findViewById(R.id.Length);
txtTotLoss=(TextView) convertView.findViewById(R.id.TotLoss);
txtDP=(TextView) convertView.findViewById(R.id.DP);
}
HashMap<String, String> map=list.get(position);
txtFrom.setText(map.get(colFrom));
txtTo.setText(map.get(colTo));
txtCFM.setText(map.get(colCFM));
txtDia.setText(map.get(colDia));
txtLength.setText(map.get(colLength));
txtTotLoss.setText(map.get(colTotLoss));
txtDP.setText(map.get(colDP));
return convertView;
}
}
Activity.java
package com.zoom.plumbingcalculation;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colCFM;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colDP;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colDia;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colFrom;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colLength;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colTo;
import static com.zoom.plumbingcalculation.CustomListViewAdapter.colTotLoss;
public class ps_ss_compressed_air extends Activity {
//Compressed Air Variable
private ImageButton CAButAdd;
private ListView CALvDataTable;
private ArrayList<HashMap<String, String>> CALvDataTableListItems;
private CustomListViewAdapter CALvDataTableAdapter;
//Compressed Air Popup Add Variable
private LayoutInflater layoutAddInflater;
private View popupAddView;
private PopupWindow popupAddWindow;
private Button CAPAButAdd;
private Button CAPAButCancel;
private Spinner CAPASpDia;
private EditText CAPATxtFrom;
private EditText CAPATxtTo;
private EditText CAPATxtCFM;
private EditText CAPATxtLength;
//Compressed Air Popup Edit Variable
private LayoutInflater layoutEditInflater;
private View popupEditView;
private PopupWindow popupEditWindow;
private Button CAPEButEdit;
private Button CAPEButCancel;
private Spinner CAPESpDia;
private EditText CAPETxtFrom;
private EditText CAPETxtTo;
private EditText CAPETxtCFM;
private EditText CAPETxtLength;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ps_ss_compressed_air);
//Compressed Air Variable
CAButAdd = (ImageButton) findViewById(R.id.CAButAdd);
CALvDataTable =(ListView) findViewById(R.id.CALvDataTable);
CALvDataTableListItems= new ArrayList<HashMap<String,String>>();
CALvDataTableAdapter=new CustomListViewAdapter(this, CALvDataTableListItems);
CALvDataTable.setAdapter(CALvDataTableAdapter);
CAButAdd.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
layoutAddInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
popupAddView = layoutAddInflater.inflate(R.layout.ps_ss_compressed_air_popup_add, null);
//popupView.setAlpha(0.5F);
popupAddView.setAlpha(1);
popupAddWindow = new PopupWindow(
popupAddView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
CAPAButAdd = (Button)popupAddView.findViewById(R.id.CAPAButAdd);
CAPAButCancel = (Button)popupAddView.findViewById(R.id.CAPAButCancel);
CAPASpDia = (Spinner) popupAddView.findViewById(R.id.CAPASpDia);
CAPATxtFrom = (EditText) popupAddView.findViewById(R.id.CAPATxtFrom);
CAPATxtTo = (EditText) popupAddView.findViewById(R.id.CAPATxtTo);
CAPATxtCFM = (EditText) popupAddView.findViewById(R.id.CAPATxtCFM);
CAPATxtLength = (EditText) popupAddView.findViewById(R.id.CAPATxtLength);
CAPAButAdd.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
//Add--------------------------------------------------------------------------------------------
HashMap<String,String> temp = new HashMap<>();
temp.put(colFrom, CAPATxtFrom.getText().toString());
temp.put(colTo, CAPATxtTo.getText().toString());
temp.put(colCFM, CAPATxtCFM.getText().toString());
temp.put(colDia, CAPASpDia.getSelectedItem().toString());
temp.put(colLength, CAPATxtLength.getText().toString());
temp.put(colTotLoss, "");
temp.put(colDP, "");
CALvDataTableListItems.add(temp);
CALvDataTableAdapter.notifyDataSetChanged();
//-----------------------------------------------------------------------------------------------
popupAddWindow.dismiss();
}});
CAPAButCancel.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupAddWindow.dismiss();
}});
//popupWindow.showAsDropDown(CAButAdd, 50, 50);
popupAddWindow.showAtLocation(popupAddView, Gravity.CENTER,0,0);
popupAddWindow.setFocusable(true);
popupAddWindow.update();
}});
CALvDataTable.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
{
layoutEditInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
popupEditView = layoutEditInflater.inflate(R.layout.ps_ss_compressed_air_popup_edit, null);
popupEditView.setAlpha(1);
popupEditWindow = new PopupWindow(
popupEditView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
CAPEButEdit = (Button)popupEditView.findViewById(R.id.CAPEButEdit);
CAPEButCancel = (Button)popupEditView.findViewById(R.id.CAPEButCancel);
CAPESpDia = (Spinner) popupEditView.findViewById(R.id.CAPESpDia);
CAPETxtFrom = (EditText) popupEditView.findViewById(R.id.CAPETxtFrom);
CAPETxtTo = (EditText) popupEditView.findViewById(R.id.CAPETxtTo);
CAPETxtCFM = (EditText) popupEditView.findViewById(R.id.CAPETxtCFM);
CAPETxtLength = (EditText) popupEditView.findViewById(R.id.CAPETxtLength);
HashMap<String, String> temp=CALvDataTableListItems.get(position);
CAPETxtFrom.setText(temp.get(colFrom));
CAPETxtTo.setText(temp.get(colTo));
CAPETxtCFM.setText(temp.get(colCFM));
CAPESpDia.setSelection(((ArrayAdapter<String>)CAPESpDia.getAdapter()).getPosition(temp.get(colDia)));
CAPETxtLength.setText(temp.get(colLength));
CAPEButEdit.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
//Edit-------------------------------------------------------------------------------------------
HashMap<String,String> temp= CALvDataTableListItems.get(position);
temp.put(colFrom, CAPETxtFrom.getText().toString());
temp.put(colTo, CAPETxtTo.getText().toString());
temp.put(colCFM, CAPETxtCFM.getText().toString());
temp.put(colDia, CAPESpDia.getSelectedItem().toString());
temp.put(colLength, CAPETxtLength.getText().toString());
temp.put(colTotLoss, "");
temp.put(colDP, "");
CALvDataTableListItems.set(position,temp);
CALvDataTableAdapter.notifyDataSetChanged();
//-----------------------------------------------------------------------------------------------
popupEditWindow.dismiss();
}});
CAPEButCancel.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupEditWindow.dismiss();
}});
//popupWindow.showAsDropDown(CAButEdit, 50, 50);
popupEditWindow.showAtLocation(popupEditView, Gravity.CENTER,0,0);
popupEditWindow.setFocusable(true);
popupEditWindow.update();
//Toast.makeText(ps_ss_compressed_air.this, Integer.toString(position+1) + " Clicked", Toast.LENGTH_SHORT).show();
}
});
CALvDataTable.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
CALvDataTableListItems.remove(position);
CALvDataTableAdapter.notifyDataSetChanged();
Toast.makeText(ps_ss_compressed_air.this,"Removed", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
custom_list_view.xml
<?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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:divider="#000000"
android:weightSum="7">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/From"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/To"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/CFM"
android:layout_weight="1" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/Dia"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/Length"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/TotLoss"
android:layout_weight="1"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/DP"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
</LinearLayout>
Activity.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:id="#+id/CAappbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/CAtoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:logo="#mipmap/ic_launcher"
app:title="#string/app_name">
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="14sp"
android:text="#string/Compressed_Air_Name"
android:id="#+id/CALabCAName"
android:layout_weight="7"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/CAButAdd"
android:layout_weight="1"
android:background="#drawable/ps_ss_compressed_air_but_add"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/CAappbar"
android:id="#+id/linearLayoutCAHeader">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:divider="#000000">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Nodes_Lab"
android:id="#+id/CALabNodes"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:weightSum="2"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Nodes_From_Lab"
android:id="#+id/CALabFrom"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Nodes_To_Lab"
android:id="#+id/CALabTo"
android:textStyle="bold"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/CFM_Lab"
android:id="#+id/CALabCFM"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Diameter_Lab"
android:id="#+id/CALabDia"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Length_Lab"
android:id="#+id/CALabLength"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/Total_Loss_Lab"
android:gravity="center"
android:id="#+id/CALabTotLoss"
android:textStyle="bold"
android:layout_weight="1"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/Pressure_Drop_Lab"
android:id="#+id/CALabDP"
android:textStyle="bold"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/linearLayoutCAHeader"
android:id="#+id/CALvDataTable"
android:longClickable="true"/>
</RelativeLayout>

Categories

Resources