I have 2 class, which the first class i created Loop button
and second class i want to get button value from first class..
Here is my code
public class FirstClass extends AppCompatActivity {
public static Button[] btn = new Button[8];
#Override
protected void onCreate(Bundle savedInstanceState) {
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rlid);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
for(int i=0; i<btn.length; i++)
{
btn[i] = new Button(this);
btn[i].setTextColor(Color.BLUE);
btn[i].setText((i + 0) + " ");
btn[i].setLayoutParams(lp);
relativeLayout.addView(btn[l]);
}
}
}
public class SecondClass extends Activity {
Button btn2= (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button bb = FirstClass.btn[2]; //get button no 2 from 1st class
bb.setTextColor(Color.YELLOW); // try to change into yellow but nothing happen
}
});
Although bad practice of using views as static fields, your code should work.
Another problem is that you have created buttons in the first activity and done nothing with them. You should probably add them to some layout of the activity.
The good practice would be not to store buttons or any views in static fields, and get results using Intents. https://developer.android.com/training/basics/intents/result.html
Related
I tried to build an easy Android App using Java and, obviously, Android studio. After adding an OnClickListener the app keeps crashing on the emulator.
public class MainActivity extends AppCompatActivity {
EditText first = findViewById(R.id.firstInput);
EditText second = findViewById(R.id.secondInput);
EditText result = findViewById(R.id.result);
private Button add;
private View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
clicked();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = new Button(this);
add.setOnClickListener(onClickListener);
};
void clicked(){
int i = Integer.parseInt(first.getText().toString());
int z = Integer.parseInt(second.getText().toString());
int r = i + z;
result.setText(r);
}
};
Wrong location of object declaration
It should be below the setContentView
EditText first = findViewById(R.id.firstInput);
EditText second = findViewById(R.id.secondInput);
EditText result = findViewById(R.id.result);
Button haven't added to the layout
add = new Button(this);
addContentView(add, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Note: This is impractical, you should have added this on your layout xml unless you wanted to create dynamic layout
Warning issue of setting a text by integer
From:
result.setText(r);
To:
result.setText(String.valueOf(r));
I am testing my knowledge of android studio and seem to be running into a problem I am trying to make an app that takes the value of a edittext and puts that value into a textview when you click a button. After that it brings you to a page with a back button on it and when I click that it brings me to the first page. But when I try to click the first button it doesn't work anymore.
Code:
public class TestApp extends AppCompatActivity {
private EditText ET;
private Button Add;
private TextView TV;
public String Array[] = new String[9999999];
private Button GoBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_app);
ET = (EditText) findViewById(R.id.ET);
Add = (Button) findViewById(R.id.Add);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getC = ET.getText().toString();
setContentView(R.layout.value);
TV = (TextView) findViewById(R.id.TV);
GoBack = (Button) findViewById(R.id.GoBack);
if (getC.length() > 0){
for (int i = 0; i < Array.length; i++){
if (Array[i] == null){
Array[i] = getC;
TV.setText(Reminders[i]);
}
}
}
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_calender_for_glasses);
}
});
}
});
}
}
thank you for your help.
If not necessary, don't modify the current content view. If you want multiple screens, you have to create multiple activities or fragments...
Moreover :
at the beginning, you inflate R.layout.activity_test_app
when you click on goBack button (1), you inflate R.layout.activity_calender_for_glasses and it's not the same as the first screen
(1) be careful to coding rules: https://source.android.com/setup/contribute/code-style
I am getting started on Android development and I've had trouble finding answers since I may not know how to word things properly. What I am trying to do is setting all buttons invisible once I click on any button. The easy way out of it is this:
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
Button button4 = findViewById(R.id.button4);
setInvisible(button1);
setInvisible(button2);
setInvisible(button3);
setInvisible(button4);
However I feel like this goes against the DRY principle of programming. I mean what if there were 100 buttons?
After doing some thinking, I imagine I can use a loop and have i be the place holder for the numbers of each button. That way it would loop through every single one. However I am not sure on what methods to use.
You can do it as follows:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private int[] btnIds = new int[]{R.id.button1, R.id.button2, R.id.button3, R.id.button4};
private List<Button> buttonList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_group);
for(int i = 0; i < btn.length; i++){
buttonList.add(findViewById(btnIds[i]));
buttonList.get(i).setOnClickListener(this);
}
...
}
...
#Override
public void onClick(View v) {
//Either check for button ids or simply:
for (Button button : buttonList) {
button.setVisibility(View.INVISIBLE);//or View.GONE
}
}
}
Hi I am working on calculator in android.
I am having a EditText in wchich no will displayed depending on click of particular no.for example when user press 1 it will display 1.now when user clicks on 2 it will display 12 and so on.
i have things like this
public class Main extends Activity implements OnClickListener {
public static int no1=0,no2=0,op=0,flag=0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText display1=(EditText) findViewById(R.id.display);
Button one= (Button) findViewById(R.id.one);
one.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId()==R.id.one)
{
switch(flag)
{
case 1:no1*=10+1;display1.setText(no1);
case 2:no2*=10+1;display1.setText(no2);
}
}
but here i am getting error as "display1 cannot be resolved".
any solution to make work..??
Sometimes a complete code example helps:
public class Main extends Activity implements OnClickListener {
private int no1=0;
private int no2=0;
private int op=0;
private int flag=0;
private EditText display1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
display1=(EditText) findViewById(R.id.display);
Button one= (Button) findViewById(R.id.one);
one.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId()==R.id.one)
{
switch(flag)
{
case 1:no1*=10+1;display1.setText(no1);
case 2:no2*=10+1;display1.setText(no2);
}
}
}
Basically you want to declare display1 as a private instance variable. The other variables do not need to be static, and should probably be declared private. And declarations formatted on separate lines for improved readability.
Have fun.
Because it's only available in your onCreate method. Declare it as a field in your class.
private EditText display1;
It should be better to keep a string showing in the EditText so that you have no difficulties appending and erasing. Then you can parse it to an integer so that you will be able to do math with it.
First you should declare your buttons and edit text globally:
public class Main extends Activity implements OnClickListener {
private EditText display1 = (EditText) findViewById(R.id.display);
private Button one= (Button) findViewById(R.id.one);
private Button two= (Button) findViewById(R.id.two);
If you have the button label set as the number you can use it like this:
public void onClick(View view){
display1.setText(display1.getText.toString() + ((Button) view).getText().toString());
}
That way, you can append the numbers to the end of the string in the display. If you want to use the number it's showing, you can use the following code:
int number = Integer.parseInt(display1.getText().toString());
Declare both your EditText and Button globally.
I'm trying to create a view where the user can click a "plus" button, and have additional EditTexts be created. The goal is to have a base of 2 EditTexts, and each time the user clicks the button, add another 2 EditTexts.
How can I do this? I can add EditTexts from Java, but I can't figure out how to add and handle a list of them dynamically.
I was hoping to take however many pairs of EditTexts, and push it into a key/value HashMap or something.
Any ideas of how to do this? Thanks!
public class MyActivity extends Activity {
private LinearLayout main;
private int id = 0;
private List<EditText> editTexts = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
Button addButton = new Button(this);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addEditText();
}
});
Button submit = new Button(this);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (EditText editText : editTexts) {
editText.getText().toString();
// whatever u want to do with the strings
}
}
});
main.addView(addButton);
main.addView(submit);
setContentView(main);
}
private void addEditText() {
LinearLayout editTextLayout = new LinearLayout(this);
editTextLayout.setOrientation(LinearLayout.VERTICAL);
main.addView(editTextLayout);
EditText editText1 = new EditText(this);
editText1.setId(id++);
editTextLayout.addView(editText1);
editTexts.add(editText1);
EditText editText2 = new EditText(this);
editText2.setId(id++);
editTextLayout.addView(editText2);
editTexts.add(editText2);
}
Do it in a ListView.
Then you can just add them to a ListAdapter.
And then use adapter.notifyDatasetChanged()
May be I am not clear but Instead of adding Individual edit text you can add as Group View like Linear layout here you can use any flag values to add dynamic name conversions also.
That view you can update into List View like inflating rows in the List View....