Changing EditText to default string - android

hello guys seriously am new to android and please i would like you to help me with this code...to edit it
public class LoginFragment extends Fragment implements OnClickListener {
private EditText login, password, domain;
private ImageView apply;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.setup_generic_login, container, false);
login = (EditText) view.findViewById(R.id.setup_username);
password = (EditText) view.findViewById(R.id.setup_password);
domain = (EditText) view.findViewById(R.id.setup_domain);
apply = (ImageView) view.findViewById(R.id.setup_apply);
apply.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.setup_apply) {
if (login.getText() == null || login.length() == 0 || password.getText() == null || password.length() == 0 || domain.getText() == null || domain.length() == 0) {
Toast.makeText(getActivity(), getString(R.string.first_launch_no_login_password), Toast.LENGTH_LONG).show();
return;
}
SetupActivity.instance().genericLogIn(login.getText().toString(), password.getText().toString(), domain.getText().toString());
}
}}
i'd like to assign domain a default string, (like domain = "pearlcom.au.ug")
i've edited the xml file and removed the EditText field
please don't close my post.......just ignore!
thanks for those who are goin to help me!

you can just do:
android:text="#string/domain" and assign whatever domain string you choose in the strings.xml and you would have that as default.
-> If you want a placeholder displayed, please include
android:hint="#string/enter_domain"

Simply do this in onCreate():
domain.setText("pearlcom.au.ug");

Related

Recyclerview get data from radiobutton

I want select only one radio button in recyclerView and get its data in its Activity.
I have gone through following solutions :
Single selection in RecyclerView
Select only one radiobutton in a recyclerview
And I made one solution :
private static int lastCheckedPos = -1;
binding.radioButton.setChecked(mImagesList.get(position).isSelected());
binding.radioButton.setTag(new Integer(position));
//for default check in first item
if(position == 0 && mImagesList.get(0).isSelected() && binding.radioButton.isChecked())
{
radioButton = binding.radioButton;
lastCheckedPos = 0;
}
binding.radioButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
RadioButton cb = (RadioButton)v;
int clickedPos = ((Integer)cb.getTag()).intValue();
if(cb.isChecked())
{
if(radioButton != null)
{
radioButton.setChecked(false);
mImagesList.get(lastCheckedPos).setSelected(false);
}
radioButton = cb;
lastCheckedPos = clickedPos;
}
else
radioButton = null;
mImagesList.get(clickedPos).setSelected(cb.isChecked());
}
});
I have written this in onBindViewHolder method.
Now to get data i have written this in adapter :
public String getUserId() {
if(lastCheckedPos == -1)
{
return null;
} else {
return mImagesList.get(lastCheckedPos).getUser_code();
}
}
And get this in activity :
userId = adapter.getUserId();
But I am not able to get any data in activity. It is always showing null. Also if I click twice on radio button then it deselected.
Please ask if anything unclear. Any help would be appreciated.
Thank you :)
Replace int clickedPos = ((Integer)cb.getTag()).intValue(); by int clickedPos =position;

next image with text in same time

I'm a beginner Android developer. I have list text when clicked next change text in one textview. I created imagelist how can when click next change imageview same time textview
public class Home extends Fragment implements View.OnClickListener {
int stringIdList[] = {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five};
int imageIdList[] = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};
int stringListCounter = 0;
int imageListCounter = 0;
TextView text21;
ImageView image2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
text21=(TextView) rootView.findViewById(R.id.dyktext);
image2=(ImageView) rootView.findViewById(R.id.imagedyk);
ImageButton next = (ImageButton)rootView.findViewById(R.id.next);
ImageButton previous = (ImageButton) rootView.findViewById(R.id.previous);
#Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.next && stringListCounter < stringIdList.length - 1 ) {
stringListCounter++;
}else if (id == R.id.previous && stringListCounter > 0 ) {
stringListCounter--;
}
text21.setText(stringIdList[stringListCounter]);
}
}
Try this if I understood you correctly...
#Override
public void onClick(View v) {
int id = v.getId();
boolean updateText = false,
updateImage = false;
if(id == R.id.next) {
if (stringListCounter < stringIdList.length - 1 ) {
stringListCounter++;
updateText = true;
}
if (imageListCounter < imageIdList.length - 1 ) {
imageListCounter++;
updateImage = true;
}
}else if (id == R.id.previous) {
if (stringListCounter > 0 ) {
stringListCounter--;
updateText = true;
}
if (imageListCounter > 0 ) {
imageListCounter--;
updateImage = true;
}
}
if (updateText) {
text21.setText(stringIdList[stringListCounter]);
}
if (updateImage) {
image2.setImageResource(imageIdList[imageListCounter]);
}
}
I'm not sure, if I understand your question correctly.
You would like to change the image in the ImageView at the same moment as you change yout text in the TextView, right?
In this case, you simply can add the command for setting a image resource below your setText() command.
In your case:
...
text21.setText(stringIdList[stringListCounter]);
image2.setImageResource(imageIdList[stringListCounter]);
Another hint: you should give your variables meaningful names (for example "textViewTitle" or "descriptionTV" ... instead of "text21")
I hope that I could help you!

How to iterate through a set of buttons in Android Studio?

I'm trying to make my first app which has a set of 15 buttons. when you press a button the color changes between two numbers.Then if you press a specificity different "commit" button the buttons won't change colors any more.
My question right now is how would I be able to iterate through the buttons on the screen? I believe I need a assign the buttons a "name", "type", or something like it, and then find all instances where that happens but I cannot find the relevant getter/setter methods.
Here is my code so far:
public void clickHandler(View view) {
Button btn = (Button) view;
int id = btn.getId();
boolean clicked = isChosen(btn);
if( clicked == true && id != 1) {
btn.setBackgroundColor(-3355444);
}
else{
btn.setBackgroundColor(-16777216);
}
}
public boolean isChosen(Button btn){
ColorDrawable buttonColor = (ColorDrawable) btn.getBackground();
int colorId = buttonColor.getColor();
if(colorId == -16777216){
return true;
}
else return false;
}
public boolean player = true;
public void changeTurn(View view) {
TextView t = (TextView) findViewById(R.id.textView3);
if(player == false) {
t.setText("Player 2's turn");
player = true;
}
else{
t.setText("Player 1's turn");
player = false;
}
Hope this piece of code helps
final int buttonCount = 15;
LinearLayout mLayout = (LinearLayout) findViewById(R.id.pager);
for (int i = 0; i < buttonCount; i++) {
TextView textView = new TextView(this);
textView.setTag(String.valueOf(i));
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int buttonID = Integer.valueOf(view.getTag().toString());
updateColorForButtonAtPosition(buttonCount);
}
});
mLayout.addView(textView);
}
private void updateColorForButtonAtPosition (int buttonCount){
// add your functionality here
}

Android: remove focus on EditText in PreferenceGroup

I have a form that when this form is submitted, I want to disable my textedit, would you please help me in this implementation,
Thanks in advance!
Here is part of my class that submitted button is:
public class MyForm extends PreferenceGroup implements View.OnClickListener {
if (isFormSubmited) {
submitter.disable();
FormField.buttonCatchButton.setText("View");
updateForm();
}
my button for disable the editTex
private void disable(ViewGroup layout) {
layout.setEnabled(false);
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
if (child instanceof ViewGroup) {
disable((ViewGroup) child);
} else {
child.setEnabled(false);
}
}
}
my onclick method in another class
public void onClick(View v) {
FormFragment formFragment = new FormFragment();
Bundle bundle = new Bundle();
bundle.putString(Constants_Args.ITEMS_KEY, itemsKey);
if (catchFormFile != null) {
bundle.putString(Constants_Args.CATCH_FORM_FILE_PATH, catchFormFile.getAbsolutePath());
}
if (catchFormJSONObject != null) {
bundle.putString(Constants_Args.CATCH_FORM_JSONOBJECT, catchFormJSONObject.toString());
}
if (catchSpeciesJSONArray != null) {
bundle.putString(Constants_Args.CATCH_SPECIES_JSONARRAY, catchSpeciesJSONArray.toString());
}
catchFormFragment.setArguments(bundle);
MainActivity mainActivity = (MainActivity)getContext();
mainActivity.pushFragment(formFragment);
}
My problem is that I don't know how to set: If form submitted, formFragment needs to let its child views that the field is disabled

Emulator doesn't give Error but Device gives java.lang.NullPointerException on putExtra()

I have a big problem please someone help me please.
I have an ActionBar with Tabs and 3 Fragments.
In onCreateView method i have this:
MainActivity.java
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
TextView textView;
final Intent intent = new Intent(getActivity(),ResultActivity.class);
if(getArguments().getInt(ARG_SECTION_NUMBER) == 1){
rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button btn = (Button) rootView.findViewById(R.id.procura_ingredientes);
final EditText ing_1 = (EditText) rootView.findViewById(R.id.ingredient_1);
final EditText ing_2 = (EditText) rootView.findViewById(R.id.ingredient_2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent.putExtra(MOD_SEARCH,1);
String ingredient_1 = ing_1.getText().toString();
String ingredient_2 = ing_2.getText().toString();
if(ingredient_1.length() <= 0) ingredient_1 = "";
if(ingredient_2.length() <= 0) ingredient_2 = "";
intent.putExtra("ingredient_1",ingredient_1);
intent.putExtra("ingredient_2",ingredient_2);
startActivity(intent);
}
});
}else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
rootView = inflater.inflate(R.layout.fragment_two, container, false);
GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
final ImageAdapter imageAdapter = new ImageAdapter(getActivity());
gridview.setAdapter(imageAdapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(getActivity()," " + position, Toast.LENGTH_SHORT).show();
long cat = imageAdapter.getItemId(position);
if(cat <= 0) cat = 1;
intent.putExtra(MOD_SEARCH,2);
intent.putExtra("cat_id",cat);
startActivity(intent);
}
});
}else if(getArguments().getInt(ARG_SECTION_NUMBER) == 3) {
rootView = inflater.inflate(R.layout.fragment_three, container, false);
final EditText editText = (EditText) rootView.findViewById(R.id.recipe_search_term);
Button button = (Button) rootView.findViewById(R.id.submit_recipe_search);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int cont = editText.getText().toString().length();
if (cont < 2) {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setTitle("Atenção");
dialog.setMessage("O nome da receita precisa ter mais de " + cont + " letras ");
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
dialog.show();
} else {
intent.putExtra(MOD_SEARCH,0);
String recipe = editText.getText().toString();
if(recipe.length() <= 0) recipe = "";
intent.putExtra("recipe_name",recipe);
startActivity(intent);
}
}
});
}
return rootView;
/* textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));*/
}
}
If you look you see
if(getArguments().getInt(ARG_SECTION_NUMBER) == 1){
}else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
else
this manage what tab user are in, all go to same ResultActivity but pass diferent ExtraStrings,
intent.putExtra(MOD_SEARCH,0);
intent.putExtra(MOD_SEARCH,1);
intent.putExtra(MOD_SEARCH,2);
the MOD_SEARCH control what Result was execute.
ResultActivity.java
public static final String MOD_SEARCH = null;
mode = getIntent().getIntExtra(MOD_SEARCH,0);
if(mode == 0){ ..
}else if(mode == 1) ..
}else if(mode == 2) ..
So, in emulator it works fine all, when i click in in each fragment it shows me the correct information.
After publishing app to Google Play few days ago and i get this errors:
java.lang.NullPointerException
at android.content.Intent.putExtra(Intent.java:5558)
at com.iondev.cozinhaparasolteiros.MainActivity$PlaceholderFragment$2.onItemClick(MainActivity.java:256)
and
java.lang.NullPointerException
at android.content.Intent.putExtra(Intent.java:5325)
at com.iondev.cozinhaparasolteiros.MainActivity$PlaceholderFragment$1.onClick(MainActivity.java:226)
I dont understand because in emulator it works without error and device have error, i don't know what is wrong, please someone have a suggestion, need repair because are in store.
Replace this
public static final String MOD_SEARCH = null;
for this
public static String MOD_SEARCH = "";
I think your problem is with the word "final" as initializing the variable with "null" being kind "final" will not be able to change its value later, so to assign to the Intent is Null.
I hope you serve.
Sorry for my low level of English.
You have not initialized Intent any where in your code because it giving NullPointerException here..
intent.putExtra(MOD_SEARCH,2);
intent.putExtra("cat_id",cat);
take it as local and initilize where ever you want..

Categories

Resources