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..
Related
I have an FragmentActivity which contain WebView, Edittext1, Edittext2, Checkboxes and button. When I click the button in fragment it will move to Results_activity. And if I click button from Results_activity it move back to FragmentActivity.
But my edittext values getting cleared while moving back.
FragmentActivity xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_light"
android:orientation="vertical">
<LinearLayout
android:id="#+id/questionContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<WebView
android:id="#+id/questionWV"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="#+id/ansA_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp">
<EditText
android:id="#+id/ansA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:freezesText="true"
android:hint="Enter answer here..."
android:minWidth="50dp"
android:minHeight="50dp"
android:scrollbars="vertical"
android:textAppearance="#android:style/TextAppearance.Medium" />
</LinearLayout>
<LinearLayout
android:id="#+id/ansB_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp">
<EditText
android:id="#+id/ansB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:freezesText="true"
android:hint="Enter answer here..."
android:minWidth="50dp"
android:minHeight="50dp"
android:scrollbars="vertical"
android:textAppearance="#android:style/TextAppearance.Medium" />
</LinearLayout>
<LinearLayout
android:id="#+id/answers_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp">
<RelativeLayout
android:id="#+id/answer_a_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_weight="1">
<CheckBox
android:id="#+id/answerA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:hint="0"
android:maxLines="100"
android:minWidth="50dp"
android:minHeight="50dp"
android:scrollbars="vertical"
android:text="Varianta A"
android:textAppearance="#android:style/TextAppearance.Medium" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/answer_b_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<CheckBox
android:id="#+id/answerB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="false"
android:layout_marginStart="15dp"
android:hint="1"
android:maxLines="100"
android:minWidth="50dp"
android:minHeight="50dp"
android:scrollbars="vertical"
android:text="Varianta B"
android:textAppearance="#android:style/TextAppearance.Medium" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/answer_c_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<CheckBox
android:id="#+id/answerC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:hint="2"
android:maxLines="100"
android:minWidth="50dp"
android:minHeight="50dp"
android:scrollbars="vertical"
android:text="Varianta C"
android:textAppearance="#android:style/TextAppearance.Medium" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/answer_d_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<CheckBox
android:id="#+id/answerD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:hint="3"
android:maxLines="100"
android:minWidth="50dp"
android:minHeight="50dp"
android:scrollbars="vertical"
android:text="Varianta D"
android:textAppearance="#android:style/TextAppearance.Medium" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center"
android:orientation="horizontal"
android:paddingBottom="30dp">
<Button
android:id="#+id/btnFinishTest"
style="#android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/testbutton"
android:onClick="FinishTest"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Finish Test" />
<Button
android:id="#+id/btnAnswer"
style="#android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/testbutton"
android:onClick="SubmitAnswer"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Submit Answer" />
</LinearLayout>
</LinearLayout>
Fragment Code
my edittexts are et1 and et2
public class QuestionFragment2018ii extends Fragment {
LinearLayout answersContainer;
int currentPageNr;
private String input1;
#Override
public View onCreateView(LayoutInflater lInflater, ViewGroup container,Bundle saveInstanceState){
currentPageNr = getArguments().getInt("position");
//initialize some variables
final Question2018ii currentQuestion2018ii = MyServerData2018ii.getInstance().getQuestion(currentPageNr);
View rootView = lInflater.inflate(R.layout.quiz_activity_fragment,container,false);
//initialize show answer option
final String ans = currentQuestion2018ii.getExplanationText();
final EditText et1 = rootView.findViewById(R.id.ansA);
final EditText et2 = rootView.findViewById(R.id.ansB);
if(MyServerData2018ii.getInstance().getTestState().equals("finished") ){
et1.setText("Your Response: " + et.getText());
et2.setText("Correct Answer: " + ans);
}
//initialize Explanation option
//initialize answers
answersContainer = (LinearLayout)rootView.findViewById(R.id.answers_container);
String[] answers = currentQuestion2018ii.getAllAnswersText();
for (int i = 0; i < answersContainer.getChildCount(); i++) {
RelativeLayout checkboxContainer = (RelativeLayout)answersContainer.getChildAt(i);
CheckBox cb = (CheckBox)checkboxContainer.getChildAt(0);
cb.setMovementMethod(new ScrollingMovementMethod());
cb.setText(answers[i]);
cb.setChecked(currentQuestion2018ii.isChecked(i));
if(MyServerData2018ii.getInstance().getTestState().equals("finished") ){
cb.setEnabled(false);
questionWebView.loadUrl("file:///android_asset/" + currentQuestion2018ii.getSolutionText());
//check if answer is right or wrong
if(currentQuestion2018ii.isCorrectAnswer(i)){
cb.setTextColor(Color.parseColor("#4DAD47"));
cb.setTypeface(null, Typeface.BOLD);
} else {
cb.setTextColor(Color.parseColor("#CE0B0B"));
}
}else{
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = ((CheckBox) v);
int number = Integer.parseInt(cb.getHint().toString());
currentQuestion2018ii.setChecked(number, cb.isChecked());
}
});
}
}
return rootView;
}
}
ResultsActivity xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingBottom="10dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="30dp"
android:text="#string/results"
android:textColor="#4DAD47"
android:textSize="20sp"
android:textStyle="bold|italic" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="30dp" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:paddingBottom="20dp"
android:text="#string/correct_answers" />
</LinearLayout>
<TextView
android:id="#+id/myTotalAnswers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:paddingRight="5dp"
android:text="50/100" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainContainer"></LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<Button
android:id="#+id/check_results"
style="#android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/testbutton"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/check_results" />
<Button
android:id="#+id/btnMainMenu"
style="#android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#drawable/testbutton"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/main_menu" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
Quiz activity
public class QuizActivity2018ii extends AppCompatActivity {
Spinner spinCategory;
EditText questionNr;
ArrayList<String> allCategories;
int totalQuestions;
AlertDialog dialog;
ViewPager pager;
QuestionPagerAdapter2018ii pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz_activity_view_pager);
allCategories = new ArrayList<>(MyServerData2018ii.getInstance().getCategoryList());
totalQuestions = MyServerData2018ii.getInstance().getTotalQuestions();
//initialize category spinner
ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_dropdown_item,allCategories);
spinCategory = (Spinner) findViewById(R.id.category);
spinCategory.setAdapter(categoryAdapter);
spinCategory.setSelection(0);
//set Category spinner callback
spinCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String currentQuestionRealCategory = MyServerData2018ii.getInstance().getQuestionCategory(pager.getCurrentItem());
String selectedCategory = spinCategory.getItemAtPosition(position).toString();
if (!selectedCategory.equals(currentQuestionRealCategory)) {
int firstQuestionNumberFromCategory = MyServerData2018ii.getInstance().getFirstQuestionNumberFromCategory(selectedCategory);
ViewPager pager = (ViewPager) findViewById(R.id.qPager);
pager.setCurrentItem(firstQuestionNumberFromCategory, false);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//initialize number
questionNr = (EditText) findViewById(R.id.dialog_question_number);
questionNr.clearFocus();
//set number callbacks
questionNr.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_ENTER)) {
v.clearFocus();
CharSequence s = questionNr.getText();
if (!s.toString().isEmpty()) {
Integer questionNumber = Integer.parseInt(s.toString());
//avoids out of range indexes
if (questionNumber > totalQuestions + 1) {
questionNumber = totalQuestions;
}
if (questionNumber < 0) {
questionNumber = 1;
}
//create looping effect
if (questionNumber == totalQuestions + 1) {
questionNumber = 1;
((EditText)v).setText("1");
}
if (questionNumber == 0) {
questionNumber = totalQuestions;
((EditText)v).setText(String.valueOf(totalQuestions));
}
ViewPager pager = (ViewPager) findViewById(R.id.qPager);
pager.setCurrentItem(questionNumber, false);
questionNr.clearFocus();
} else {
questionNr.setText("1");
}
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return false;
}
});
//initialize pager
pager = (ViewPager)findViewById(R.id.qPager);
pagerAdapter = new QuestionPagerAdapter2018ii(getSupportFragmentManager());
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(1, false);
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override
public void onPageSelected(int position) {
Integer currentQuestion = pager.getCurrentItem();
//change spinner
String currentCategory = MyServerData2018ii.getInstance().getQuestionCategory(currentQuestion);
int categoryPosition = MyServerData2018ii.getInstance().getCategoryList().indexOf(currentCategory);
spinCategory.setSelection(categoryPosition);
//change numberPicker
if(currentQuestion <= 0){currentQuestion = totalQuestions;}
if(currentQuestion > totalQuestions){currentQuestion = 1;}
questionNr.setText(currentQuestion.toString());
questionNr.clearFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(questionNr.getWindowToken(), 0);
}
#Override
public void onPageScrollStateChanged(int state) {
int totalQuestions = MyServerData2018ii.getInstance().getTotalQuestions();
if (state == ViewPager.SCROLL_STATE_IDLE) {
if (pager.getCurrentItem() == totalQuestions + 1) {
pager.setCurrentItem(1, false);
}
if (pager.getCurrentItem() == 0) {
pager.setCurrentItem(totalQuestions, false); // false will prevent sliding animation of view pager
}
}
}
});
}
public void FinishTest(View v){
//check if there are unanswered questions
if(MyServerData2018ii.getInstance().getTestState().equals("inProgress")){
ArrayList<String> UnansweredQuestions = new ArrayList<>();
LinkedHashMap<String,Object> allQuestions = MyServerData2018ii.getInstance().getAllQuestions();
for(Map.Entry category: allQuestions.entrySet()){
Question2018ii[] question2018iis = (Question2018ii[])category.getValue();
for(int i = 0; i < question2018iis.length; i++){
Boolean[] userAnswers = question2018iis[i].getUserAnswers();
if(!Arrays.asList(userAnswers).contains(true)){
String checkedCategory = (String)category.getKey();
Integer questionNumberInList = MyServerData2018ii.getInstance().getQuestionListNumber(checkedCategory,i);
UnansweredQuestions.add(String.valueOf(questionNumberInList));
}
}
}
if(UnansweredQuestions.size() > 0){
dialog = new AlertDialog.Builder(this)
.create();
LayoutInflater infl = LayoutInflater.from(this);
dialog.setView(infl.inflate(R.layout.dialog_message,null));
dialog.show();
TextView message = (TextView)dialog.findViewById(R.id.message);
String unfinished = getResources().getString(R.string.unfinished_text);
String questions = TextUtils.join(",",UnansweredQuestions);
message.setText(unfinished + "\n" + questions + ".");
dialog.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
showResults();
}
});
}else{showResults();}
}else{
showResults();
}
}
public void showResults(){
int animationDuration;
if(MyServerData2018ii.getInstance().getTestState().equals("finished")){
animationDuration = 10;
}else{
animationDuration = 2000;
}
View mainView = LayoutInflater.from(this).inflate(R.layout.quiz_activity_show_results,null);
LinearLayout mainContainer = (LinearLayout)mainView.findViewById(R.id.mainContainer);
mainView.findViewById(R.id.btnMainMenu).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//finish the test and go to main menu
Intent Main = new Intent(getApplicationContext(),MainActivity.class);
finish();
startActivity(Main);
MyServerData2018ii.getInstance().setTestState("notStarted");
MyServerData2018ii.getInstance().clearAnswers();
Toast.makeText(getBaseContext(),R.string.text_ended,Toast.LENGTH_LONG).show();
}
});
mainView.findViewById(R.id.check_results).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
Question2018ii[] currentCategory;
int totalCategoryQuestions;
int correctCategoryQuestions;
int totalCorrectQuestions = 0;
//checking and adding each category
for(String category: allCategories){
View categoryContainer = LayoutInflater.from(this).inflate(R.layout.quiz_activity_category_results,null);
TextView categoryName = (TextView)categoryContainer.findViewById(R.id.categoryName);
//set name
categoryName.setText(category);
currentCategory = MyServerData2018ii.getInstance().getCategory(category);
totalCategoryQuestions = currentCategory.length;
//check answers
correctCategoryQuestions = 0;
for(int i=0; i < currentCategory.length;i++){
Boolean isCorrect = Arrays.equals(currentCategory[i].getAllCorrectAnswers(),currentCategory[i].getUserAnswers());
if(isCorrect){ correctCategoryQuestions++;}
}
totalCorrectQuestions += correctCategoryQuestions;
//set results
String result = String.valueOf(correctCategoryQuestions) + "/" + String.valueOf(totalCategoryQuestions);
final ProgressBar progress = (ProgressBar)categoryContainer.findViewById(R.id.progressBar);
progress.setMax(totalCategoryQuestions*100);
final TextView myResult = (TextView)categoryContainer.findViewById(R.id.categoryResult);
final String myResultText = "/" + String.valueOf(totalCategoryQuestions);
ValueAnimator val = new ValueAnimator();
val.setObjectValues(0, correctCategoryQuestions*100);
val.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
myResult.setText(String.valueOf((Integer)animation.getAnimatedValue()/100) + myResultText);
progress.setProgress( ((Integer) animation.getAnimatedValue()));
}
});
val.setDuration(animationDuration);
val.start();
mainContainer.addView(categoryContainer);
}
//animate results
final TextView tvTotalResult =(TextView)mainView.findViewById(R.id.myTotalAnswers);
final String totalResultS = "/" + String.valueOf(totalQuestions);
ValueAnimator totalResultsAnimator = new ValueAnimator();
totalResultsAnimator.setObjectValues(0, totalCorrectQuestions);
totalResultsAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
tvTotalResult.setText(String.valueOf(animation.getAnimatedValue()) + totalResultS);
}
});
totalResultsAnimator.setDuration(animationDuration);
totalResultsAnimator.start();
MyServerData2018ii.getInstance().setTestState("finished");
setContentView(mainView);
}
}
this is my question yearwise activity(from this I launched the fragment activity)
public class QuestionYearwise extends AppCompatActivity{
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.question_yearwise);
Button btn2018ii = (Button)findViewById(R.id.btn2018ii);
btn2018ii.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Quiz2018ii = new Intent(getApplicationContext(),QuizActivity2018ii.class);
MyServerData2018ii.getInstance().setTestState("inProgress");
startActivity(Quiz2018ii);
}
});
}
}
Replace the method like this,
mainView.findViewById(R.id.check_results).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
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>
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;
problem with settingText for each and individual edittext in listview.Edittext focus missing..Check my image above which is haveing custom keypad and i need to settext when keypad buttons is pressed.Below is my code. i tried but the focus is going for some other edittext.Please help me out.
private LayoutInflater mInflater;
public ArrayList myItems = new ArrayList();
List < HashMap < String, String >> painItems;
public MyAdapet()
{
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
painItems = new ArrayList < HashMap < String, String >> ();
HashMap < String, String > map = new HashMap < String, String > ();
for(int i=1;i<10;i++)
{
map.put("row_2", "Location Content");
map.put("row_3","Content");
map.put("row_4", "50");
map.put("row_5", "");
painItems.add(map);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return painItems.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_xml, null);
} else {
}
returnText = (EditText) convertView
.findViewById(R.id.returnedit);
returnText.setInputType(InputType.TYPE_NULL);
//Fill EditText with the value you have in <span id="r774jm31_1" class="r774jm31">data source</span>
//we need to update adapter once we finish with editing
returnText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
listview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
returnText.requestFocus();
}
});
return convertView;
}
My Listxml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:descendantFocusability="afterDescendants"
android:focusable="false"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
My custom List:
<?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:background="#cfcfcf"
android:gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/seq_bg"
android:layout_centerInParent="true"
/>
<TextView
android:id="#+id/sqlno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="left"
android:text="1"
android:textColor="#000000"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="#+id/lcontent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="Location"
android:textColor="#ffffff"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:textStyle="bold" />
<TextView
android:id="#+id/addcontent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="Add Content"
android:textColor="#ffffff"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:textStyle="bold" />
<TextView
android:id="#+id/drop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="50"
android:textColor="#ffffff"
android:gravity="center"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:layout_weight="1"
/>
<EditText
android:id="#+id/returnedit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="67"
android:textColor="#000000"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:layout_weight="1"
/>
<Button
android:id="#+id/viewnotesButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="view Notes"
android:textColor="#000000"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Small"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
My clicklisteners for custom kepad is like this:
button0.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
returnText.setText(returnText.getText().toString()+"0");
MyAdapet.notifyDataSetChanged();
}
});
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
returnText.setText(returnText.getText().toString()+"1");
MyAdapet.notifyDataSetChanged();
}
});
try this code -
returnText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
listview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
returnText.requestFocus();
notifyDataSetChanged(); // for update adapter
}
});
Hope this code helps you !!!
if its not working please let me know i will try to help more.
ListView in Graphical Layout is fit on appropriate height but when i imported into android device, ListView is reduced in a small field. i want ListView are the same in Graphical Layout in xml file and in real device screen
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10sp"
android:id="#+id/header"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/id"
android:layout_weight="1.25"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/amount"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/alert"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/detail"
android:layout_weight="1"
android:gravity="center"/>
</LinearLayout>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10sp"></ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.5">
<Button
android:id="#+id/addQueue"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:text="#string/addQueue"/>
<Button
android:id="#+id/submit"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:text="#string/submit"/>
<Button
android:id="#+id/reset"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/addQueue"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="#string/resetQueue" />
</RelativeLayout>
I have tried to modify your layout. Try this may be it will be helpful for you.. just create a new xml layout file and check it is working for you or not.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/addQueue"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/submit"
android:text="addQueue" />
<Button
android:id="#+id/submit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="submit" />
<Button
android:id="#+id/reset"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="resetQueue" />
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.25"
android:gravity="center"
android:text="text1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text4" />
</LinearLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/addQueue"
android:layout_alignParentLeft="true"
android:layout_below="#+id/header" >
</ListView>
</RelativeLayout>
My custom adapter class is here. :
public class my_custom_adapter extends ArrayAdapter<String> {
private Context context = null;
String[] elements = null;
private ArrayList<Boolean> itemChecked = null;
public my_custom_adapter(Context context, int type, String[] elements2)
{
super(context, type, elements2);
this.elements = elements2;
this.context = context;
itemChecked = new ArrayList<Boolean>();
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("master_check_change")) {
boolean check_value = intent.getBooleanExtra("check_value", false);
set_checked(check_value);
notifyDataSetChanged();
}
}
};
context.registerReceiver(receiver, new IntentFilter("master_check_change"));
set_checked(false);
}
// AS EVERY TIME LISTVIEW INFLATE YOUR VIEWS WHEN YOU MOVE THEM SO YOU NEED TO SAVE ALL OF YOUR CHECKBOX STATES IN SOME ARRAYLIST OTHERWISE IT WILL SET ANY DEFAULT VALUE.
private void set_checked(boolean is_checked)
{
for (int i=0; i < elements.length; i++) {
itemChecked.add(i, is_checked);
}
}
//THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder
{
public TextView tv;
public CheckBox cb;
public ImageView iv;
}
#Override
public View getView (final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.inflated_layout, null, false);
holder = new ViewHolder();
holder.cb = (CheckBox) rowView.findViewById(R.id.checkBox1);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.iv = (ImageView) rowView.findViewById(R.id.imageView1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
if (holder != null) {
holder.tv.setText(elements[position]);
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
itemChecked.set(position, isChecked);
}
});
if(position < itemChecked.size()) {
holder.cb.setChecked(itemChecked.get(position));
}
}
return rowView;
}
}
My activity class is here where i am create custom adapter object,
public class Test_stflowActivity extends Activity {
public CheckBox master_cb = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp_layout);
String[] elements = new String[10];
for (int i = 0; i < 10; i++) {
elements[i] = "elements " + i;
}
CheckBox master_cb = new CheckBox(getApplicationContext());
master_cb.setText("Check All");
//HERE IS THE LIST VIEW WHICH I HAVE CREATED IN MY XML FILE.
ListView lv = (ListView) findViewById(R.id.listView1);
//HERE I AM CREATING CUSTOM ADAPTER OBJECT.
my_custom_adapter adapter = new my_custom_adapter(this, android.R.layout.simple_list_item_1, elements);
lv.addHeaderView(master_cb);
lv.setAdapter(adapter);
master_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Intent my_intent = new Intent("master_check_change");
my_intent.putExtra("check_value", isChecked);
sendBroadcast(my_intent);
}
});
}
}