In this video it shows the code to fetch the text of button which is clicked in your Android App. It shows how one can type cast the View to Button variable type and fetch the respective text from it in your Android App.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]
Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...
However, the main Java code is copied below also for reference:
package com.programmerworld.textofthebutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}
public void buttonSample(View view){
textView.setText(((Button) view).getText().toString());
}
public void buttonProgrammerWorld(View view){
textView.setText(((Button) view).getText().toString());
}
}
--