I am a beginner at android app developing and I have few buttons in my page which i want to change the image with a click. However, eventhough i have labeled the buttons according to the order and used the id's to create the buttons in the oncreate method, when I test it doesnt change the image for the proper button. For example, if the code is written to trigger 2nd button, it triggers the 5th button. I hope my question is understandable. The code is below.
public class StartGameActivity extends Activity implements View.OnClickListener{
Button one, two, three, four, five, six, seven, eight, nine;
int counter, onOff;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.game_start);
one = (Button) findViewById(R.id.empty_button);
two = (Button) findViewById(R.id.empty_button2);
three = (Button) findViewById(R.id.empty_button3);
four = (Button) findViewById(R.id.empty_button4);
five = (Button) findViewById(R.id.empty_button5);
six = (Button) findViewById(R.id.empty_button6);
seven = (Button) findViewById(R.id.empty_button7);
eight = (Button) findViewById(R.id.empty_button8);
nine = (Button) findViewById(R.id.empty_button9);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v == one){
v.setPressed(true);
v.setBackgroundResource(R.drawable.circle);
}
else if(v == two){
v.setPressed(true);
v.setBackgroundResource(R.drawable.circle);
}
This is the xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:contentDescription="#string/game_board"
android:src="#drawable/gameboard2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
<Button
android:id="#+id/empty_button"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignBaseline="#+id/empty_button3"
android:layout_alignBottom="#+id/empty_button3"
android:layout_alignLeft="#+id/empty_button4"
android:background="#drawable/customemptybutton" />
<Button
android:id="#+id/empty_button2"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_above="#+id/empty_button5"
android:layout_alignLeft="#+id/empty_button5"
android:background="#drawable/customemptybutton2"
android:gravity="center" />
it pretty much goes similarly for until the end
Just read your comment.
Please post your xml, are you sure the id's assumed by the Java matches the XML? Also, you don't need to setPressed() in the onClick(). Since all the buttons are sharing the same listener, I've seen practices like the following work better rather than holding references to all your buttons.
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// application logic
case R.id.button2:
//application logic
}
}
Related
I have this problem: I want to put some Buttons at a certain locations, for example at the four squares of the screen (resolved) and I also want them to become red when clicking on each one, at exactly those locations (not resolved yet).
This is the main xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_weight="15"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:components="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/layout1">
<Button
android:id="#+id/one"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#color/colorPrimary"/>
<Button
android:id="#+id/two"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:background="#color/colorPrimary" />
<Button
android:id="#+id/three"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary" />
<Button
android:id="#+id/four"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="#color/colorPrimary"/>
</RelativeLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
And this is the main java:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btnTag = new Button(this);
btnTag.setLayoutParams(rel_btn);
btnTag.setBackgroundColor(Color.BLUE);
btnTag.setOnClickListener(listen);
layout.addView(btnTag);
}
private View.OnClickListener listen = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (pressed != null) {
Button button1 = (Button) pressed;
button1.setBackgroundColor(Color.BLUE);
}
Button button2 = (Button) view;
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(8, Color.RED);
button2.setBackgroundDrawable(drawable);
pressed = view;
}
in onCreate set up your buttons:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button one = (Button) findViewById(R.id.one);
one.setOnClickListener(listen);
Button two = (Button) findViewById(R.id.two);
two.setOnClickListener(listen);
Button three = (Button) findViewById(R.id.three);
three.setOnClickListener(listen);
Button four = (Button) findViewById(R.id.four);
four.setOnClickListener(listen);
and finally create your listener for all 4 of the buttons:
private View.OnClickListener listen = new View.OnClickListener() {
#Override
public void onClick(View view) {
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(8, Color.RED);
Button button = (Button) view;
button.setBackground(drawable);
}
};
If you were created Buttons in the layout then why are you adding it dynamically.
If you are using layout then inflate these buttons using "findViewById()". And when user will click perform the action to that or all buttons.
one.setBackgroundColor(Color.RED);
But if you are using dynamic button creation then store all 4 button id's which you are creating. Then perform something like this.
buttonId[index].setBackgroundColor(Color.RED);
Make your main class implement to View.OnclickListener
public class MainFragment extends Fragment implements View.OnClickListener {
private void setUpViews() {
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
}}
#Override
public void onClick(View view) {
if(view instance of Button){
button.setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
}
}
}
Well, if you give those buttons IDs, then you can change the color of them when you click it.
So if you tap on buttonID, do your logic and then change the color to buttonID.
// If you're in an activity:
buttonID.setBackgroundColor(getResources().getColor(R.color.red));
Add the color red to your color xml.
this can go inside of your onCreate. the buttonOne and buttonTwo at the IDs to the buttons in your XML.
This is the most BASIC way to do it. There are more ways, you can see those answers here too.
buttonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//logic here
}
});
buttonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//logic here
}
});
When you have a listener changing something is relatively easy. You do not need to implement different methods or check every button one by one. Since Button is a View as well, do the following pseudo-code.
// You need this if part, if you have other views that having the same listener.
// Otherwise, you do not need this if check.
if(is Clicked View is Button) {
// Now here, take the clicked view, which we know it is a button and change its colour.
}
By the way, the code above will be inside the overridden method onClick.
EDIT
To clarify, I am adding a small chunk of code.
#Override
public void onClick(View view) {
// Checking if the clicked View is a Button or not.
if(view instanceof Button) {
// view.getId() returns an integer.
Button clickedButton = (Button) findViewById(view.getId());
// Now, change the button's colour.
clickedButton.setBackground(........);
}
}
Hope it helps. Have a nice day!
I have many buttons with different color names on them.
yellow - red - blue
I want, when user tap on one it creates a border around it (select the button) and in the end of my activity I have another button to SAVE the color user selected.
<Button
android:text="Yellow"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:id="#+id/button1" />
<Button
android:text="Red"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:id="#+id/button2" />
<Button
android:text="SAVE"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:id="#+id/buttonsave" />
java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color);
Button btnYellow;
btnYellow = (Button) findViewById(R.id.button1);
Button btnRed;
btnRed = (Button) findViewById(R.id.button2);
Intent intent = getIntent();
String url2 = intent.getStringExtra("image");
btnYellow.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
how can I selected a button when user click on it and get a value (red,green, red1) when user click in save?
Place each button in a FrameLayout. This will give the button a border. Changing the background color on the FrameLayout will change the border of the button.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnYellow"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:text="Yellow" />
</FrameLayout>
Set an onClickListener for the buttons that looks something like the following, but don't use the hard-coded colors - this is just an example. mLastClicked is a member variable defined as Button mLastClicked.
#Override
public void onClick(View view) {
if (mLastClicked !=null) {
((FrameLayout) mLastClicked.getParent()).setBackgroundColor(0xFFFFFFFF);
}
mLastClicked = (Button) view;
switch (view.getId()) {
case R.id.btnYellow:
((FrameLayout) view.getParent()).setBackgroundColor(0xFFFFFF00);
break;
case R.id.btnRed:
// Similar to yellow
break;
case R.id.btnSave:
// Do something with mLastClicked to save it
break;
}
}
You can define your button as a shape To give it a border, use the element (name the file your.xml and place it in res/drawables):
and Refer this link
I need some help. I need to do a screen similar to the screen lock ios 9. Here is an example of this screen: If there are any lessons or ideas for the layout I will be glad. buttons I wanted to do through tablelayout and circle images, and paste text in them, but it may be better ideas
#metalink : I have implement this in android
what should you do
<RelativeLayout
... >
<LinearLayout
android:id="#+id/contain"
... android:layout_width="250dip"
android:orientation="horizontal"
android:weightSum="4" >
<!-- weightSum to 4 = whatever the screen, display
my children views in 4 sections -->
<View
... android:layout_weight="1"
android:background="#drawable/green_dots" />
<!-- weight to 1 = this takes one section -->
<View
... android:layout_weight="1"
android:background="#drawable/green_dots" />
<!-- weight to 1 = this takes one section -->
<View
... android:layout_weight="1"
android:background="#drawable/green_dots" />
<View
... android:layout_weight="1"
android:background="#drawable/green_dots" />
</LinearLayout>
<RelativeLayout
android:layout_below="#id/contain" ... >
... Here display your buttons (or textviews) with
custom drawable background for each one
</RelativeLayout>
Then in your methods inside this Activity:
// init your buttons var
Button one, two, three, four, five ...;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the layout above
setContentView(R.layout.activity_main);
// init your buttons
one = (Button) findViewById(R.id.button1);
two = (Button) findViewById(R.id.button2);
three = (Button) findViewById(R.id.button3);
... etc.
// set them to your implementation
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
... etc.
}
// call this function when one button is pressed
public void onClick(View view) {
// retrieves the id of clicked button
switch(view.getId()) {
case R.id.button1:
methodToSaveNumber(int);
break;
case R.id.button2:
methodToSaveNumber(int);
break;
case R.id.button3:
methodToSaveNumber(int);
break;
... etc.
}
}
Interesting info: German court just invalidated Apple's slide-to-unlock patent. It is sometimes a good idea not to copy a view 1:1, though this time Apple lost the patent on that. Good luck ;)
germany-slide-to-unlock
Is there a way to set one click listener on all views in a hierarchy in one line? I believe we have to do something like the following:
<LinearLayout id="#+id/parent">
<TextView id="#+id/item1" />
<TextView id="#+id/item2" />
<TextView id="#+id/item3" />
</LinearLayout>
OnClickListener listener = ...;
View view = inflater.inflate(R.layout.the_above_layout);
view.setOnClickListener(listener);
view.findViewById(R.id.item1).setOnClickListener(listener);
view.findViewById(R.id.item2).setOnClickListener(listener);
view.findViewById(R.id.item3).setOnClickListener(listener);
...
But that's really a lot of code for a big view hierarchy. I could make a recursive function to do it, but is there something already available by android that does this for us?
Thanks
Android just implements the OnClickListener for you when you define the android:onClick="someMethod" attribute in xml.
<LinearLayout id="#+id/parent">
<TextView id="#+id/item1" android:onClick="myFancyMethod" />
<TextView id="#+id/item2" android:onClick="myFancyMethod" />
<TextView id="#+id/item3" android:onClick="myFancyMethod" />
</LinearLayout>
Then in your Activity just define a method like this.
public void myFancyMethod(View v) {
// does something very interesting
}
That is probably the fastest way to do it.
that is nice example i think so bcz i m also finding shortest way to clici refer this site for click listner
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3;
#Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do stuff;
break;
case R.id.button2:
// do stuff;
break;
...
}
}
Well, this is the way add this line in your xml-
<TextView id="#+id/item1"
android:onClick="myMethod"
/>
And make sure you write this method in your class in which xml is used
public void myMethod(View v){
//content goes here
}
this will trigger your own methods
I want to add views on click of button. My screen has a text view and an edit text and a "Add More" button. on the click of button another set of text view and edit text should get appended to the screen. I am also planning to add scroll view once the my screen grows longer.
My Code snippet is as follows
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
text.setText("hello how are you?"); //this is just an example, I may want to add lot more here.
linearLayout.addView(text);
setContentView(linearLayout);
}
I know I am missing something very basic but I do not know what is that. :(
Please help.
First of all, you need to initialize each TextView with a reference to the context. So, in your onClick, given that it is in MyActivity.java:
public void onClick(View v) {
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT); // Or what you decide
text.setText("Your text");
linearLayout.addView(text);
}
Second, given that you have a call to setContentView in onCreate which also adds your linearlayout to the view, you do not need to call setContentView(linearLayout) each time the button is clicked.
you should first create a new TextView and EditText add it to the linearLayout using linearLayout.addView(). No need to call setContentView() you probably already added it.
This One help you
In your XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myMainRelativeLayout"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="47dp"
android:text="Add Button" />
And your Java file Like
public class MainActivity extends Activity implements
android.view.View.OnClickListener {
Button btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btnAdd) {
RelativeLayout rl = (RelativeLayout)findViewById(R.id.myMainRelativeLayout);
Button btn = new Button(this);
btn.setId(1234);
btn.setText("Add Button Runtime");
btn.setOnClickListener(this);
rl.addView(btn);
}
if(v.getId() == 1234)
{
Toast.makeText(getApplicationContext(), "This is Your New Button", Toast.LENGTH_LONG).show();
}
}
}
This one just example you can add any view like this