Clicking EditText causes Button onClick() functions to not work - java

I have an ExpandableListView whose children are a RelativeLayout of four LinearLayouts that contain TextViews which will pull answers to a question and assign that question a score from 1-4 (bad, mediocre, good, and world class respectively), and finally an EditText for the user to leave comments on why they rated the question that score. When the user clicks a score box, the background changes colour to highlight the selected score, and sets the other score box colours back to their default colour to show that they are no longer selected. Below is my Java to show how this works:
// ChildView views
badScoreView = convertView.findViewById(R.id.qwedit_main_badView);
mediocreScoreView = convertView.findViewById(R.id.qwedit_main_mediocreView);
goodScoreView = convertView.findViewById(R.id.qwedit_main_goodView);
worldclassScoreView = convertView.findViewById(R.id.qwedit_main_wcView);
badScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_badTextView);
mediocreScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_mediumTextView);
goodScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_goodTextView);
worldClassScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_worldclassTextView);
questionCommentEditText = (EditText) convertView.findViewById(R.id.qwedit_main_commentEditText);
// TODO: TextView score subcategory setters
badScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 1;
badScoreView.setBackgroundColor(Color.parseColor("#eca9a7"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
questionCommentEditText.clearFocus();
}
});
mediocreScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 2;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f7d6a6"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
questionCommentEditText.clearFocus();
}
});
goodScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 3;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#addbad"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
questionCommentEditText.clearFocus();
}
});
worldclassScoreView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 4;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#80baeb"));
questionCommentEditText.clearFocus();
}
});
And the layout, just in case that is relevant:
<?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="wrap_content">
<LinearLayout
android:id="#+id/qwedit_elv_item_score_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/qwedit_main_badView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/badScore">
<TextView
android:id="#+id/qwedit_main_badTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Bad"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_mediocreView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/mediocreScore">
<TextView
android:id="#+id/qwedit_main_mediumTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Mediocre"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_goodView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/goodScore">
<TextView
android:id="#+id/qwedit_main_goodTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="Good"/>
</LinearLayout>
<LinearLayout
android:id="#+id/qwedit_main_wcView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="#color/worldclassScore">
<TextView
android:id="#+id/qwedit_main_worldclassTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#color/white"
android:textSize="20sp"
android:text="World Class"/>
</LinearLayout>
</LinearLayout>
<EditText
android:id="#+id/qwedit_main_commentEditText"
android:layout_width="930dp"
android:layout_height="wrap_content"
android:layout_below="#+id/qwedit_elv_item_score_container"
android:cursorVisible="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="text"
android:hint="Comments"/>
</RelativeLayout>
For some reason, when the user presses the EditText and either adds text or clicks out of it, the button onClick() functions no longer work (i.e., they no longer change colour).
Is this due to something with focus? the javacode onClick functions are in a CustomListViewAdapterin order to be able to inflate the same ExpandableListView for as many Questions as I will have (somewhere around 200 or so).

As far as I understood the problem is with the focus of the View. You might need to use an OnFocusChangeListener and then check if the focus event happened when the screen was in touch mode.
Or you could try to add these lines inside each onClick(View view) method:
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setFocusableInTouchMode(false);
This way, view will get focus and you could do something inside the onClick() method after these lines of code, after the view was again set into setFocusableInTouchMode(false);

Related

Can't dismiss a popup window (other solutions do not work)

I am trying to create a simple PopupWindow with a text entry field.
I know there are many questions of this type, but none seem to solve my problem. This is the XML of my popup layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:background="#color/white"
android:gravity="center"
android:padding="15dp"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dodaj produkt po identyfikatorze"
android:textSize="15sp"
android:gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:textSize="20sp"
android:hint="Identyfikator"
android:minWidth="250dp"
android:id="#+id/insert_edit"
android:gravity="center"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:id="#+id/insert_ok"
android:text="OK"/>
<Button
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:id="#+id/insert_cancel"
android:text="Anuluj"/>
</LinearLayout>
</LinearLayout>
Now, I want to create this layout in my main activity when a FAB is pressed. This is how I do it:
public class ScrollingActivity extends AppCompatActivity {
private ActivityScrollingBinding binding;
private int ACTIVITY_LOAD_DATABASE = 1;
AbstractDatabase database;
RowAdapter rowAdapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = Room.databaseBuilder(getApplicationContext(), AbstractDatabase.class, "magazyn_db")
.allowMainThreadQueries().build();
binding = ActivityScrollingBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
FloatingActionButton fab = binding.addCode;
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.println(Log.INFO, "VIEWs", "Showing popup");
PopupWindow codePopup;
View popupView = getLayoutInflater().inflate(R.layout.insert_code, binding.getRoot());
codePopup = new PopupWindow(popupView);
codePopup.setFocusable(true);
codePopup.setOutsideTouchable(true);
View cancelButton = codePopup.getContentView().findViewById(R.id.insert_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
codePopup.dismiss();
}
});
View acceptButton = codePopup.getContentView().findViewById(R.id.insert_ok);
acceptButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View editView = codePopup.getContentView().findViewById(R.id.insert_edit);
String id = ((EditText)editView).getText().toString().trim();
insertNewElement(id);
codePopup.dismiss();
}
});
}
});
[...]
For an unknown reason my popup doesn't go dismissed, both when I click outside of it, or press the cancel button (when I tried to print some logs from the handler, they worked). I have tried many solutions, also inflating it with no root, but then 'showAtPosition' doesn't even show it, and no solutions here on stackoverflow seem to help me even though there are many of them.
Is anyone able to point the problem here?
public void dismiss () Disposes of the popup window. This method can be invoked only after showAsDropDown(android.view.View) has been executed. Failing that, calling this method will have no effect.

How do I create collapsable edittext areas similar to the attachment in android

Thanks for reading this, I would like to create a collapsable edittext content area in my android activity and I don't know how to do it. see the screenshot below
when the user clicks a particular view containing the edittext, an edittext appears and is only hidden when the user clicks the view again.
KOTLIN
text.setOnClickListener(View.OnClickListener {
if(text_layout.visibility == View.VISIBLE)
text_layout.visibility = View.GONE
else
text_layout.visibility = View.VISIBLE
})
use Visiblity to handle that
JAVA
text = findViewById(R.id.text);
text_layout = findViewById(R.id.text_layout);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(text_layout.getVisibility() == View.VISIBLE)
text_layout.setVisibility(View.GONE);
else
text_layout.setVisibility(View.VISIBLE);
}
});
XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello asdasdasd!"
android:id="#+id/text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="200dp"
android:id="#+id/text_layout"
android:visibility="gone"
android:background="#000"/>

How to delete layout when button pressed

I am new to android please help me to find the appropriate solution for this situation.
please refer UI(image link) . I am trying to do if user clicks add button new listview should inflate. and when user press the delete button on particular listview, entire row should get deleted.
Problem 1 : when pressing delete wrong row is getting deleted.
Problem 2 : when user press start group id value of all the added row should saved in arraylist.
Problem 3 : I have to apply limit in add and delete .. User cannot add more than 20 row and at least one row should available i.e., user can't delete the all rows.
UI looked like : App UI
code:XML file of each row
<?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">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:text="1" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="time"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="delete" />
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
XML code of UI part
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/leanierLayout_test"
android:orientation="vertical">
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
/>
OnCreate Method :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ap_auto_start_test);
ArrayAdapter<String> adapter2;
adapter2 = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,groupId);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Button add = findViewById(R.id.button1);
final LinearLayout data = (LinearLayout) findViewById(R.id.leanierLayout_test);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
LayoutInflater li = LayoutInflater.from(getApplicationContext());
final View view = li.inflate(R.layout.tryandtest,null);
data.addView(view);
Spinner s = view.findViewById(R.id.spinner);
s.setAdapter(adapter2);
Button delete= findViewById(R.id.button2);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
data.removeViewInLayout(view);
}
});
}catch (Exception e){
e.printStackTrace();
Log.d("infilate exception", e.toString());
}
}
});
}
You can look at doing CRUD functionality on a recyclerview. Stop using the listview. It is very inefficient.
The following link should give you a very clear guideline on how to achieve your functionality.
Recycler view CRUD

Textview Gravity not working properly in android

What's wrong with my code, I'm trying to display my TextView named "invalid", in different locations (left,right,center), but the gravity (left,right,center) won't work!
My text.xml is
<?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:orientation="vertical"
android:padding="20dp" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/etext"
android:hint="#string/comment"
android:inputType="textPassword"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button"
android:layout_weight="25"/>
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="ToggleButton"
android:layout_weight="75"
android:checked="true"
android:paddingLeft="15dp"/>
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/invalid"
android:layout_gravity="center"
android:gravity="center" />
</LinearLayout>
My TextPlay.java is
public class TextPlay extends Activity {
Button button;
ToggleButton tbutton;
TextView tview;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
button = (Button) findViewById(R.id.button1);
tbutton = (ToggleButton) findViewById(R.id.toggleButton1);
tview = (TextView) findViewById(R.id.textView1);
et = (EditText) findViewById(R.id.etext);
tbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (tbutton.isChecked()) {
et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else {
et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String input = et.getText().toString();
System.out.println(input);
if (input.contentEquals("left")) {
tview.setGravity(Gravity.LEFT);
} else if (input.contentEquals("right")) {
System.out.println("inside right");
tview.setGravity(Gravity.RIGHT);
} else if (input.contentEquals("right")) {
tview.setGravity(Gravity.CENTER);
}
}
});
}
}
You set this text view a width of "wrap_content" it means, what ever the text is, the view take the size of the text.
and in the LinearLayout , the default gravity (used here) is 'center'
you should try this :
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent" <= change this
android:layout_height="wrap_content"
android:text="#string/invalid"
android:gravity="center" <= then this gravity will be taken into account
/>
99% of the time, not working properly == not used properly.
You are mistaking gravity and layout_gravity.
gravity is the way the text will align itself in the TextView. The TextView being in wrap_content this does nothing, as the TextView is exactly the size of the text.
layout_gravity is the way the TextView will align itself in its parent, in your case in the vertical LinearLayout
You have given your TextView width wrap_content, and that is the problem, Check below code and replace it to your code.
<TextView
android:id="#+id/txtInvalid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/invalid"
android:gravity="center"
/>
One reason why gravity does not work properly can be if you have gravity and textAlignment
If you put it like this
<TextView
android:id="#+id/textView1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center|center_horizontal"
android:textAlignment="viewStart"
Android studio editor will give this warning:
Inconsistent alignment specification between textAlignment and gravity attributes: was center|center_horizontal, expected start (Incompatible direction here)
But if you add textAlignment inside the styles then it doesn't complain.
This can be the issue why your gravity value will not work
<TextView
android:id="#+id/textView2"
style="#style/Widget.App.TextView.Label.Default"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center|center_horizontal"
Check this answer for the difference between them:
Text/Layout Alignment in Android (textAlignment, gravity)
set android:layout_width="fill_parent" for textView1
Make sure you don't have something like that hanging around
app:layout_box="left|top"

Android - Problem with ViewFlipper

I am making a game that requires multiple views within an Activity, and i decided to use Viewflipper to do.
The thing is. I need to have 3 views in in the viewflipper, and the LAST view will transfer back to the first one.
My problem is that the buttons are acting weird, and they are either not going to the next, or skipping the third view. I tried to put vf.setDisplayedChild(R.Id.gamescreen1); at the last view but then the whole thing breaks down.
Thanks for all answers in advance, i have been stuck with this problem for DAYS! and yes, i know that i am a noob :(
[SOURCECODE]
public class GameActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_game);
final ViewFlipper vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
//SCREEN 1
Button btnSTART = (Button) findViewById(R.id.btnSTART);
//SCREEN 2
Button btnALT1 = (Button) findViewById(R.id.btnALT1);
Button btnALT2 = (Button) findViewById(R.id.btnALT1);
//SCREEN 3
Button btnALT3 = (Button) findViewById(R.id.btnALT1);
Button btnALT4 = (Button) findViewById(R.id.btnALT1);
//screen 1
btnSTART.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
//screen 2 // Either button will go to view 3
btnALT1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
btnALT2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
//screen 3 // Either button will go back to view 1
btnALT3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
btnALT4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vf.showNext();
}
});
}
}
[XML]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/gamescreen1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="435dp" android:gravity="top">
<ListView android:layout_width="fill_parent" android:id="#+id/list1" android:layout_height="184dp" android:layout_weight="0.53"></ListView>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:gravity="bottom|center" android:layout_height="wrap_content">
<Button android:layout_height="wrap_content" android:id="#+id/btnSTART" android:layout_width="200dp" android:text="#string/btnstart"></Button>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/gamescreen2" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/list2"></ListView>
</LinearLayout>
<LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="alt1" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT1"></Button>
<Button android:text="alt2" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT2"></Button>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/gamescreen3" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:weightSum="1" android:gravity="top" android:layout_height="326dp">
</LinearLayout>
<LinearLayout android:orientation="vertical" android:gravity="bottom|center" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:text="alt3" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT3"></Button>
<Button android:text="alt4" android:layout_width="200dp" android:layout_height="wrap_content" android:id="#+id/btnALT4"></Button>
</LinearLayout>
</LinearLayout>
try this
flipper.setDisplayedChild(1);
.
.to
.
flipper.setDisplayedChild(3);
if(flipper.getCurrentView() == 3)
{
flipper.setDisplayedChild(1);
}
setDisplayChild takes an integer parameter that is the zero based index of the child to display NOT the id of the child to display. A bit confusing I know.

Categories