protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
gl = (GridLayout) findViewById(R.id.grid);
array = new Button[7][6];
Button btn;
for(int i=0; i<7; i++)
{
for(int j=0; j<6; j++)
{
btn = new Button(this);
btn.setId(7*i + j + 1);
array[i][j] = btn;
gl.addView(btn);
}
}
turn = 0;
Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show();
}
#Override
public void onClick(View v)
{
Toast.makeText(this, "Test1", Toast.LENGTH_SHORT).show();
if(v instanceof Button)
{
Toast.makeText(this, "Test2", Toast.LENGTH_SHORT).show();
int[] d = GetCellByID(v.getId());
Button b = (Button)v;
b.setEnabled(false);
if(turn == 0)
{
b.setBackgroundColor(Color.YELLOW);
turn = 1;
}
else
{
b.setBackgroundColor(Color.RED);
turn = 0;
}
array[d[0]][d[1]] = b;
}
}
This is the code, the Toasts are for testing if what the code is running.
The activity implements OnClickListener
The onClick method does not work, I used it because I have 42 buttons and I can't write 42 setOnClickListener() methods for each button.
In the code I create, in two loop, 42 buttons (7*6) and every time each button is pressed it would be disabled and change the background color of the button one time yellow next time red and again.
You are missing to call the following inside the nested for loop:
btn.setOnClickListener(this);
Here, this refers to Activity which implements OnClickListener
set the onClickListner for your button you have missed that.
for(int i=0; i<7; i++)
{
for(int j=0; j<6; j++)
{
btn = new Button(this);
btn.setId(7*i + j + 1);
array[i][j] = btn;
gl.addView(btn);
btn.setOnClickListener(this);
}
}
From the code snipper you've posted, I don't see anything linking the onClick method to your button(s).
Try adding
btn.setOnClickListener(this);
into the for-loop
Call method in your onCreate()
yourButton.setOnClickListener(this);
You must call method in your onCreate()
Try this :
yourbuttonname.setOnClickListener(this);
or
yourbuttonname.setOnClickListener(yourActivity.this);
Related
I want to link my Buttons to the associated WebViews. I have 2 lists, one of Button the other one of WebViews. What I'm trying to do is, showing or hiding the webView[x] associated to button[x]
nbObjects = 2;
Button[] buttons = null;
buttons = new Button[nbObjects];
buttons[0] = findViewById(R.id.button0);
buttons[1] = findViewById(R.id.button1);
WeView[] webViews = null;
webViews = new WebView[nbObjects];
webViews[0] = findViewById(R.id.webView0);
webViews[1] = findViewById(R.id.webView1);
for (int i = 0; i < nbObjects; i++) {
webViews[i].setVisibility(View.GONE);
final int j = i;
buttons[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (webViews[j].getVisibility() == View.VISIBLE) {
webViews[j].setVisibility(View.GONE);
} else {
webViews[j].setVisibility(View.VISIBLE);
}
}
});
}
Result: when I click on Button0, everything works. WebView0 shows and hides. But not for WebView1 when I click on Button1.
Solution/Update
My WebViews are actually showing up. I added a background color to spot them when no URL was loaded. Now time to loadURL
I'm adding EditText in linear layout and it gives a view like that in image.
I'm getting this view by using this code.
public class SearchRecipe extends AppCompatActivity {
LinearLayout parentLayout;
ImageButton searchRecipe;
private int EDITTEXT_ID = 1;
private List<EditText> editTextList;
EditText editTextItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_recipe);
setActionBar();
init();
searchRecipe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText editTextItem = (EditText) parentLayout.findViewById(EDITTEXT_ID);
for (int i = 0; i < editTextList.size(); i++) {
Log.e("All Values=", editTextList.get(i).getText().toString());
Toast.makeText(SearchRecipe.this, editTextItem.getText().toString() + " ", Toast.LENGTH_SHORT).show();
}
}
});
}
public void init() {
parentLayout = (LinearLayout) findViewById(R.id.parent_layout); //make sure you have set vertical orientation attribute on your xml
searchRecipe = (ImageButton) findViewById(R.id.search_button);
editTextList = new ArrayList<EditText>();
TextView addMoreText = new TextView(this);
addMoreText.setText("Add More Ingredients");
addMoreText.setGravity(Gravity.CENTER);
addMoreText.setPadding(20, 20, 20, 20);
addMoreText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.add, 0);
addMoreText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editTextItem = new EditText(SearchRecipe.this);
editTextItem.setId(EDITTEXT_ID);
editTextList.add(editTextItem);
EDITTEXT_ID++;
editTextItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cross, 0);
editTextItem.setPadding(20, 20, 20, 20);
editTextItem.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
parentLayout.removeView(editTextItem);
return true;
}
});
parentLayout.addView(editTextItem, 0);
}
});
parentLayout.addView(addMoreText);
}
Now the only problem I'm facing is that. I'm not getting the text from edittext properly. Let me Explain what I want to do.
Click on Add More TextView will add one more edit text.
After adding all edittexts I will click on Search button.
By clicking search button will get the data from edittexs and save in arraylist. I tried a lot but can't do this properly. will you please help me to do this thing ? I'm stuck in from many days.
if you are createing edit text run time only for this purpose then there is no need of below tow lines
editTextItem.setId(EDITTEXT_ID);
EDITTEXT_ID++;
To retrive data from each edit box follow below things
for (EditText editText : editTextList) {
/* now you can get the value from Edit-text and save in the ArrayList
or you can append it in same string*/
yourArraList.add(editText.getText().toString()));
}
Get the editext from your list editTextList
String data = editTextList.get(index).getText().toString();
Add check for editTextList should not be null or empty.
You can iterate over list using for-each loop
for (EditText editText : editTextList) {
// now you can get the value from Edit-text and save in the ArrayList
yourArraList.add(editText.getText().toString()));
}
you can do like below if view inside fragment.
public static String getText(final Activity activity) {
final StringBuilder stringBuilder=new StringBuilder();
LinearLayout scrollViewlinerLayout = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
ArrayList<String> msg = new ArrayList<String>();
for (int i = 0; i < scrollViewlinerLayout.getChildCount(); i++)
{
LinearLayout innerLayout = (LinearLayout) scrollViewlinerLayout.getChildAt(i);
EditText editText = (EditText) innerLayout.findViewById(R.id.meeting_dialog_et);
msg.add(editText.getText().toString());
}
for (int j=0;j<msg.size();j++)
{
stringBuilder.append(msg.get(j)).append(";");
}
Toast t = Toast.makeText(activity.getApplicationContext(), stringBuilder.toString(), Toast.LENGTH_SHORT);
t.show();
return stringBuilder.toString();
}
there is another way is make arraylist Edittexes and add each edittext when it added to layout. then you can get like below:
for (int i = 0; i < Edittexes.size(); i++) {
if (Edittexes.get(i) == view)
{
String text=Edittexes.get(i).getText();
}
}
i make a button to start a game. whenever i click on that button from AVD, unfortunately the program has stopped and i am new to android programming. i try googling my problem and it seems there is something wrong with the code. can anyone help me? i also share a link to my code, visit https://www.dropbox.com/sh/hyitjbgda69rkd4/3Iz8WuM5-5
the main activity
public class MainActivity extends Activity
{
private Game game1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.startbutton);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// Perform action on click
game1 = new Game(this);
setContentView(game1);
}
});
}
}
Game class :
public Game(OnClickListener onClickListener)
{
super((Context) onClickListener);
caneta = new Paint();
this.caneta.setARGB(255, 0, 0, 0);
this.caneta.setAntiAlias(true);
this.caneta.setStyle(Style.STROKE);
this.caneta.setStrokeWidth(5);
l = this.getWidth();
a = this.getHeight();
singlesquare = new Cell[x][y];
int xss = l / x;
int yss = a / y;
for (int z = 0; z < y; z++)
{
for (int i = 0; i < x; i++)
{
singlesquare[z][i] = new Empty(xss * i, z * yss);
}
}
}
You really shouldn't be calling setContentView() more than once. Is that the line that's causing the error? Further, the object type of game1 doesn't appear to be a View.
Look at following android code. IMGS is a 2 dimensional array of ImageView. I am adding onClickListener to it in a for loop. How to identify which view has been clicked ? I dont want to iterate over all the 36 elements using view.getId();.
private static final Integer[] Icons = {
R.drawable.r,
R.drawable.re,
R.drawable.u,
R.drawable.et,
R.drawable.w,
R.drawable.ya
};
private ImageView[][] IMGS= new ImageView[6][6];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IMGS[0][0] = (ImageView) findViewById(R.id.immg11);
for (int i=0; i < 6; i++)
{
for (int j=0; j<6; j++)
{
Drawable d = getResources().getDrawable(Icons[i]);
IMGS[i][j].setImageDrawable(d);
IMGS[i][j].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//HOW TO KNOW WHICH VIEW HAS BEEN CLICKED ?
}
});
}
}
}
Notice the View argument passed to onClick(View v)? It's the view that was clicked.
You can call v.getId() to retrieve its id.
Agreed, but there are 36 elements over which i have to iterate if I use this method. Is there a way to know the indexes of clicked IMGS ?
You can use setTag() to save whatever data you want in each view and retrieve it later with getTag().
i've made a simple counter program, where i click the plus or minus button and it adds or removes 1 from a counter.
I would like to be able to add more counters: ex. counter1, counter2, counter3 etc.
Here's an example of how i'm doing now with 2 counters, but as you can see i have declared my variables, and i would like to generate them automaticly
int counter, counter2;
ImageButton add, sub, add2, sub2, btnSet;
TextView display, display2;
And here's how i am using the plus button
// Plus button setup
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
display.setText(""+counter);
Vibrator vibr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibr.vibrate (20);
You could create a class that holds the information related to your increment button:
class IncrementButton {
int step;
int counter;
ImageButton add, sub;
TextView display;
public IncrementButton(int step) {
this.step = step;
add = ...
sub = ...
display = ...
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter += step;
display.setText("" + counter);
Vibrator vibr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibr.vibrate (20);
}
}
...
}
}
and put objects of that class in a list - for example for 3 buttons:
List<IncrementButton> buttons = new ArrayList<IncrementButton> ();
for (int i = 0; i < 3; i++) {
IncrementButton btn = new IncrementButton(i + 1);
buttons.add(btn);
}
Create your won widget that contains counter, 2 buttons (for add and subscribe) and text view,
put all you logic there (on click listener etc...), and just hold list of how-many-instances-that-you-want in your activity.