How to clean some of EditText? - android

My app has some instances of EditText. I have to clean all the EditTexts when I
click the button. I need to write a condition cleaning all the EditTexts
in which some of EditTexts are empty. How I will do it?
public class Fragment1 extends Fragment implements View.OnClickListener
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v=inflater.inflate(R.layout.fragment1,null);
seekBar1 = v.findViewById(R.id.fragment1SeekBar1);
seekBar2 = v.findViewById(R.id.fragment1SeekBar2);
seekBar3=v.findViewById(R.id.fragment1SeekBar3);
edTxt1=v.findViewById(R.id.fragment1EditText1);
edTxt2=v.findViewById(R.id.fragment1EditText2);
edTxt3=v.findViewById(R.id.fragment1EditText3);
fragment1button1=v.findViewById(R.id.fragment1Button1);
fragment1button1.setOnClickListener(this);}
#Override
public void onClick(View view){
int val1 = Integer.parseInt( edTxt1.getText().toString());
int val2=Integer.parseInt( edTxt2.getText().toString());
int val3=Integer.parseInt( edTxt3.getText().toString());
switch(view.getId()){
case R.id.fragment1Button1:
if(edTxt1.equals("")){
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
}else if(edTxt2.equals("")){
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();}
else if (edTxt3.equals("")){
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();}
break;}
}
}

If you want to set 'EditText` is empty just use
if(edTxt1.getText().toString().equals("")) {
edTxt1.setText("");
edTxt2.setText("");
edTxt3.setText("");
} else if(edTxt2.getText().toString().equals("")) {
edTxt1.setText("");
edTxt2.setText("");
edTxt3.setText("");
} else if (edTxt3.getText().toString().equals("")) {
edTxt1.setText("");
edTxt2.setText("");
edTxt3.setText("");
}
break;
or if you want to remove edittext
if (edTxt1.getText().toString().equals("")) {
edTxt1.setVisibility(View.GONE);
edTxt2.setVisibility(View.GONE);
edTxt3.setVisibility(View.GONE);
} else if(edTxt2.getText().toString().equals("")) {
edTxt1.setVisibility(View.GONE);
edTxt2.setVisibility(View.GONE);
edTxt3.setVisibility(View.GONE);
} else if (edTxt3.getText().toString().equals("")) {
edTxt1.setVisibility(View.GONE);
edTxt2.setVisibility(View.GONE);
edTxt3.setVisibility(View.GONE);
}
break;

if(edTxt1.equals(""))
Your conditions are wrong. They should look like this:
if(edTxt1.getText().toString().equals(""))

If you want to set text of an edit text to null:
edTxt1.setText("");
Otherwise you can disappear it setting to invisible.
edTxt1.setVisibility(View.INVISIBLE);

Try this way
switch(view.getId())
{
case R.id.fragment1Button1:
if (TextUtils.isEmpty(edTxt1.getText().toString())) {
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
} else if (TextUtils.isEmpty(edTxt2.getText().toString())) {
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
} else if (TextUtils.isEmpty(edTxt3.getText().toString())) {
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
}
break;
}

For every EditText do it like this
if (!edTx.getText().toString().equals("")) {
edTx.setText("");
}

Related

How to add text to TextView without replacing the old text?

As I mentioned in the title ,I want to add text to a Textview without replacing the previous text .
In my application I have a TextView and 7 buttons .On every button click I set the text of button to the TextView.
If the button is clicked on first time ,Setting the text to TextView ,and if the same button is clicked 2nd time I am removing that button's text from TextView.
Here What I want to do is for 7 buttons I want to set positions(uniqueness for sun to sat) in TextView and when the respective button is clicked that text is set to the TextView and if the button is clicked 2nd time that specific position of the text should remove .
Here text shouldn't replace the previous text that is important to have and if some button's are selected and again that are deselected means TextView should show the default text as "Never"
I tried to get source from SO but I can't find a clear solution for this .
If anyone helps me to come out from this ,that's much helpful for me .
coding
public class CreateAlarm extends Activity implements View.OnClickListener {
private Button mbtn_Sun, mbtn_Mon, mbtn_Tue, mbtn_Wed, mbtn_Thu, mbtn_Fri, mbtn_Sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_alarm);
mRepeat = (TextView) findViewById(R.id.mRepeat);
mbtn_Sun = (Button) findViewById(R.id.mbtn_Sun);
mbtn_Mon = (Button) findViewById(R.id.mbtn_Mon);
mbtn_Tue = (Button) findViewById(R.id.mbtn_Tue);
mbtn_Wed = (Button) findViewById(R.id.mbtn_Wed);
mbtn_Thu = (Button) findViewById(R.id.mbtn_Thu);
mbtn_Fri = (Button) findViewById(R.id.mbtn_Fri);
mbtn_Sat = (Button) findViewById(R.id.mbtn_Sat);
mbtn_Sun.setOnClickListener((View.OnClickListener) this);
mbtn_Mon.setOnClickListener((View.OnClickListener) this);
mbtn_Tue.setOnClickListener((View.OnClickListener) this);
mbtn_Wed.setOnClickListener((View.OnClickListener) this);
mbtn_Thu.setOnClickListener((View.OnClickListener) this);
mbtn_Fri.setOnClickListener((View.OnClickListener) this);
mbtn_Sat.setOnClickListener((View.OnClickListener) this);
int hours = mTimePicker.getCurrentHour();
mCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mRepeat.setText("");
} else
mRepeat.setText("Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mRepeat.setText("");
} else
mRepeat.setText("Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mRepeat.setText("");
} else
mRepeat.setText("Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mRepeat.setText("");
} else
mRepeat.setText("Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mRepeat.setText("");
} else
mRepeat.setText("Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mRepeat.setText("");
} else
mRepeat.setText("Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mRepeat.setText("");
} else
mRepeat.setText("Sat");
break;
default:
mRepeat.setText("Never");
}
}
}
Image :
By default the TextView text is "Never".
You can define a TreeMap as:
private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
as a field of your class and add/remove the days to/from the TreeMap when the corresponding button is clicked. So the implementation of onClick method will be:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mAlarmDays.remove(0);
} else
mAlarmDays.put(0, "Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mAlarmDays.remove(1);
} else
mAlarmDays.put(1, "Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mAlarmDays.remove(2);
} else
mAlarmDays.put(2, "Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mAlarmDays.remove(3);
} else
mAlarmDays.put(3, "Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mAlarmDays.remove(4);
} else
mAlarmDays.put(4, "Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mAlarmDays.remove(5);
} else
mAlarmDays.put(5, "Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mAlarmDays.remove(6);
} else
mAlarmDays.put(6, "Sat");
break;
}
StringBuilder repeatDays = new StringBuilder();
if (mAlarmDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (String day:mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
mRepeat.setText(repeatDays.toString());
}
You should set each button id first,add this to your xml for each specific button : android:id="sun" and ...
My suggestion is: use a single TextView can make your logic quite complex
Use a horizontal LinearLayout instead, you will have 7 TextView inside with predefine text and position. Just simply show/hide them according to which button is clicked and you don't have to deal with any complex string analize.

Change State of ToggleButton programmatically

I have placed a togglebutton with id toggleButton
and I am using the code below to check if it's on or off
public void toggleClick(View view){
boolean on = ((ToggleButton) view).isChecked();
if(on){
}else{
}
}
Everything works fine.. I am trying to change the state of the toggle button, I tried a lot of codes through internet but none changes the state of the toggle to off.
I tried this:
ToggleButton toggleButtons;
toggleButtons = (ToggleButton) findViewById(R.id.toggleButton); //In onCreate
toggleButtons.setChecked(false);
any ideas please?
Use setChecked(boolean checked) by example:
toggleButtons.setChecked(true)
I ended up using Toggle feature using ImageButton. The code below works fine.
*
private ImageButton ib;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//......
ib = (ImageButton) rootView.findViewById(R.id.imagefavouriteButton);
if (checkFavorite()) {
ib.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.fav_on));
} else {
ib.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.fav_off));
}
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkFavorite()) {
remFromFav();
} else {
addFavorites();
}
}
});
//.....
}
private boolean checkFavorite(){// code to check in db}
public void addFavorites() {
ib.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.fav_on));
Toast.makeText(getActivity(), "Added ...", Toast.LENGTH_SHORT).show();
}
public void remFromFav() {
ib.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.fav_off));
Toast.makeText(getActivity(), "Removed ...", Toast.LENGTH_SHORT).show();
}
Hope this helps you.

How to use horizontal view pager inside recycler view

I want to use viewpager inside recycler view and set data on pager from api.
For more details, i'm adding my code in question.
My Code is
public void onBindViewHolder(final worksheetAdapter.CustomViewHolder holder, final int position) {
final int a = position; rec_worksheet.setTag(position);
Set text on TextView
holder.question.setText(userlist.get(position).get("ques"));
holder.question.startAnimation(animFadein);
holder.tvans1.setText(userlist.get(position).get("ans1"));
holder.tvans1.startAnimation(animFadein);
holder.tvans2.setText(userlist.get(position).get("ans2"));
holder.tvans2.startAnimation(animFadein);
holder.tvans3.setText(userlist.get(position).get("ans3"));
holder.tvans3.startAnimation(animFadein);
holder.tvans4.setText(userlist.get(position).get("ans4"));
holder.tvans4.startAnimation(animFadein);
holder.submitbtn.setVisibility(View.GONE);
if(position == userlist.size() - 1){
holder.submitbtn.setVisibility(View.VISIBLE);
}
holder.queNo.setText("Question No "+String.valueOf(position+1));
holder.tvTotalQue.setText("Total Ques. "+String.valueOf(userlist.size()));
holder.tvSubName.setText(userlist.get(position).get("subject_name"));
holder.submitbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
submit();
}
});
Check if first answer is equals to correct answer
holder.llAns1.setTag(position);
holder.llAns1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.getTag(position);
holder.llAns1.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
allAns.add(userlist.get(position).get("ans1"));
correctans = userlist.get(position).get("correctans");
if(holder.tvans1.getText().toString().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans1"));
}else {
try {
wrongAns.add(userlist.get(position).get("ans1"));
CorrectAns.remove(position);
}catch (IndexOutOfBoundsException e){}
}
}
});
Check if secound answer is equals to correct answer
holder.llAns2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.llAns2.setTag(position);
holder.llAns2.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
correctans = userlist.get(position).get("correctans");
if(holder.tvans2.getText().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans2"));
}else {
try {
wrongAns.add(userlist.get(position).get("ans2"));
CorrectAns.remove(correctans);
}catch (IndexOutOfBoundsException e){}
}
}
});
Check if third answer is equals to correct answer
holder.llAns3.setTag(position);
holder.llAns3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.llAns3.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
correctans = userlist.get(position).get("correctans");
if(holder.tvans3.getText().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans3"));
}else {
try {
wrongAns.add(userlist.get(position).get("ans3"));
CorrectAns.remove(correctans);
}catch (IndexOutOfBoundsException e){}
}
}
});
Check if forth answer is equals to correct answer
holder.llAns4.setTag(position);
holder.llAns4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.llAns4.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
correctans = userlist.get(position).get("correctans");
if(holder.tvans4.getText().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans4"));
Log.d("corrAns1",String.valueOf(CorrectAns));
}else {
try {
wrongAns.add(userlist.get(position).get("ans1"));
CorrectAns.remove(correctans);
}catch (IndexOutOfBoundsException e){}
Log.d("corrAns2",String.valueOf(CorrectAns));
}
}
});
}
You cannot use a viewpager inside a recyclerview. If you want to use horizontal swiping inside a recyclerview then you will have to use a recyclerview inside a recyclerview. This child recyclerview will be have a horizontal layout.
This question precisely addresses that.
For the 2nd issue:
Add the following lines
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
below this line:
if(position == userlist.size() - 1){
holder.submitbtn.setVisibility(View.VISIBLE);
}
for 3rd issue:
//add this below your adapter class
private ArrayList selectedOptions;
in your onbindviewholder add this below holder.llAns4.setBackgroundResource(R.drawable.round_strock_green)
if(selectedOptions.get(position) != null){
switch (selectedOptions.get(position)){
case 1:
holder.llAns1.setBackgroundResource(R.drawable.que_correct_back);
break;
case 2:
holder.llAns2.setBackgroundResource(R.drawable.que_correct_back);
break;
case 3:
holder.llAns3.setBackgroundResource(R.drawable.que_correct_back);
break;
case 4:
holder.llAns4.setBackgroundResource(R.drawable.que_correct_back);
break;
}
}
I have answered 3 things now. Please ask new stackoverflow question for further issues.

How to Show toast when I switch Tab from BaseAdapter?

I want to Show a Tabbed based inside Dialog so for this I use This Library.
I am able to populate my Dialog and also Tab is switching from Tab_1 to Tab_2. But when I am clicking on its Cancel, Ok or Neutral Button, I am not getting Toast.
Please guide me how can I use Initialize Fragments and Toast on Button Click in my BaseAdapter.
My code is here
public class ContactListAdapter extends BaseAdapter implements ISimpleDialogListener, ISimpleDialogCancelListener,IFragmentListener {
slidingListItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
contactListItems = contactList.get(position);
String id_attende = contactListItems.getPhone();
String id_attende_name = contactListItems.getName();
TabDialogFragment.createBuilder(context, mFragmentManager)
.setTitle(id_attende_name)
.setSubTitle(id_attende)
.setTabButtonText(new CharSequence[]{"Calendar", "Summary"})
.setPositiveButtonText("Ok")
.setNegativeButtonText("Cancel")
.setNeutralButtonText("Neutral")
.setRequestCode(REQUEST_TABBED_DIALOG)
.show();
}
});
return convertView;
}
#Override
public void onCancelled(int requestCode) {
switch (requestCode) {
case REQUEST_TABBED_DIALOG:
Toast.makeText(context, "Dialog cancelled", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
#Override
public void onNegativeButtonClicked(int requestCode) {
if (requestCode == REQUEST_TABBED_DIALOG) {
Toast.makeText(context, "Negative button clicked", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNeutralButtonClicked(int requestCode) {
if (requestCode == REQUEST_TABBED_DIALOG) {
Toast.makeText(context, "Neutral button clicked", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onPositiveButtonClicked(int requestCode) {
if (requestCode == REQUEST_TABBED_DIALOG) {
Toast.makeText(context, "Positive button clicked", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFragmentViewCreated(Fragment fragment) {
int selectedTabPosition = fragment.getArguments().getInt(PageFragment.ARG_DAY_INDEX, 0);
View rootContainer = fragment.getView().findViewById(R.id.root_container);
Log.i(TAG, "Position: " + selectedTabPosition);
switch (selectedTabPosition) {
case 0:
selectedTabPositionZeroCase(rootContainer);
break;
case 1:
selectedTabPositionOneCase(rootContainer);
break;
default:
break;
}
}
private void selectedTabPositionZeroCase(View rootContainer) {
// add view in container for first tab
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View tabProductDetailLayout = li.inflate(R.layout.tab_one_layout, (ViewGroup) rootContainer);
TextView textView = (TextView) tabProductDetailLayout.findViewById(R.id.text_view);
textView.setText("hello: tab1");
}
private void selectedTabPositionOneCase(View rootContainer) {
// add view in container for second tab
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View tabProductDetailLayout2 = li.inflate(R.layout.tab_one_layout, (ViewGroup) rootContainer);
TextView textView1 = (TextView) tabProductDetailLayout2.findViewById(R.id.text_view);
textView1.setText("hello: tab2");
}
#Override
public void onFragmentAttached(Fragment fragment) {
mMyScheduleFragments.add(fragment);
}
#Override
public void onFragmentDetached(Fragment fragment) {
mMyScheduleFragments.remove(fragment);
}
}
Actually from the link, you have provided, They are passing the MainActivity.this where you are passing context and there is a problem. So as per my suggestion just implement these callbacks in your MainActivity.java class instead of Adapter class.

How can i change background color of my layout by clicking a Button

How can i change background color of my layout by clicking on a Button ?
This is my code :
Button color_change;
LinearLayout layout;
int value = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
color_change = (Button)findViewById(R.id.color_btn);
layout = (LinearLayout)findViewById(R.id.LL);
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (value == 1) {
layout.setBackgroundColor(Color.RED);
}
else if (value == 2) {
layout.setBackgroundColor(Color.BLUE);
}
else if (value == 3) {
layout.setBackgroundColor(Color.MAGENTA);
}
else if (value == 4) {
layout.setBackgroundColor(Color.DKGRAY);
value = 0;
}
value++;
}
});
But i want replace if else condition with other code , because this code is complex and i want short code.
So any one suggest me, how can i short my code ?
You can try this
put all color into a array and in onclick method get particular color from the array and set it like
int color[]=new int[]{Color.BLUE,Color.RED,Color.GRAY};
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (value <color.length) {
layout.setBackgroundColor(color[value]);
}
value++;
}
});
There is no magic. Use the switch instead of else ifs. For the added clarity, you might consider having constants or something as values to value, so that you don't have to deal with hard-coded integers.
You could use an array to store the data:
int[] colors = {Color.RED, Color.BLUE, Color.BLACK};
int index = value % colors.length;
layout.setBackgroundColor(colors[index]);
value++;
You will use like this...
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switch(value)
{
case 1:layout.setBackgroundColor(Color.RED);
break;
case 2:layout.setBackgroundColor(Color.BLUE);
break;
case 3: layout.setBackgroundColor(Color.MAGENTA);
break;
case 4:layout.setBackgroundColor(Color.DKGRAY);
value = 0;
break;
}
value++;
}
});
A list of colors you want to use
List<int> colorsList = new List<int>();
colorsList.add(Color.RED);
colorsList.add(Color.WHITE);
colorsList.add(Color.BLUE);
colorsList.add(Color.GREEN);
//here you can add other colors to list
Iterator<int> colorIterator = colorsList.iterator();
function to get next color
int getNextColor()
{
if(colorIterator.hasNext())
{
return colorIterator.next();
}
else
{
colorIterator = colorsList.iterator();
return colorIterator.next();
}
}
and here is your listener
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int color = getNextColor();
layout.setBackgroundColor(color);
}
});

Categories

Resources