How can I put numbers In several TextViews? - android

Hey I have gridview buttons like keyboard with green light if I click on one of them turns to red anyways and I have 4 TextViews so I want when I press a number text it on textview1 then second number goes to txt2 the third to txt3 the fourth to txt4 but the txt4 must be changeable if I pressed on another number automatically changes and it is important that I can't use same number for example in txt1 I pressed number 1 the button goes red so I can't use it in txt2,3,4 Until I delete it from txt1 look at my picture and if someone want the code i'm happy to put it
(and how automatically jumps to the next textview after one number)
MainActivity.java this helps me with edit text I want it for TextView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (TextView)findViewById(R.id.txt1);
editText = (EditText) findViewById(R.id.editText);
b1 = (Button) findViewById(R.id.n1);
b2 = (Button) findViewById(R.id.n2);
b3 = (Button) findViewById(R.id.n3);
b4 = (Button) findViewById(R.id.n4);
b5 = (Button) findViewById(R.id.n5);
b6 = (Button) findViewById(R.id.n6);
b7 = (Button) findViewById(R.id.n7);
b8 = (Button) findViewById(R.id.n8);
b9 = (Button) findViewById(R.id.n9);
b0 = (Button) findViewById(R.id.n0);
ent = (Button) findViewById(R.id.enter);
clr = (Button) findViewById(R.id.Clear);
buttonEffect(b0);
buttonEffect(b1);
buttonEffect(b2);
buttonEffect(b3);
buttonEffect(b4);
buttonEffect(b5);
buttonEffect(b6);
buttonEffect(b7);
buttonEffect(b8);
buttonEffect(b9);
/* b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
b8.setVisibility(View.INVISIBLE);
b9.setVisibility(View.INVISIBLE);
b0.setVisibility(View.INVISIBLE);
ent.setVisibility(View.INVISIBLE);
clr.setVisibility(View.INVISIBLE);*/
//for default keyboard don't appear
editText.setShowSoftInputOnFocus(false);
editText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
b1.setVisibility(View.VISIBLE);
b2.setVisibility(View.VISIBLE);
b3.setVisibility(View.VISIBLE);
b4.setVisibility(View.VISIBLE);
b5.setVisibility(View.VISIBLE);
b6.setVisibility(View.VISIBLE);
b7.setVisibility(View.VISIBLE);
b8.setVisibility(View.VISIBLE);
b9.setVisibility(View.VISIBLE);
b0.setVisibility(View.VISIBLE);
ent.setVisibility(View.VISIBLE);
clr.setVisibility(View.VISIBLE);
return false;
}
});
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
textContainer = s.toString();
prevLength = s.length();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editText.setSelection(editText.getText().length());
}
#Override
public void afterTextChanged(Editable s) {
length = s.length();
if (!textContainer.isEmpty()) {
if (s.length() > 1) {
if (prevLength < length) {
if (!textContainer.contains(s.toString().subSequence(length - 1, length))) {
length = s.length();
} else {
editText.getText().delete(length - 1, length);
}
}
}
} else {
textContainer = s.toString();
}
}
});
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "0"));
b0.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String zero = "0";
txt1.setText(zero);
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "1"));
b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String one = "1";
txt1.setText(one);
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "2"));
b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String two = "2";
txt1.setText(two);
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "3"));
b3.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String three = "3";
txt1.setText(three);
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "4"));
b4.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String four = "4";
txt1.setText(four);
}
});
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "5"));
b5.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String five = "5";
txt1.setText(five);
}
});
b6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "6"));
b6.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String six = "6";
txt1.setText(six);
}
});
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "7"));
b7.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String seven = "7";
txt1.setText(seven);
}
});
b8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "8"));
b8.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String eight = "8";
txt1.setText(eight);
}
});
b9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().insert(editText.getText().length(), "9"));
b9.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
String nine = "9";
txt1.setText(nine);
}
});
ent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
clr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clr.setPressed(true);
int length = editText.getText().length();
if (length > 0) {
editText.getText().delete(length - 1, length);
}
if (editText.getText().toString().contains("0")) {
b0.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b0.setEnabled(false);
}
else{
b0.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b0.setEnabled(true);
}
if (editText.getText().toString().contains("1")) {
b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b1.setEnabled(false);
}
else{
b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b1.setEnabled(true);
}
if (editText.getText().toString().contains("2")) {
b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b2.setEnabled(false);
}
else{
b2.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b2.setEnabled(true);
}
if (editText.getText().toString().contains("3")) {
b3.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b3.setEnabled(false);
}
else{
b3.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b3.setEnabled(true);
}
if (editText.getText().toString().contains("4")) {
b4.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b4.setEnabled(false);
}
else{
b4.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b4 .setEnabled(true);
}
if (editText.getText().toString().contains("5")) {
b5.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b5.setEnabled(false);
}
else{
b5.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b5.setEnabled(true);
}
if (editText.getText().toString().contains("6")) {
b6.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b6.setEnabled(false);
}
else{
b6.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b6.setEnabled(true);
}
if (editText.getText().toString().contains("7")) {
b7.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b7.setEnabled(false);
}
else{
b7.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b7.setEnabled(true);
}
if (editText.getText().toString().contains("8")) {
b8.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b8.setEnabled(false);
}
else{
b8.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b8.setEnabled(true);
}
if (editText.getText().toString().contains("9")) {
b9.setBackgroundDrawable(getResources().getDrawable(R.drawable.redww));
b9.setEnabled(false);
}
else{
b9.setBackgroundDrawable(getResources().getDrawable(R.drawable.greenww));
b9.setEnabled(true);
}
}
});
}
public static void buttonEffect(View button){
button.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
v.getBackground().setColorFilter(0xe0ffffff, PorterDuff.Mode.SRC_ATOP);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP: {
v.getBackground().clearColorFilter();
v.invalidate();
break;
}
}
return false;
}
});
}
#Override
public void onBackPressed() {
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
b8.setVisibility(View.INVISIBLE);
b9.setVisibility(View.INVISIBLE);
b0.setVisibility(View.INVISIBLE);
ent.setVisibility(View.INVISIBLE);
clr.setVisibility(View.INVISIBLE);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
b8.setVisibility(View.INVISIBLE);
b9.setVisibility(View.INVISIBLE);
b0.setVisibility(View.INVISIBLE);
ent.setVisibility(View.INVISIBLE);
clr.setVisibility(View.INVISIBLE);
return super.onTouchEvent(event);
}
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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.mike.keyboardtest.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="350dp"
android:gravity="bottom"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/linearLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:orientation="horizontal">
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:clickable="true"
android:minHeight="80dp"
android:text="1"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:clickable="true"
android:minHeight="80dp"
android:text="2"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:clickable="true"
android:minHeight="80dp"
android:text="3"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#112"
android:minHeight="80dp"
android:text="clear"
android:clickable="true"
android:textColor="#color/colorPrimaryDark"
android:textSize="25sp"
android:textStyle="bold"
android:id="#+id/button_clear" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<Button
android:id="#+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:clickable="true"
android:minHeight="80dp"
android:text="4"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:clickable="true"
android:minHeight="80dp"
android:text="5"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/button_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:minHeight="80dp"
android:clickable="true"
android:text="6"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/button_enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ccc"
android:minHeight="80dp"
android:clickable="true"
android:text="enter"
android:textColor="#color/colorPrimaryDark"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<Button
android:id="#+id/button_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:minHeight="80dp"
android:clickable="true"
android:text="7"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/button_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:minHeight="80dp"
android:clickable="true"
android:text="8"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/button_9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:minHeight="80dp"
android:clickable="true"
android:text="9"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/button_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#drawable/button_selector"
android:minHeight="80dp"
android:clickable="true"
android:text="0"
android:textColor="#333"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:id="#+id/text_1"
android:layout_width="0dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#drawable/greenww"
android:layout_weight="1"/>
<TextView
android:id="#+id/text_2"
android:layout_width="0dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#drawable/greenww"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="#+id/text_3"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#drawable/greenww"
android:gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"/>
<TextView
android:id="#+id/text_4"
android:layout_width="0dp"
android:background="#drawable/greenww"
android:layout_height="60dp"
android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>

int click = 0;
TextView tv [] = new TextView[4];
onCreate:
tv[0] = (TextView) findViewById(R.id.tv1);
tv[1] = (TextView) findViewById(R.id.tv2);
tv[2] = (TextView) findViewById(R.id.tv3);
tv[3] = (TextView) findViewById(R.id.tv4);
onClick:
if(click <= 3) {
switch (v.getId()){
case R.id.n1:
number = "1";
clickCount.add(0);
btn[0].setEnabled(false);
break;
}
setTv(number, click);
click++;
}
metod:
public void setTv(String str, int click){
switch (click){
case 0:
tv[0].setText(str);
break;
case 1:
tv[1].setText(str);
break;
case 2:
tv[2].setText(str);
break;
case 3:
tv[3].setText(str);
break;
}
}
delete button in:
if(click != 0) {
tv[--click].setText("");
}
xml file:
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="400dp"
android:gravity="bottom"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="true"
android:focusableInTouchMode="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:orientation="horizontal">
<Button
android:id="#+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:text="1"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:minHeight="80dp"
android:text="2"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/n3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:minHeight="80dp"
android:text="3"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#112"
android:minHeight="80dp"
android:text="clear"
android:textColor="#color/colorPrimaryDark"
android:textSize="25sp"
android:textStyle="bold"
android:id="#+id/Clear" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<Button
android:id="#+id/n4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:text="4"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/n5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:minHeight="80dp"
android:text="5"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/n6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:minHeight="80dp"
android:text="6"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ccc"
android:minHeight="80dp"
android:text="enter"
android:textColor="#color/colorPrimaryDark"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<Button
android:id="#+id/n7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:text="7"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/n8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:minHeight="80dp"
android:text="8"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/n9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:minHeight="80dp"
android:text="9"
android:textColor="#333"
android:textSize="25sp" />
<Button
android:id="#+id/n0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:minHeight="80dp"
android:text="0"
android:textColor="#333"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="4"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:id="#+id/tv1"
android:layout_width="0dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#drawable/tvstroke"
android:layout_weight="1"/>
<TextView
android:id="#+id/tv2"
android:layout_width="0dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#drawable/tvstroke"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="#+id/tv3"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#drawable/tvstroke"
android:gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"/>
<TextView
android:id="#+id/tv4"
android:layout_width="0dp"
android:background="#drawable/tvstroke"
android:layout_height="60dp"
android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
TextView stroke:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp"
android:color="#color/colorPrimary"/>
</shape>
Main Activity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
int click = 0;
TextView tvKeyBoard;
TextView tv [] = new TextView[4];
Button btn[] = new Button[10];
ArrayList<Integer> clickCount = new ArrayList<>();
String fullNamber = "";
Button del, clear;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
del = (Button) findViewById(R.id.enter);
clear = (Button) findViewById(R.id.Clear);
tv[0] = (TextView) findViewById(R.id.tv1);
tv[1] = (TextView) findViewById(R.id.tv2);
tv[2] = (TextView) findViewById(R.id.tv3);
tv[3] = (TextView) findViewById(R.id.tv4);
btn[0] = (Button) findViewById(R.id.n1);
btn[1] = (Button) findViewById(R.id.n2);
btn[2] = (Button) findViewById(R.id.n3);
btn[3] = (Button) findViewById(R.id.n4);
btn[4] = (Button) findViewById(R.id.n5);
btn[5] = (Button) findViewById(R.id.n6);
btn[6] = (Button) findViewById(R.id.n7);
btn[7] = (Button) findViewById(R.id.n8);
btn[8] = (Button) findViewById(R.id.n9);
btn[9] = (Button) findViewById(R.id.n0);
for (Button b : btn){
b.setOnClickListener(this);
}
del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(clickCount.size() != 0) {
btn[clickCount.get(clickCount.size()-1)].setEnabled(true);
clickCount.remove(clickCount.size()-1);
}
if(click != 0) {
tv[--click].setText("");
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < clickCount.size(); i++ ){
btn[clickCount.get(i)].setEnabled(true);
}
clickCount.clear();
}
});
}
#Override
public void onClick(View v) {
String number = "";
if(click <= 3) {
switch (v.getId()){
case R.id.n1:
number = "1";
clickCount.add(0);
btn[0].setEnabled(false);
break;
case R.id.n2:
number = "2";
clickCount.add(1);
btn[1].setEnabled(false);
break;
case R.id.n3:
number = "3";
clickCount.add(2);
btn[2].setEnabled(false);
break;
case R.id.n4:
number = "4";
clickCount.add(3);
btn[3].setEnabled(false);
break;
case R.id.n5:
number = "5";
clickCount.add(4);
btn[4].setEnabled(false);
break;
case R.id.n6:
number = "6";
clickCount.add(5);
btn[5].setEnabled(false);
break;
case R.id.n7:
number = "7";
clickCount.add(6);
btn[6].setEnabled(false);
break;
case R.id.n8:
number = "8";
clickCount.add(7);
btn[7].setEnabled(false);
break;
case R.id.n9:
number = "9";
clickCount.add(8);
btn[8].setEnabled(false);
break;
case R.id.n0:
number = "0";
clickCount.add(9);
btn[9].setEnabled(false);
break;
}
setTv(number, click);
click++;
}
}
#Override
public void onBackPressed() {
if(del.isEnabled()){
for (Button b : btn){
b.setVisibility(View.INVISIBLE);
}
del.setVisibility(View.INVISIBLE);
clear.setVisibility(View.INVISIBLE);
}else
super.onBackPressed();
}
public void setTv(String str, int count){
switch (count){
case 0:
tv[0].setText(str);
break;
case 1:
tv[1].setText(str);
break;
case 2:
tv[2].setText(str);
break;
case 3:
tv[3].setText(str);
break;
}
}
}

For brevity, I've only chosen five buttons.
MainActivity
public class MainActivity extends Activity {
List<String> values = new ArrayList<>();
String[] values = new String[]{" ", " ", " ", " "};
TextView textView[] = new TextView[4];
int size = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
textView[0] = (TextView) findViewById(R.id.text_1);
textView[1] = (TextView) findViewById(R.id.text_2);
textView[2] = (TextView) findViewById(R.id.text_3);
textView[3] = (TextView) findViewById(R.id.text_4);
// bind your buttons views here
// for ex:
button0 = (Button) findViewById(R.id.button_0);
// so on up to button 9
// ...
//Also bind views for Clear button and enter
}
// This method is set statically in xml file check below
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_0:
setText(0);
button0.setEnabled(false);
break;
case R.id.button_1:
setText(1);
button1.setEnabled(false);
break;
case R.id.button_2:
setText(2);
button2.setEnabled(false);
break;
case R.id.button_3:
setText(3);
button3.setEnabled(false);
break;
case R.id.button_4:
setText(4);
button4.setEnabled(false);
break;
case R.id.button_clear:
if (size == 0) {
return;
}
String numberToCleared = values[size - 1];
clearButton(Integer.parseInt(numberToCleared));
size--;
values[size] = " ";
bindText();
break;
case R.id.button_enter:
//do what you want to do here
break;
}
}
private void clearButton(int number) {
switch (number) {
case 0:
button0.setEnabled(true);
break;
case 1:
button1.setEnabled(true);
break;
case 2:
button2.setEnabled(true);
break;
case 3:
button3.setEnabled(true);
break;
case 4:
button4.setEnabled(true);
break;
}
}
public void bindText() {
for (int i = 0; i < 4; i++) {
textView[i].setText(values[i]);
}
}
public void setText(int number) {
if (size == 4) {
clearButton(Integer.parseInt(values[3]));
size--;
}
values[size] = String.valueOf(number);
size++;
bindText();
}
}
layout.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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/text_1"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:padding="4dp"/>
<TextView
android:id="#+id/text_2"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:padding="4dp"/>
<TextView
android:id="#+id/text_3"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:padding="4dp"/>
<TextView
android:id="#+id/text_4"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:padding="4dp"/>
</LinearLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="vertical"
android:rowCount="2">
<Button
android:id="#+id/button_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:onClick="onClick"
android:text="0"/>
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:onClick="onClick"
android:text="1"/>
<Button
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:onClick="onClick"
android:text="2"/>
<Button
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:onClick="onClick"
android:text="3"/>
<Button
android:id="#+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:onClick="onClick"
android:text="4"/>
<Button
android:id="#+id/button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:onClick="onClick"
android:text="Clear"/>
<Button
android:id="#+id/button_enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:onClick="onClick"
android:text="Enter"/>
</GridLayout>
</LinearLayout>
button_selector.xml Place this in your drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/green" android:state_enabled="true"/>
<item android:drawable="#color/red" android:state_enabled="false"/>
<item android:drawable="#color/green"/>
</selector>
Logic (as OP asked for it)
The values array stores the numbers that are supposed to be displayed in TextViews.
When a button is pressed the values array is updated in the setText() method.
When clear is pressed this is what happens -
if (size == 0) {
return; // if size is 0 do nothing.
}
String numberToCleared = values[size - 1];
//You've to clear the button color. So, get the last value in array
//and send it to clearButton() function.
clearButton(Integer.parseInt(numberToCleared));
//remove the number from values array and decrement size
size--;
values[size] = " ";
bindText();
break;

Related

ListView is showing only 1 data while there are 3 records coming in the listview adapter

I want a listview in a tabhost. My data is coming from the database. Through retrofit, I am getting 3 records from the database. I am passing these 3 records to the ListView Adapter that I have created. These records are coming till the constructor of Adapter but after that in the getView method only 1 record is accessed 3 times. I am not sure why this is happening.
This is my Post Activity:
public static final ArrayList<WorkProfilePojo> mProfiles = new ArrayList<>();
BaseURL baseURL = new BaseURL();
VendorPostAdapter pAdapter;
ListView mPostList;
public List<WorkProfilePojo> returnedList = new ArrayList<>();
String lv_vendorId = null;
public static String lv_name;
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
lv_vendorId = intent.getStringExtra("lv_vendorId");
Log.e("vendor id", lv_vendorId);
lv_name = intent.getStringExtra("lv_name");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
getRetrofit();
}
private void getRetrofit() {
Retrofit retrofit = new RetrofitObject().getRetrofit();
final WorkProfileforPostTabAPI mProfileAPI =
retrofit.create(WorkProfileforPostTabAPI.class);
final Call<ArrayList<WorkProfilePojo>> mCall =
mProfileAPI.getWork(lv_vendorId);
mCall.enqueue(new Callback<ArrayList<WorkProfilePojo>>() {
#Override
public void onResponse(Call<ArrayList<WorkProfilePojo>> call,
Response<ArrayList<WorkProfilePojo>> response) {
mProfiles.clear();
returnedList = (ArrayList<WorkProfilePojo>)response.body();
WorkProfilePojo wp;
Log.e("Teste2",
returnedList.get(0).getLv_eventSubCategory());
for (int i = 0; i<= returnedList.size()-1; i++){
wp=new WorkProfilePojo();
wp.setLv_vendorWorkId(returnedList.get(i).getLv_vendorWorkId());
wp.setLv_eventSubCategory(returnedList.get(i).getLv_eventSubCategory());
wp.setLv_workDescription(returnedList.get(i).getLv_workDescription());
wp.setLv_numWorkLikes(returnedList.get(i).getLv_numWorkLikes());
wp.setLv_numWorkComments(returnedList.get(i).getLv_numWorkComments());
mProfiles.add(wp);
Log.e("retrofit profile size: ",
String.valueOf(mProfiles.size()));
populateListView(mProfiles);
}
#Override
public void onFailure(Call<ArrayList<WorkProfilePojo>> call,
Throwable t) {
Log.e(TAG, "FAIL");
}
});
}
private void populateListView(ArrayList<WorkProfilePojo> mProfiles) {
mPostList = (ListView) findViewById(R.id.listVPost);
Log.e("func prof size: ", String.valueOf(mProfiles.size()));
pAdapter = new VendorPostAdapter(this, mProfiles, lv_name);
mPostList.setAdapter(pAdapter);
}
This is my Adapter:
public class VendorPostAdapter extends BaseAdapter {
Context context;
ArrayList<WorkProfilePojo> lv_profiles = new ArrayList<>();
String lv_name;
LayoutInflater inflater;
public VendorPostAdapter(Context context, ArrayList<WorkProfilePojo>
lv_profiles, String lv_name){
this.context = context;
this.lv_profiles =lv_profiles;
this.lv_name = lv_name;
Log.e("adapter name", lv_name );
Log.e("adapter workid", lv_profiles.get(0).getLv_vendorWorkId());
Log.e("adapter workid", lv_profiles.get(1).getLv_vendorWorkId());
Log.e("adapter workid", lv_profiles.get(2).getLv_vendorWorkId());
}
private class ViewHolder {
TextView mtxtViewPartnerName;
TextView mtxtViewEventCategory;
TextView mtxtViewDate ;
TextView mtxtViewFillDescription;
GridView mgrdViewPhotos ;
ImageView mimgLike ;
ImageView mimgPostProfilePic ;
ImageView mimgShare ;
ImageView mimgComment ;
ImageView mimgLikeThumb ;
TextView mtxtNoOfLikes ;
TextView mtxtNoOfComments ;
TextView mtxtComments;
}
#Override
public int getCount() {
return lv_profiles.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
inflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.content_post, parent,
false);
holder = new ViewHolder();
holder.mtxtViewPartnerName = (TextView)
convertView.findViewById(R.id.txtViewPartnerName);
holder.mtxtViewEventCategory= (TextView)
convertView.findViewById(R.id.txtViewEventCategory);
holder.mtxtViewDate = (TextView)
convertView.findViewById(R.id.txtViewDate);
holder.mtxtViewFillDescription = (TextView)
convertView.findViewById(R.id.txtViewFillDescription);
holder.mgrdViewPhotos = (GridView)
convertView.findViewById(R.id.grdViewPhotos);
holder.mimgLike = (ImageView)
convertView.findViewById(R.id.imgLike);
holder.mimgPostProfilePic = (ImageView)
convertView.findViewById(R.id.imgPostProfilePic);
holder.mimgShare = (ImageView)
convertView.findViewById(R.id.imgShare);
holder.mimgLikeThumb = (ImageView)
convertView.findViewById(R.id.imgLikeThumb);
holder.mimgComment = (ImageView)
convertView.findViewById(R.id.imgComment);
holder.mtxtNoOfLikes = (TextView)
convertView.findViewById(R.id.txtViewNoOfLikes);
holder.mtxtNoOfComments = (TextView)
convertView.findViewById(R.id.txtViewNoOfComments);
holder.mtxtComments = (TextView)
convertView.findViewById(R.id.txtViewComments);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
final WorkProfilePojo wp = lv_profiles.get(position);
Log.e("getView name", lv_name );
Log.e("getView workid",
lv_profiles.get(position).getLv_vendorWorkId());
holder.mtxtViewPartnerName.setText( lv_name );
holder.mtxtViewEventCategory.setText( wp.getLv_eventSubCategory() );
FormatDate lv_date = new FormatDate();
holder.mtxtViewDate.setText(lv_date.formatDayMonDateYr(wp.getLv_creationDate()));
holder.mtxtViewFillDescription.setText(wp.getLv_workDescription());
holder.mtxtViewFillDescription.setText(wp.getLv_workDescription());
holder.mimgLikeThumb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,
VendorWorkLikesActivity.class);
intent.putExtra("lv_workId", wp.getLv_vendorWorkId());
Log.e("postad workid", wp.getLv_vendorWorkId());
context.startActivity(intent);
}
});
holder.mtxtComments.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,
VendorWorkCommentActivity.class);
context.startActivity(intent);
}
});
return convertView;
}
}
This is my activity_post.xml wrapped in relative layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundcolour"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="1"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
/>
</RelativeLayout>
This is my content_post for line item wrapped in relative layout:
<RelativeLayout
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgPostProfilePic"
android:src="#drawable/profileicon"
android:layout_width="60dp"
android:layout_height="60dp" />
<TextView
android:id="#+id/txtViewPartnerName"
style="#style/InputLable"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_toEndOf="#id/imgPostProfilePic"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="partner Name" />
<TextView
android:id="#+id/txtViewManageWork"
style="#style/Keywords"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:layout_marginTop="5dp"
android:text="Managed" />
<TextView
android:id="#+id/txtViewEventCategory"
style="#style/InputLable"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginLeft="220dp"
android:paddingRight="5dp"
android:layout_marginTop="5dp"
android:text="" />
<TextView
android:id="#+id/txtViewEvent"
style="#style/Keywords"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_toEndOf="#id/txtViewEventCategory"
android:layout_marginTop="5dp"
android:text="Event" />
<TextView
android:id="#+id/txtViewDate"
style="#style/InputLable"
android:layout_width="100dp"
android:layout_height="25dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
android:text="Date" />
<TextView
android:id="#+id/txtViewDescription"
style="#style/Keywords"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="80dp"
android:text="Work Description" />
<TextView
android:id="#+id/txtViewFillDescription"
style="#style/InputLable"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp"
android:text="XYZ" />
<TextView
android:id="#+id/txtViewPhotos"
style="#style/Keywords"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="130dp"
android:text="Photos" />
<GridView
android:id="#+id/grdViewPhotos"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:numColumns="auto_fit"
android:layout_below="#id/txtViewPhotos">
</GridView>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:id="#+id/divider"
android:background="#color/colorDarkGray"
android:layout_below="#id/grdViewPhotos"
android:layout_weight="0"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dividerlayout"
android:orientation="horizontal"
android:layout_below="#id/divider"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<ImageView
android:id="#+id/imgLikeThumb"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_below="#id/grdViewPhotos"
android:clickable="true"
android:src="#drawable/likethumb" />
<TextView
android:id="#+id/txtViewNoOfLikes"
style="#style/InputLable"
android:layout_width="30dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:layout_marginTop="5dp"
android:text="0" />
<TextView
android:id="#+id/txtViewNoOfComments"
style="#style/InputLable"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="5dp"
android:textAlignment="center"
android:text="0" />
<TextView
android:id="#+id/txtViewComments"
style="#style/InputLable"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_margin="5dp"
android:text="Comments" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:id="#+id/divider1"
android:background="#color/colorDarkGray"
android:layout_below="#id/dividerlayout"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="0"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dividerlayout1"
android:layout_below="#id/dividerlayout"
android:layout_marginTop="5dp">
<ImageView
android:id="#+id/imgLike"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="#id/divider1"
android:layout_marginTop="5dp"
android:clickable="true"
android:src="#drawable/likeicon2" />
<TextView
android:id="#+id/txtViewLike"
style="#style/InputLable"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#id/divider1"
android:text="Likes" />
<ImageView
android:id="#+id/imgShare"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="60dp"
android:layout_below="#id/divider1"
android:layout_centerInParent="true"
android:layout_marginTop="5dp"
android:clickable="true"
android:src="#drawable/shareicon3" />
<TextView
android:id="#+id/txtViewShare"
style="#style/InputLable"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#id/divider1"
android:text="Share" />
<ImageView
android:id="#+id/imgComment"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="45dp"
android:layout_below="#id/divider1"
android:layout_marginTop="5dp"
android:clickable="true"
android:src="#drawable/commenticon" />
<TextView
android:id="#+id/txtViewComment"
style="#style/InputLable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#id/divider1"
android:text="Comment" />
</LinearLayout>
</RelativeLayout>
From retrofit results, I am getting 3 records from database:
func workid:: W00000000000013
func workid:: W00000000000014
func workid:: W00000000000015
But in getView() method I am getting only 1 record coming 3 times:
Likesad name: W00000000000013
Likesad name: W00000000000013
Likesad name: W00000000000013
holder.mtxtViewDate.setText(lv_date.formatDayMonDateYr(wp.getLv_creationDate(getPostion())));
holder.mtxtViewFillDescription.setText(wp.getLv_workDescription(getPostion()));
holder.mtxtViewFillDescription.setText(wp.getLv_workDescription(getPostion()));
This Might work.

Display corresponding details of user when selected from list

I have developed a screen where a no. of people from the database are displayed in a list view. I want to display the profile page of the selected person. So my question is how to bind each detail of the selected person like name, contact, etc. to the profile page which I have created? Will I have to call the getById API in the onItemClickListener?
Here's the edited code:-
public class Test extends AppCompatActivity {
List<Genie> genieList;
GenieAdapter genieAdapter;
TextView responseView;
ProgressBar progressBar;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
responseView = (TextView) findViewById(R.id.responseView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
button = (Button) findViewById(R.id.test);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Test.this, "Blahblah", Toast.LENGTH_LONG).show();
new RetrieveFeedTask().execute();
}
});
}
class RetrieveFeedTask extends AsyncTask<Void, Void, List<Genie>> {
private Exception exception;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
responseView.setText("");
}
protected List<Genie> doInBackground(Void... urls) {
GenieService genieService = new GenieService();
return genieService.getAll();
}
protected void onPostExecute(List<Genie> genies) {
if (genies == null) {
new ArrayList<Genie>(); // "THERE WAS AN ERROR"
} else {
progressBar.setVisibility(View.GONE);
Log.i("INFO", genies.get(0).name);
List<String> rows = genies.stream().map(genie -> getRow(genie)).collect(Collectors.toList());
genieAdapter=new GenieAdapter(getApplicationContext(),R.layout.genie_list, genies);
ListView list=(ListView)findViewById(R.id.listViewMain);
list.setAdapter(genieAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Test.this, "" + position, Toast.LENGTH_SHORT).show();
// if (position == 1) {
// startActivity(new Intent(Test.this, viewGenie1.class));
// }
}
});
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("name", "%s");
intent.putExtra("add", "%s");
intent.putExtra("phn", "%s");
intent.putExtra("sal", "%s");
intent.putExtra("lea", "%s");
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
private String getRow(Genie g) {
return String.format("%s, %s, %s, %s, %s", g.name, g.salary, g.contact, g.paid_leaves, g.address);
}
}
}
Here's the viewGenie1.class:-
public class viewGenie1 extends AppCompatActivity implements View.OnClickListener {
TextView name;
EditText address, contact, salary, leaves;
Button attendance;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_genie1);
name = (TextView) findViewById(R.id.txName);
address = (EditText) findViewById(R.id.txAddress);
contact = (EditText) findViewById(R.id.txContact);
salary = (EditText) findViewById(R.id.txSalary);
leaves = (EditText) findViewById(R.id.txLeaves);
Button update=(Button)findViewById(R.id.btUpdate);
update.setOnClickListener(this);
Button delete=(Button)findViewById(R.id.delete);
delete.setOnClickListener(this);
Button attendance = (Button) findViewById(R.id.attendance);
attendance.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAtt();
}
});
String value = "";
if (getIntent().hasExtra("name")) {
String name = getIntent().getExtras().getString("name");
String add = getIntent().getExtras().getString("add");
String phn = getIntent().getExtras().getString("phn");
String sal = getIntent().getExtras().getString("sal");
String lea = getIntent().getExtras().getString("lea");
}
name.setText(value);
address.setText(value);
contact.setText(value);
salary.setText(value);
leaves.setText(value);
}
#Override
public void onClick(View view) {
final AlertDialog.Builder builder=new AlertDialog.Builder(viewGenie1.this);
builder.setMessage("Are you sure you want to delete records?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
new deleteTask().execute();
Toast.makeText(viewGenie1.this, "Genie deleted..!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(viewGenie1.this, navDrawer.class));
// GenieService genieService=new GenieService();
// genieService.delete(2);
// Log.d("Information", String.valueOf(genieService.delete(2)));
// Log.i("INFO", genies.get(0).name);
// startActivity(new Intent(viewGenie1.this,Test.class));
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
private class deleteTask extends AsyncTask {
#Override
protected Object doInBackground(Object[] objects) {
GenieService genieService = new GenieService();
return genieService.delete(6);
}
}
public void showAtt() {
Intent intent = new Intent(this, viewAbsentee.class);
startActivity(intent);
}
}
Here's the xml file of the profile page I have created with hard coded values but want to display the actual values from the local mysql database using an API call:-
<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="#drawable/bcak"
tools:context="com.codionics.geniem.AddGenie"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="313dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/gradientbackground"
android:orientation="vertical">
<ImageView
android:layout_width="117dp"
android:layout_height="117dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:src="#drawable/genie" />
<TextView
android:id="#+id/txName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Abc"
android:textColor="#ffffff"
android:textSize="21sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="115dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="175dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact"
android:textColor="#f000"
android:textStyle="bold"
android:textSize="20sp" />
<EditText
android:id="#+id/txContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="123456789"
android:textColor="#3F51B5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#f000"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="#+id/txAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="Pune"
android:textColor="#3F51B5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
<LinearLayout
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="#drawable/ic_attach_money_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:text="Paid leaves : "
android:textColor="#303F9F"
android:textSize="27dp"
android:textStyle="bold" />
<EditText
android:id="#+id/txLeaves"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:layout_weight="1"
android:text=" 5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="#drawable/ic_money" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:text="Salary : "
android:textColor="#303F9F"
android:textSize="27dp"
android:textStyle="bold" />
<EditText
android:id="#+id/txSalary"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:text=" 5000"
android:textColor="#123" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="30dp"
android:background="#drawable/buttonstylegradient"
android:text="Update Genie"
android:textColor="#fff" />
<Button
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="80dp"
android:layout_marginTop="-50dp"
android:background="#drawable/buttonstylegradient"
android:text="Delete Genie"
android:textColor="#fff" />
<Button
android:id="#+id/attendance"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonstylegradient"
android:textColor="#fff"
android:text="Attendance" />
</LinearLayout>
I want to display the details in the a profile page like this:-
profile page
Please pass the value in Intent using putExtra()
Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("key","Value"); //Key must be unique and value should be the value which you want to pass to viewGenie1 class.
startActivity(intent);
In viewGenie1 class you can get the value like this
String value="";
if(getIntent().hasExtra("key")) {
value = getIntent().getExtras().getString("key");
}
Please replace
String value = "";
if (getIntent().hasExtra("name")) {
String name = getIntent().getExtras().getString("name");
String add = getIntent().getExtras().getString("add");
String phn = getIntent().getExtras().getString("phn");
String sal = getIntent().getExtras().getString("sal");
String lea = getIntent().getExtras().getString("lea");
}
name.setText(value);
address.setText(value);
contact.setText(value);
salary.setText(value);
leaves.setText(value);
To
String mName = "",mAdd="",mPhn="",mSal="",mLea="";
if (getIntent().hasExtra("name")) {
mName = getIntent().getExtras().getString("name");
mAdd = getIntent().getExtras().getString("add");
mPhn = getIntent().getExtras().getString("phn");
mSal = getIntent().getExtras().getString("sal");
mLea = getIntent().getExtras().getString("lea");
}
name.setText(mName);
address.setText(mAdd);
contact.setText(mPhn);
salary.setText(mSal);
leaves.setText(mLea);

UI glitch in Listview android

I'm implementing a ListView and on quick scrolling in large lists this glitch occurs. I am using ViewHolder Pattern
[![UI Glitch in ListView][1]][1]
[1]:
getView :
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(mResource, parent, false);
viewHolder = new ViewHolder();
viewHolder.titleView = (TextView) convertView.findViewById(R.id.sheet_name_text_view);
viewHolder.rowsCount = (TextView) convertView.findViewById(R.id.rows_count_text_view);
viewHolder.selectMessage = (TextView) convertView.findViewById(R.id.row_info_text);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final ImportedTable item = getItem(position);
if (item != null) {
viewHolder.titleView.setText(item.getTableName());
int columnsCount = 0;
for (boolean isSelected : item.getSelectedColumns()) {
if (isSelected) {
columnsCount++;
}
}
viewHolder.rowsCount.setText(context.getString(R.string.label_rowscounttext, (item.getRecordsCount() - item.getHeaderRow()), columnsCount));
}
final CheckBox isSelectedForImport = (CheckBox) convertView.findViewById(R.id.import_table_list_toggle_button);
RelativeLayout checkboxLayout = (RelativeLayout) convertView.findViewById(R.id.import_table_list_toggle_layout);
checkboxLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isSelectedForImport.performClick();
}
});
final TextView selectMessage = (TextView) convertView.findViewById(R.id.row_info_text);
isSelectedForImport.setChecked(item.isSelected());
if (!isSelectedForImport.isChecked()) {
convertView.setClickable(true);
viewHolder.titleView.setTextColor(Color.parseColor("#8A000000"));
} else {
convertView.setClickable(false); viewHolder.titleView.setTextColor(Color.parseColor("#DE000000"));
}
isSelectedForImport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((ImportActivity) context).getNewApplication().hasChildren(item.getSheetId(), item.getTableId()) && !isSelectedForImport.isChecked()) {
isSelectedForImport.setChecked(true);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AlertDialog dialog;
builder.setMessage(context.getString(R.string.importapplication_appscreen_label_lookupwarningmessage));
builder.setPositiveButton(context.getString(R.string.ui_label_continue), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < item.getColumnType().size(); i++) {
removeLookups(item.getSheetId(), item.getTableId(), i);
}
isSelectedForImport.setChecked(false);
item.setSelected(false);
dialog.dismiss();
}
});
builder.setNegativeButton(context.getString(R.string.ui_label_dontcontinue), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.show();
notifyDataSetChanged();
} else if (!isSelectedForImport.isChecked()) {
item.setSelected(isSelectedForImport.isChecked());
notifyDataSetChanged();
} else {
if (isSelectedForImport.isChecked()) {
for (int i = 0; i < item.getColumnType().size(); i++) {
if (item.getSelectedColumns().get(i) && item.getColumnType().get(i).equals("SINGLE_LOOKUP")) {
enableParents(item.getSheetId(), item.getTableId(), i);
}
}
notifyDataSetChanged();
}
item.setSelected(isSelectedForImport.isChecked());
}
mCallback.onTableSelectionToggle();
}
});
if (((ImportActivity) context).getNewApplication().hasChildren(item.getSheetId(), item.getTableId())) {
Set<String> children = ((ImportActivity) context).getNewApplication().getChildTableNames(item.getSheetId(), item.getTableId());
Iterator<String> childIterator = children.iterator();
String childrenString = childIterator.next();
while (childIterator.hasNext()) {
childrenString += ", ";
childrenString += childIterator.next();
}
selectMessage.setText(context.getString(R.string.label_importlookupchildrentext) +" "+ childrenString);
selectMessage.setVisibility(View.VISIBLE);
} else {
selectMessage.setVisibility(View.GONE);
}
return convertView;
}
This does not occur all the time, and it doesn't occur for small lists. I have no other common problems described in other posts here (as long as this glitch doesn't occur, all views work correctly, and are in proper order).
XML Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sheet_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/sheet_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:textColor="#DE000000"
android:textSize="16sp"
tools:text="Sheet Name" />
<TextView
android:id="#+id/rows_count_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#8A000000"
android:textSize="14sp"
tools:text="Rows : 3" />
<TextView
android:id="#+id/row_info_text"
android:textSize="12sp"
android:layout_width="match_parent"
android:ellipsize="end"
android:maxLines="1"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:visibility="gone"
tools:visibility="gone"
tools:text="This form is looked up by "/>
</LinearLayout>
<RelativeLayout
android:id="#+id/import_table_list_toggle_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="19dp"
android:paddingRight="19dp"
android:layout_alignParentRight="true">
<CheckBox
android:id="#+id/import_table_list_toggle_button"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingBottom="24dp"
android:paddingTop="24dp" />
</RelativeLayout>
As I mention problem is RelativeLayout layout.
try this...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sheet_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:weightSum="10">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/sheet_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:textColor="#DE000000"
android:textSize="16sp"
tools:text="Sheet Name"/>
<TextView
android:id="#+id/rows_count_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#8A000000"
android:textSize="14sp"
tools:text="Rows : 3"/>
<TextView
android:id="#+id/row_info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:ellipsize="end"
android:maxLines="1"
android:textSize="12sp"
android:visibility="gone"
tools:text="This form is looked up by "
tools:visibility="gone"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/import_table_list_toggle_layout"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingLeft="19dp"
android:paddingRight="19dp">
<CheckBox
android:id="#+id/import_table_list_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:paddingBottom="24dp"
android:paddingTop="24dp"/>
</RelativeLayout>

setOnClickListener on a TextView inside a Cardview

I'm having some trouble implementing a setOnClickListener on a TextView inside a Cardview.I use this Cardview to populate a recicleview.
I have tried setting up the listener in the onBindViewHolder,but i can't see the log.
Single Item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:clickable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/fotoUser"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:background="#f9fbff"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:minHeight="300dp" />
<LinearLayout
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="#+id/fotoUser"
android:weightSum="1"
android:background="#color/colorPrimary"
android:gravity="center_horizontal">
<ImageView
android:src="#drawable/ic_diaf"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="#+id/imageView2"
android:layout_weight="0.03"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="1.0"
android:id="#+id/txtDiaf"
android:paddingTop="0dp"
android:paddingRight="5dp"
android:textColor="#ffffff"
android:layout_gravity="center" />
<ImageView
android:src="#drawable/ic_tempi"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="#+id/imageView3"
android:layout_weight="0.03"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="1sec"
android:id="#+id/txtTempo"
android:paddingTop="0dp"
android:paddingRight="5dp"
android:textColor="#ffffff"
android:layout_gravity="center" />
<ImageView
android:src="#drawable/ic_iso"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="#+id/imageView4"
android:layout_weight="0.03"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="100"
android:id="#+id/txtIso"
android:paddingTop="0dp"
android:paddingRight="5dp"
android:textColor="#ffffff"
android:layout_gravity="center" />
<ImageView
android:src="#drawable/ic_fl"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="#+id/imageView5"
android:layout_weight="0.03"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="100mm"
android:id="#+id/txtFl"
android:paddingTop="0dp"
android:textColor="#ffffff"
android:layout_gravity="center" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USERNAME"
android:id="#+id/userName"
android:clickable="true"
android:textStyle="bold|italic"
android:paddingTop="4dp"
android:paddingLeft="2dp"
android:layout_below="#+id/linear"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DESCRIZIONE"
android:id="#+id/descFoto"
android:paddingTop="4dp"
android:paddingLeft="2dp"
android:background="#color/colorPrimary"
android:paddingBottom="3dp"
android:layout_below="#+id/userName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#color/abc_primary_text_material_dark" />
<ImageView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:src="#drawable/ic_like"
android:layout_toLeftOf="#+id/likes"
android:layout_alignTop="#+id/descFoto"
android:layout_alignBottom="#+id/descFoto" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NL"
android:id="#+id/likes"
android:paddingTop="4dp"
android:background="#color/colorPrimary"
android:paddingBottom="3dp"
android:layout_below="#+id/userName"
android:textColor="#color/abc_primary_text_material_dark"
android:layout_above="#+id/spazio"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/spazio"
android:background="#color/colorPrimaryDark"
android:layout_below="#+id/descFoto"
android:layout_alignParentLeft="true"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="70dp"
android:id="#+id/btnLIKE"
android:clickable="true"
android:src="#drawable/ic_nolike"
android:layout_gravity="bottom"
android:adjustViewBounds="true"
android:paddingBottom="3dp"
android:layout_weight="0.06"
android:layout_above="#+id/linear"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
List->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="5dp"
android:background="#fffffc">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listaFoto"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:dividerHeight="0dp"
android:divider="#fff9fa" /></RelativeLayout>
Bind->
public void onBindViewHolder(final SeguitiFragment_FotoADP.FotoViewHolder holder,final int position) {
SeguitiFragment_Foto fotoS = foto.get(position);
//.....
holder.nomeUser.setText(fotoS.getUser());
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
//.........
}
Adapter->
public class SeguitiFragment_FotoADP extends RecyclerView.Adapter<SeguitiFragment_FotoADP.FotoViewHolder>{
private List<SeguitiFragment_Foto> foto;
private Context c;
private String NomeFile;
public SeguitiFragment_FotoADP(List<SeguitiFragment_Foto> foto,Context c) {
this.foto = foto;
this.c=c;
}
#Override
public SeguitiFragment_FotoADP.FotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_subfoto, parent, false);
return new FotoViewHolder(itemView);
}
#Override
public void onBindViewHolder(final SeguitiFragment_FotoADP.FotoViewHolder holder,final int position) {
SeguitiFragment_Foto fotoS = foto.get(position);
if(fotoS.getDiaframma()==-1){
holder.diaf.setText("--");
}else{
holder.diaf.setText(fotoS.getDiaframma()+"");
}
if(fotoS.getIso()==-1){
holder.iso.setText("--");
}else{
holder.iso.setText(fotoS.getIso()+"");
}
holder.nomeUser.setText(fotoS.getUser());
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
holder.desc.setText(fotoS.getDescrizione());
holder.tempo.setText(fotoS.getTempi()+ "sec");
holder.fl.setText(fotoS.getMillimetri()+"mm");
NomeFile=fotoS.getIdFoto();
File file = new File (c.getFilesDir(), NomeFile+".jpg");
if (!file.exists ()) {
downloadFTP ftp = new downloadFTP(fotoS.getIdFoto(), holder);
ftp.execute();
}else{
settaImmagine(holder);
}
}
#Override
public int getItemCount() {
return foto.size();
}
public static class FotoViewHolder extends RecyclerView.ViewHolder {
public ImageView immagine;
public TextView nomeUser;
public TextView desc;
public TextView diaf;
public TextView tempo;
public TextView iso;
public TextView fl;
public ImageView like;
public FotoViewHolder(View convertView) {
super(convertView);
nomeUser=(TextView)convertView.findViewById(R.id.userName);
desc=(TextView)convertView.findViewById(R.id.descFoto);
immagine=(ImageView)convertView.findViewById(R.id.fotoUser);
diaf=(TextView)convertView.findViewById(R.id.txtDiaf);
iso=(TextView)convertView.findViewById(R.id.txtIso);
fl=(TextView)convertView.findViewById(R.id.txtFl);
tempo=(TextView)convertView.findViewById(R.id.txtTempo);
like=(ImageView) convertView.findViewById(R.id.btnLIKE);
}
public void settaImmagine(FotoViewHolder v){
File file = new File(c.getFilesDir(), NomeFile + ".jpg");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
v.immagine.setImageBitmap(bitmap);
}
}
Thank You!
Try to set onClickListener after setText on your onBindViewHolder.
Refer this.
holder.mTextView.setText(mValues.get(position));
holder.mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Click", "Effettuato");
}
});
As per your code change this.
holder.nomeUser.setText(fotoS.getUser());
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
EDIT 1:
In your FotoViewHolder overRide this before settaImmagine function.
#Override
public String toString() {
return super.toString() + " '" + nomeUser.getText();
}
Use
in xml
<TextView
<-- other attributes -->
android:clickable="true"
/>
in Adapter
nomeUser= (TextView)view.findViewById(R.id.**id**);
and finally
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
Inside your
class FotoViewHolder
nomUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
if you want to identify different nomUser ,you can get the adapter position by using this code
int pos = getAdapterPosition();
This is your TextView in your layout :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USERNAME"
android:id="#+id/userName"
android:textStyle="bold|italic"
android:paddingTop="4dp"
android:paddingLeft="2dp"
android:layout_below="#+id/linear"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
You don't have android:clickable="true" . Add android:clickable="true" to your required TextView
and in your onBindViewHolder add :
holder.nomeUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Click","clicked");
}
});
First add below property in you parent(Means parent Linear-layout of your singleItem) of Single Item layout,
android:descendantFocusability="blocksDescendants"
and then try as below,
public void onBindViewHolder(final SeguitiFragment_FotoADP.FotoViewHolder holder,final int position)
{
SeguitiFragment_Foto fotoS = foto.get(position);
bind(position,fotoS);
}
now in your FotoViewHolder class make method as below,
public void bind(int pos, SeguitiFragment_Foto fotoS) {
nomeUser.setText(fotoS.getUser());
nomeUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Click","Effettuato");
}
});
}

CheckBox in android gridview not working on first click

am doing an android application, in that am loading check boxes in grid view from JSON web services . Up to this it is working perfectly. when i click the check box for the first time it is not triggering the event. but the second time it is working properly. Please help me if anybody knows
My java Code:
#SuppressWarnings("deprecation")
public void showFolderPopUPforEdit() {
int count = 0;
String folderID = null;
String selectedfolderID = null;
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
editpopupView = layoutInflater.inflate(R.layout.edit_folders, null);
editpopupWindow = new PopupWindow(editpopupView,
LayoutParams.MATCH_PARENT, 1060);
editpopupWindow.showAtLocation(editpopupView, Gravity.BOTTOM, 0, 0);
try {
leadna_leadsFolderjsonaray = leadna_leadFoldersjson
.getJSONArray("folderList");
for (int i = 0; i < leadna_leadsFolderjsonaray.length(); i++) {
cb = new CheckBox(this);
cb.setChecked(false);
cb.setFocusable(false);
cb.setFocusableInTouchMode(false);
cb.setBackground(getResources()
.getDrawable(R.drawable.uncheck1));
cb.setButtonDrawable(android.R.color.transparent);
JSONObject c = leadna_leadsFolderjsonaray.getJSONObject(i);
folderID = c.getString("category_id");
for (int j = 0; j < leadna_leadsSelectedFolderjsonaray.length(); j++) {
JSONObject arr2 = leadna_leadsSelectedFolderjsonaray
.getJSONObject(j);
selectedfolderID = arr2.getString("attach_category");
if (folderID.equalsIgnoreCase(selectedfolderID)) {
cb.setChecked(true);
cb.setBackground(getResources().getDrawable(
R.drawable.check_btn));
cb.setFocusable(false);
cb.setFocusableInTouchMode(false);
cb.setButtonDrawable(android.R.color.transparent);
break;
}
}
cb.setText(c.getString("category_name"));
cb.setTextColor(Color.BLACK);
cb.setTextSize((float) 14);
cb.setTag(c.getString("category_id"));
cb.setLayoutParams(new GridView.LayoutParams(346, 90));
cb.setPadding(54, 0, 0, 0);
cb.setOnClickListener(this);
cb.setId(i);
cb.requestFocus();
meditButtons.add(cb);
}
GridView gridView = (GridView) editpopupView
.findViewById(R.id.gridEditView1);
gridView.setAdapter(new CustomAdapter(this, meditButtons));
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Button btnSaveFolders = (Button) editpopupView
.findViewById(R.id.btnAddActivity);
btnSaveFolders.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Button btnDismiss = (Button) editpopupView.findViewById(R.id.button6);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editpopupWindow.dismiss();
}
});
}
#Override
public void onClick(View v) {
CheckBox selection = (CheckBox)v;
//selection.setCompoundDrawablesWithIntrinsicBounds(R.drawable.clickfolders,0, 0, 0);
selection.requestFocus();
if (selection.isChecked() == true) {
selection.setBackground(getResources().getDrawable(
R.drawable.uncheck1));
//selection.setButtonDrawable(android.R.color.transparent);
} else {
selection.setBackground(getResources().getDrawable(
R.drawable.check_btn));
//selection.setButtonDrawable(android.R.color.transparent);
}
//Toast.makeText(getBaseContext(), selection.getTag()+ " was pressed!", Toast.LENGTH_SHORT).show();
}
My Layout XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_black_transparent_90"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginTop="18dp"
android:text="#string/FOLDERS"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/darker_gray" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView1"
android:layout_marginRight="39dp"
android:visibility="gone" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_red_edit" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/imageView1"
android:text="EDIT"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/darker_gray"
android:textSize="16dp" />
</RelativeLayout>
<GridView
android:id="#+id/gridEditView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/ddd"
android:layout_below="#+id/textView1"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="#android:color/transparent"
android:horizontalSpacing="1dp"
android:numColumns="2"
android:verticalSpacing="1dp" >
</GridView>
<RelativeLayout
android:id="#+id/ddd"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/button6"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:background="#drawable/ic_red_cancel_47"
android:gravity="center"
android:visibility="visible" />
<Button
android:id="#+id/btnAddActivity"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:background="#drawable/ic_red_ok_47" />
</RelativeLayout>
</RelativeLayout>
Try to use
checkbox.setOnCheckedChangeListener( new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Check box "+arg0.getText().toString()+" is "+String.valueOf(arg1) , Toast.LENGTH_LONG).show();
}
} );
Instead of clickListener..

Categories

Resources