i am very new to programming language. and in android studio, I am trying to get text from EditText display it on TextView when the user press the button. but it goes wrong. anyone help me? please, thanks.
TextView welcome_lbl;
EditText enter_text;
Button welcome_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
welcome_lbl = (TextView) findViewById(R.id.welcome_lbl);
enter_text = (EditText) findViewById(R.id.enter_text);
welcome_btn = (Button) findViewById(R.id.welcome_btn);
welcome_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
welcome_lbl.setText(enter_text.getText().length());
}
});
}
String text = enter_text.getText.toString();
welcome_lbl.setText(text);
Or with your way,
welcome_lbl.setText(enter_text.getText.toString());
Related
It has been awhile so I am struggling with fragments a bit. I am working on demos right now to get familiar again. What I am doing is in the main activity, I have a button to click which opens a fragment to enter information like name, address, etc. For some reason, the main activity doesn't go away and shows over the fragment. What am I doing wrong???
Note: I have not wired the cancel button yet to go back to Main Activity without doing anything.
Main Activity:
public class MainActivity extends AppCompatActivity {
public static User mUser = new User();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button requestServiceButton = findViewById(R.id.servicesButton);
requestServiceButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
NewUser fragment = new NewUser();
transaction.replace(R.id.fragmentHolder, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
NewUser Class:
public class NewUser extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.dialog_new_user, container, false);
final EditText fName = v.findViewById(R.id.firstNameEditText);
final EditText lName = v.findViewById(R.id.lastNameEditText);
final EditText address = v.findViewById(R.id.editTextAddress);
final EditText city = v.findViewById(R.id.cityEditText);
final EditText email = v.findViewById(R.id.editTextEmailAddress);
final EditText password = v.findViewById(R.id.editTextPassword);
final EditText phoneNumber = v.findViewById(R.id.editTextPhone);
final EditText username = v.findViewById(R.id.usernameEditText);
final EditText zipCode = v.findViewById(R.id.zipEditText);
Button okButton = v.findViewById(R.id.btnOK);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.mUser.setFirstName(fName.getText().toString());
MainActivity.mUser.setLastName(lName.getText().toString());
MainActivity.mUser.setAddress(address.getText().toString());
MainActivity.mUser.setCity(city.getText().toString());
MainActivity.mUser.setEmailAddress(email.getText().toString());
MainActivity.mUser.setPassword(password.getText().toString());
MainActivity.mUser.setPhoneNumber(phoneNumber.getText().toString());
MainActivity.mUser.setUsername(username.getText().toString());
MainActivity.mUser.setZipCode(zipCode.getText().toString());
}
});
Button cancelButton = v.findViewById(R.id.btnCancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return v;
}
Hell o,
I would like to know if it's preferrable to inflate a fragment extending DialogFragment class or to display the dialog from a simple method added in onCreateView() ?
Here's a snippet for the second solution from internet (based on an activity and not fragment) :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positiveDialog = findViewById(R.id.btnPositiveDialog);
negativeDialog = findViewById(R.id.btnNegativeDialog);
positiveDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_postive_layout);
}
});
negativeDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_negative_layout);
}
});
}
private void showAlertDialog(int layout){
dialogBuilder = new AlertDialog.Builder(MainActivity.this);
View layoutView = getLayoutInflater().inflate(layout, null);
Button dialogButton = layoutView.findViewById(R.id.btnDialog);
dialogBuilder.setView(layoutView);
alertDialog = dialogBuilder.create();
alertDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
}
My only concern is that i use a fragment among navigationDrawer where i want to display my alertDialog (1 editText and 2 buttons), and i would like to prevent user from closing the dialog via backbutton or clicking outside the dialog.
Thank you very much for helping me out
the code works fine at the first time but when i rotate the screen to landscape it is disabling my fragment button even it is no longer usable after i change the orientation back to normal(portrait). why this is happening??
Main Activity
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
TextView textView;
fragment_one fragment_one;
FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment_one = new fragment_one();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.container,fragment_one).commit();
button = (Button) findViewById(R.id.mybutton_main);
textView = (TextView) findViewById(R.id.mytextView_main);
editText = (EditText) findViewById(R.id.myedittext_main);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("button Clicked");
editText.setText("button Clicked");
}
});
}
}
Fragment
public class fragment_one extends Fragment implements View.OnClickListener {
Button button;
TextView textView;
public fragment_one() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button = (Button) getActivity().findViewById(R.id.mybutton_frag);
textView = (TextView) getActivity().findViewById(R.id.mytextView_frag);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
textView.setText("clicked");
}
}
Try this:
if (savedInstanceState == null) {
fragment_one = new fragment_one();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.container,fragment_one)
.commit();
}
I have a button which adds a textview to my fragment. When I close the activity the textview disappears. The only thing I like to know is how to save and retrieve the created textview.
public class trainingsplan extends Fragment {
ImageButton Btn;
LinearLayout Layout;
private ArrayList<View> mViews= new ArrayList<View>();
int i;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.trainingsplan, container, false);
setHasOptionsMenu(true);
Btn = (ImageButton) rootView.findViewById(R.id.imageButton3);
Layout = (LinearLayout) rootView.findViewById(R.id.linearLayout);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView Test = new TextView(getActivity());
Test.setLayoutParams(lparams);
Test.setText("Hallo wie gehts dir ?");
Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Layout.addView(Test);
mViews.add(Test);
}
});
return rootView;
}
My question is how to stop the media player when the user presses the back or the home button? I really need help..Can some one please give a code and tell me where to incorporate it in the activity.
This is my code:
MY JAVA CODE:
public class fbactivity extends Activity {
private MediaPlayer myMediaPlayer;
private MediaPlayer my1MediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myMediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.milyonlarcataraftaryanyana);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.magnumsilah);
myButtonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.Button03);
myButtonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.pause();
}
});
my1MediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.sevmeksenideligibiyurekister);
//Button related to play btn
Button myButtonOne1 = (Button) findViewById(R.id.Button02);
myButtonOne1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
my1MediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo2 = (Button) findViewById(R.id.Button01);
myButtonTwo2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
my1MediaPlayer.pause();
}
});
Button butongeridon=(Button) findViewById(R.id.geridon);
butongeridon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(fbactivity.this,MainActivity.class); // the names of activity as per you program.
startActivity(i);
finish();
}
});
}
}
You inflate two buttons, one from R.id.magnumsilah, and another from R.id.Button03. However, the ClickListener is bound to only one of them, and no listener is bound to the back button.
It may well be worth it to be more thorough in you research before posting questions. Just a thought.