Android Tips
Access the attributes use in XML by :
EditText ed_1 = findViewById(R.id.ed_1);
Add Click property to the attributes by:
btn_sum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//Casting to Integer:
int num1=Integer.parseInt(ed_1.getText().toString());
int num2=Integer.parseInt(ed_2.getText().toString());
int sum=num1+num2;//Casting to String:
String Result=String.valueOf(sum);//Set Text :
txt_result.setText(Result);
}
});Intent Practice:
next_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent next =new Intent( MainActivity.this, MainActivity2.class);//orstartActivity(next);Intent next =new Intent( getApplicationContext, MainActivity2.class);
finish();
}
});To Hide the Action Bar:
getSupportActionBar().hide();Write A Toast Message:
Toast.makeText(MainActivity.this, "Hii Dear", Toast.LENGTH_SHORT).show();Send Data from one file using putExtra and receive data from 2nd file using getIntent || getStringExtra :
a.putExtra("FirstName",FirstName);a=getIntent().getStringExtra("FirstName");txt1.setText(a);Splash Screen Code:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i =new Intent(getApplicationContext(),MainActivity2.class);
startActivity(i);
finish();
}
},5000);Android ShortCut Key
(to arrange code )alt + ctrl + LCode for Delete button:
btndelete.(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s=input.getText().toString();
s=s.substring(0,s.length()-1)setOnClickListener;input.setText(s);
}
});
Amazing
ReplyDelete