java part :
package first.learn.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
void showMyName (String name){
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
txtOne.setText(name);
}
void addTwoNumbers (int a, int b ){
int c = a+b;
Toast.makeText(this, ""+c, Toast.LENGTH_SHORT).show();
txtTwo.setText(""+c );
}
TextView txtOne, txtTwo ;
Button btnOne, btntwo ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOne = (TextView) findViewById(R.id.txtOne);
txtTwo = (TextView) findViewById(R.id.txtTwo);
btnOne = (Button)findViewById(R.id.btnOne);
btntwo = (Button)findViewById(R.id.btntwo);
txtOne.setText("Alhamdulillah");
showMyName("Ahsanul Islam");
addTwoNumbers(12,10);
txtTwo.setText(" this is my test");
btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addTwoNumbers(50, 55);
}
});
btntwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showMyName("Ahsan\n\nRuhul Amin\n\nAbu Bakr\n\nMahmudul Hasan");
}
});
}
}
xml part :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_margin="20dp"
android:id="@+id/txtOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bismillahir Rahmanir Rahim!"
/>
<TextView
android:layout_margin="20dp"
android:id="@+id/txtTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bismillahir Rahmanir Rahim!"
/>
<Button
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button One "
android:id="@+id/btnOne"/>
<Button
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button two "
android:id="@+id/btntwo"/>
</LinearLayout>
No comments:
Post a Comment