It's quite simple. I have a button that has an onClickListener which takes a text from EditText, hides it and sets it to TextView which it shows in the same place. Now, i want to do that to 2 more different edit texts and text views with the same button. Code below.
XML with my buttons:
<?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"
tools:context = ".profil"
android:orientation = "vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="230dp"
android:background="@color/pozadina"
android:id="@+id/rltv">
<ImageView
android:id="@+id/slikaProfil"
android:layout_width="130dp"
android:layout_height="160dp"
android:src="@drawable/profilna"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/textProfil1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/userProfil"
android:layout_below="@+id/slikaProfil"
android:layout_centerInParent="true"
android:textSize="20sp"
android:textStyle="bold"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@+id/rltv">
<TextView
android:id="@+id/imeProfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account info"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ime i prezime"
android:layout_marginLeft="70dp"
android:drawableLeft="@drawable/profil"
android:drawablePadding="5dp"
android:id="@+id/imeProfil1"
android:textSize="16sp"
android:layout_below="@+id/imeProfil"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite ime i prezime"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="@+id/imeProfil2"
android:textSize="16sp"
android:background="@null"
android:layout_below="@+id/imeProfil1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telefon"
android:layout_marginLeft="70dp"
android:drawableLeft="@drawable/telefon"
android:drawablePadding="5dp"
android:id="@+id/telefonProfil1"
android:textSize="16sp"
android:layout_below="@+id/imeProfil2"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite broj telefona"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="@+id/telefonProfil2"
android:textSize="16sp"
android:background="@null"
android:layout_below="@+id/telefonProfil1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail"
android:layout_marginLeft="70dp"
android:drawableLeft="@drawable/mail"
android:drawablePadding="5dp"
android:id="@+id/mailProfil1"
android:textSize="16sp"
android:layout_below="@+id/telefonProfil2"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite e-mail"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="@+id/mailProfil2"
android:textSize="16sp"
android:background="@null"
android:layout_below="@+id/mailProfil1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adresa"
android:layout_marginLeft="70dp"
android:drawableLeft="@drawable/adresa"
android:drawablePadding="5dp"
android:id="@+id/adresaProfil1"
android:textSize="16sp"
android:layout_below="@+id/mailProfil2"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Upisite adresu"
android:layout_marginTop="-5dp"
android:layout_marginLeft="70dp"
android:id="@+id/adresaProfil2"
android:textSize="16sp"
android:background="@null"
android:layout_below="@+id/adresaProfil1"
/>
<Button
android:id="@+id/prihvati"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_below="@+id/adresaProfil2"
android:layout_marginLeft="70dp"
android:text="@string/submit"
android:onClick="zamijeni"/>
</RelativeLayout>
</RelativeLayout>
My java file:
public class profil extends Fragment {
private Button dugme4;
private TextView text1;
private EditText text2;
public profil(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.activity_profil, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
text1 = view.findViewById(R.id.imeProfil1);
text2 = view.findViewById(R.id.imeProfil2);
dugme4 = view.findViewById(R.id.prihvati);
text2.setVisibility(View.INVISIBLE);
dugme4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String tekst = text2.getText().toString();
if (text1.getVisibility()== View.VISIBLE){
text1.setVisibility(View.INVISIBLE);
text2.setVisibility(View.VISIBLE);
}else if (text2.getVisibility() == View.VISIBLE){
text2.setVisibility(View.INVISIBLE);
text1.setVisibility(View.VISIBLE);
text1.setText(tekst);
}
}
});
}
}
So as you see i have an if function that on click gets the text, makes the edit text invisible and text visible and at the end sets the text to the textView.
Should i do a few more if functions or should it be done differently.
You can achive this more efficiently by use only a edittext.. no need to use text view.. I am giving a sample demo below
public class profil extends Fragment {
private Button dugme4;
//private TextView text1;
private EditText text2;
public profil(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.activity_profil, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// text1 = view.findViewById(R.id.imeProfil1);
text2 = view.findViewById(R.id.imeProfil2);
dugme4 = view.findViewById(R.id.prihvati);
// text2.setVisibility(View.INVISIBLE);
dugme4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (text2.isEnabled()){
//make it not editable and non focusable
text2.setEnable(false);
text2.setCursorVisible(false);
//for clearing underline of editText so that it looks like textview
text2.setBackgroundResource(android.R.color.transparent);
}else{
text2.setEnabled(true);
text2.setCursonVisible(true);
// set any color what you want as underline
text2.setBackgroundResource(android.R.color.black);
}
}
});
}
}
Try it.hope it will solve your problem by reducing extra element and complexity.