I'm using ViewFLipper with Random.nextInt(). to change layouts onClick (Button). Now I have 3 xml layouts. 1_view.xml 2_view.xml 3_view.xml. Starting from 1_view.xml I have a button there. When button clicked I should get a random layout. it works. But the problem is now, sometimes I get the same layout (1_view.xml). When a user clicks a button on (1_view.xml), I want them to go to the layouts I have left (2_view.xml and 3_view.xml).
Codes
Main.xml
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/TVLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="left|top"
android:text="0"
android:textColor="#4CB848"
android:textSize="40sp"
android:textStyle="bold" />
<TextView
android:id="#+id/TVRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/TVLeft"
android:gravity="right"
android:text="0"
android:textColor="#FF0000"
android:textSize="40sp"
android:textStyle="bold" />
</RelativeLayout>
<ViewFlipper
android:id="#+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include
android:id="#+id/1_View"
layout="#layout/1_view" />
<include
android:id="#+id/2_View"
layout="#layout/2_view" />
<include
android:id="#+id/3_View"
layout="#layout/3_view" />
</ViewFlipper>
</LinearLayout>
Main.java
// FlipperView
MyViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
TVRight = (TextView) findViewById(R.id.TVRight);
TVLeft = (TextView) findViewById(R.id.TVLeft);
// 1_view.xml
Q1_btn1 = (Button) findViewById(R.id.btn_1);
Q1_btn2 = (Button) findViewById(R.id.btn_2);
Q1_btn3 = (Button) findViewById(R.id.btn_3);
Q1_btn4 = (Button) findViewById(R.id.btn_4);
Q1_btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Wrongnum++;
WrongResult.setText(Integer.toString(Wrongnum));
}
});
Q1_btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Rightnum++;
RightResult.setText(Integer.toString(Rightnum));
Random RandomView = new Random();
MyViewFlipper.setDisplayedChild(RandomView.nextInt(3));
}
});
Q1_btn3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Wrongnum++;
WrongResult.setText(Integer.toString(Wrongnum));
}
});
Q1_btn4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Wrongnum++;
WrongResult.setText(Integer.toString(Wrongnum));
}
});
You can do this in your code:
Random RandomView = new Random();
int nextViewIndex = RandomView.nextInt(3);
while (nextViewIndex == MyViewFlipper.getDisplayedChild()) {
nextViewIndex = RandomView.nextInt(3);
}
MyViewFlipper.setDisplayedChild(nextViewIndex);
Basically just call Random.nextInt() until it doesn't match current viewFlipper's index.
To only show a viewFlipper once randomly:
// Intialize this once
List<Integer> vfIndices = new ArrayList<Integer>();
for (int i = 0; i < MyViewFlipper.getChildCount(); i++) {
vfIndices.add(i);
}
// when button is clicked
if (vfIndices.size() == 0) {
return; // no more view flipper to show!
}
int viewIndex = RandomView.nextInt(vfIndices.size());
MyViewFlipper.setDisplayedChild(vfIndices.get(viewIndex));
vfIndicies.remove(viewIndex);
The idea is use an ArrayList to keep track of the viewFlipper index that has not been shown. When button is clicked, pick an index randomly from this array and set the viewFlipper to. Then remove that index from the ArrayList. Next time the button is clicked, it will show one of the remaining flippers.
Related
This is my child xml file(repeat.xml) which i want to add in main xml file
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="200dp"
android:id="#+id/divider_parent"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:text="enter name here"
android:id="#+id/tv2"
android:layout_marginTop="90dp"/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="edit"
android:id="#+id/b1"
android:layout_marginTop="160dp"/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="save"
android:id="#+id/b3"
android:layout_marginTop="160dp"
android:layout_marginLeft="110dp"/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="button2"
android:id="#+id/b2"
android:layout_marginTop="160dp"
android:layout_marginLeft="220dp"/>
</RelativeLayout>
</FrameLayout>
this is my main.xml
<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/main"
tools:context="com.example.pawan.recyclerview.Main2Activity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main1"
android:layout_marginTop="200dp">
</RelativeLayout>
this is my main.java
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
RelativeLayout rl=findViewById(R.id.main1);
// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// params.addRule(RelativeLayout.BELOW,R.id.main1);
for (int i=0;i<4;i++) {
View child = getLayoutInflater().inflate(R.layout.repeat, null);
rl.addView(child);
}
final EditText textView2=findViewById(R.id.tv2);
textView2.setEnabled(false);
Button button1=findViewById(R.id.b1);
Button button2=findViewById(R.id.b2);
Button button3=findViewById(R.id.b3);
final TimePicker tp=findViewById(R.id.timePicker);
tp.setEnabled(false);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView2.setEnabled(false);
tp.setEnabled(false);
Toast.makeText(getApplicationContext(),textView2.getText().toString()+" time is:" +tp.getCurrentHour() + ":" + tp.getCurrentMinute(),Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(Main2Activity.this, Main3Activity.class);
startActivity(intent);
finish();
}
});
}
}
when i do not use the for loop in main.java and direct add child view only once it's working fine. But since i want to add it multiple times and on adding it using for loop multiple times the repeat.xml buttons stops working, thwy does'nt give any feedback i.e they becomes fixed(not clickable) and textview are not editable. Please give a solution for this :)
Your main error is here:
for (int i=0;i<4;i++) {
// add several layouts
}
// findViewById is outside the loop. So, this code will be executed only once and only for one view.
findViewById does not automatically changes all view with that ID. It just seach and return the first view with that ID. If you have more than one view using the same ID only the first view with that ID will be returned (which is your case because you are inflating same layout several times).
Also, you must find for a View not in the whole screen (since you have several views with same ID). You can search "inside" a specific layout or inside a view...
Instead of:
// Search a view inside the whole screen (root layout).
findViewById(R.id.tv2);
You can use:
// Search a view only inside child
child.findViewById(R.id.b1);
You should do something like:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
RelativeLayout rl=findViewById(R.id.main1);
for (int i=0;i<4;i++) {
View child = getLayoutInflater().inflate(R.layout.repeat, null);
rl.addView(child);
// -----------------------------------------------------------------
// Not that I'm looking for a R.id.tv2 but inside child view... and not in root view
// Use child.findViewById(R.id.tv2) instead of findViewById(R.id.tv2)
final EditText textView2 = child.findViewById(R.id.tv2);
textView2.setEnabled(false);
// SAME HERE
Button button1 = child.findViewById(R.id.b1);
Button button2 = child.findViewById(R.id.b2);
Button button3 = child.findViewById(R.id.b3);
final TimePicker tp = child.findViewById(R.id.timePicker);
tp.setEnabled(false);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView2.setEnabled(false);
tp.setEnabled(false);
Toast.makeText(getApplicationContext(),textView2.getText().toString()+" time is:" +tp.getCurrentHour() + ":" + tp.getCurrentMinute(),Toast.LENGTH_SHORT).show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(Main2Activity.this, Main3Activity.class);
startActivity(intent);
finish();
}
});
}
Note that are better ways to what you want such as ListView, RecyclerView etc... but this is another history.
I am trying to make a customisation app for wooden signs. When the user clicks say on a 10 x10 size sign i want the edit text box to change size to demonstrate what they have done. Can this be done on android studio?
This is my code so far, I think what i have done is changed the size of the text but it crashes when the button is clicked so i don't know.
Java File:
public class Letters extends AppCompatActivity {
int txtSize = 14;
EditText Wood;
Button bSize, bSize1, bSize2, bSize3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_letters);
Button bSize = (Button) findViewById(R.id.bSize);
Button bSize1 = (Button) findViewById(R.id.bSize1);
Button bSize2 = (Button) findViewById(R.id.bSize2);
Button bSize3 = (Button) findViewById(R.id.bSize3);
bSize.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 15) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
bSize1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 16) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
bSize2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 17) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
bSize3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 18) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
}
}
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="60dp"
android:layout_width="match_parent"
android:weightSum="1">
<Button
android:id="#+id/bSize"
android:text="15 x 10 cm "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bSize" />
<Button
android:id="#+id/bSize1"
android:text="20 x 15 cm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bSize1" />
<Button
android:id="#+id/bSize2"
android:text="30 x 7 cm"
android:layout_width="103dp"
android:layout_height="61dp"
android:onClick="bSize2" />
<Button
android:id="#+id/bSize3"
android:text="30 x 20 cm"
android:layout_width="0dp"
android:layout_marginBottom="20dp"
android:layout_height="160dp"
android:onClick="bSize3"
android:layout_gravity="center_vertical" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/Wood"
android:textSize="40dp"
android:layout_alignTop="#+id/button3"
android:layout_centerHorizontal="true"
android:background="#795548"
android:hint="Choose Letters.."/>
</LinearLayout>
Your application crashes probably because of a NullPointerException. This is because you have not initialized your Wood variable in the onCreate method.
What you should do is this:
Wood = (EditText) findViewById(R.id.Wood);
Button bSize = (Button) findViewById(R.id.bSize);
Button bSize1 = (Button) findViewById(R.id.bSize1);
Button bSize2 = (Button) findViewById(R.id.bSize2);
Button bSize3 = (Button) findViewById(R.id.bSize3);
When your app crashes, there is a logcat error stacktrace in Android Studio. The stacktrace shows you what exception have caused the crash. You can easily solve problems by looking at the logcat logs.
You never initialized your EditText which is causing NullPointerException
Add this
Wood = (EditText) findViewById(R.id.Wood);
inside onCreate method.
Update: This is how you can change the height and width of EditText on button Click
final LayoutParams lparams = new LayoutParams(150,50); // First param is Width and second is height
Wood.setLayoutParams(lparams);
well i'm looking for a way to flip the button on a linear layout vertical here is a screen shot of the buttons
the xml code is
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:id="#+id/buttons_layout">
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iv_green"
android:layout_weight="1.0"
android:src="#drawable/leaf_green"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iv_other"
android:layout_weight="1.0"
android:src="#drawable/leaf_other"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iv_red"
android:layout_weight="1.0"
android:src="#drawable/leaf_red"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iv_yellow"
android:layout_weight="1.0"
android:src="#drawable/leaf_yellow"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
</LinearLayout>
what i want to do is that when i push flip button on (which is another button) i want to change the position of the current button i will explain in pics:
okay guys i used the idea that Arnab Told me about in the post below, well i want to randomly flip the image buttons so i did the following script:
public void onClick(View v) {
btnsLayout.removeAllViews();
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i = 0; i < 4; i++)
{
a.add(i);
}
Collections.shuffle(a);
btnsLayout.addView(Other, a.get(0).intValue());
btnsLayout.addView(Green, a.get(1).intValue());
btnsLayout.addView(Red,a.get(2).intValue());
btnsLayout.addView(Yellow,a.get(3).intValue());
}
the application is crashing the crash reason is:
java.lang.IndexOutOfBoundsException: index=2 count=1
what does it means ?
Thank you !
You can do this inside onCLick(View view) of Flip Button:
// Get all the views
LinearLayout btnsLayout = (LinearLayout) findViewById(R.id.buttons_layout);
ImageButton ivGreen = (ImageButton) findViewById(R.id.iv_green);
ImageButton ivOther = (ImageButton) findViewById(R.id.iv_other);
ImageButton ivRed = (ImageButton) findViewById(R.id.iv_red);
ImageButton ivYellow = (ImageButton) findViewById(R.id.iv_yellow);
// Remove views
btnLayout.removeView(ivGreen);
btnLayout.removeView(ivRed);
// ReAdd views in new index[Index 0 = position 1, Index 2 = position 3]
btnLayout.addView(ivRed, 0);
btnLayout.addView(ivGreen, 2);
Similarly :
// Remove views
btnLayout.removeView(ivOther);
btnLayout.removeView(ivYellow);
// ReAdd views in new index[Index 1 = position 2, Index 3 = position 4]
btnLayout.addView(ivYellow, 1);
btnLayout.addView(ivOther, 3);
First, you should rename the ids of the ImageButtons like :
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/first_leaf"
android:layout_weight="1.0"
android:src="#drawable/leaf_green"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/second_leaf"
android:layout_weight="1.0"
android:src="#drawable/leaf_other"
android:layout_gravity="fill_vertical"
android:scaleType="fitXY"
android:onClick="HandleClick" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/third_leaf"
android:layout_weight="1.0"
.../>
Then find your flip button in your java class and set an OnClickListener
like this:
myFlipButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
}):
Then, in the onClick function, perform the changes:
myFlipButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myFirstLeaf.setImageDrawable(getResources().getDrawable(R.drawable.leaf_red));
mySecondLeaf.setImageDrawable(getResources().getDrawable(R.drawable.leaf_yellow));
...
}
});
Hope it helps.
You can use the imgButton.setBackgroundResource(); method.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
imgButton1.setBackgroundResource(R.drawable.image3);
imgButton3.setBackgroundResource(R.drawable.image1);
}
});
If you want to swap the views you need to use a ViewGroup and you can set the index accordingly.
How do I change the position of a view in a Linear Layout.?
Hello Using Arnab Suggestion and some tuning i found a solution: the problem was that you need to insert the index 1 2 3 4 you can't randomly add the views to the linear layout.
public void addListenerOnButton() {
Button flip = (Button) findViewById(R.id.btn_flip);
final LinearLayout btnsLayout = (LinearLayout) findViewById(R.id.buttons_layout);
final ImageView Other = (ImageView) findViewById(R.id.iv_other);
final ImageView Green = (ImageView) findViewById(R.id.iv_green);
final ImageView Red = (ImageView) findViewById(R.id.iv_red);
final ImageView Yellow = (ImageView) findViewById(R.id.iv_yellow);
flip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnsLayout.removeAllViews();
ArrayList<Integer> a = new ArrayList<Integer>();
for (int i = 0; i < 4; i++) {
a.add(i);
}
Collections.shuffle(a);
for (int k = 0; k<=3 ; k++){
if (a.get(0).intValue() == k) {
btnsLayout.addView(Other, a.get(0).intValue());
}
if (a.get(1).intValue() == k) {
btnsLayout.addView(Green, a.get(1).intValue());
}
if (a.get(2).intValue() == k) {
btnsLayout.addView(Red, a.get(2).intValue());
}
if (a.get(3).intValue() == k) {
btnsLayout.addView(Yellow, a.get(3).intValue());
}
}
}
I have a test code using the View Property Animation to switch locations of two buttons. The code is as below:
public class TestAnimationActivity extends Activity {
private View mainView;
private View TempView;
private Button B1, B2, B3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate your view
setContentView(R.layout.main_test);
mainView = findViewById(R.id.mainTest);
B1 = (Button) mainView.findViewById(R.id.B1);
B2 = (Button) mainView.findViewById(R.id.B2);
B3 = (Button) mainView.findViewById(R.id.B3);
OnClickListener OCL = new OnClickListener(){
#Override
public void onClick(View v) {
if(TempView == null){
TempView = v;
}else{
int originalPos1[] = new int[2];
TempView.getLocationOnScreen( originalPos1 );
int originalPos2[] = new int[2];
v.getLocationOnScreen( originalPos2 );
v.animate().translationX((float)originalPos1[0] - (float)originalPos2[0]);
v.animate().translationY((float)originalPos1[1] - (float)originalPos2[1]);
TempView.animate().translationX((float)originalPos2[0] - (float)originalPos1[0]);
TempView.animate().translationY((float)originalPos2[1] - (float)originalPos1[1]);
TempView = null;
}
}};
B1.setOnClickListener(OCL);
B2.setOnClickListener(OCL);
B3.setOnClickListener(OCL);
}
}
The XML file is as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainTest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/B1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#B4B4B4"
android:gravity="center" />
<Button
android:id="#+id/B2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#470047"
android:gravity="center" />
<Button
android:id="#+id/B3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center" />
</LinearLayout>
When I was testing this code on my phone. The animation did not work properly.
Two buttons will switch position the first time, but the button will go to wrong position if I continue clicking them. It seems that the button still calculate the position it should move to based on its original location when the app launched.
Anyone knows what is going wrong?
I have seven equal Buttons in LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:weightSum="7" >
<Button
android:id="#+id/btn_mon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#color/white"
android:text="0" />
<Button
android:id="#+id/btn_tus"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_wen"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_thu"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_fri"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_sat"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_sun"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
</LinearLayout>
Their OnClickListener is also equal (and initializing too):
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.calendar_row, null);
}
final Button btn_mon = (Button)convertView.findViewById(R.id.btn_mon);
final Button btn_tus = (Button)convertView.findViewById(R.id.btn_tus);
final Button btn_wen = (Button)convertView.findViewById(R.id.btn_wen);
final Button btn_thu = (Button)convertView.findViewById(R.id.btn_thu);
final Button btn_fri = (Button)convertView.findViewById(R.id.btn_fri);
final Button btn_sat = (Button)convertView.findViewById(R.id.btn_sat);
final Button btn_sun = (Button)convertView.findViewById(R.id.btn_sun);
btn_mon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_mon, weekdays.get(0).data); }});
btn_tus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_tus, weekdays.get(1).data); }});
btn_wen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_wen, weekdays.get(2).data); }});
btn_thu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_thu, weekdays.get(3).data); }});
btn_fri.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_fri, weekdays.get(4).data); }});
btn_sat.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_sat, weekdays.get(5).data); }});
btn_sun.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_sun, weekdays.get(6).data); }});
onCalBtnClick method:
private void onCalBtnClick(Button btn, int day){
Log.d("debug", String.valueOf(day));
btn.setTextColor(mContext.getResources().getColor(R.color.orange));
//selectedYear, month are global
Intent intent = new Intent();
intent.putExtra("year", selectedYear);
intent.putExtra("month", month);
intent.putExtra("day", day);
setResult(RESULT_OK, intent);
finish();
}
However, if I put Log.d into onCalBtnClick method (it is called from each clicklistener), only middle three buttons work. Two buttons from left side (btn_mon, btn_tus) and two buttons from right side (btn_sat, btn_sun) don't react on user click. Middle buttons work fine.
This is similar question Android LinearLayout make button doesn't work but my layout file corresponds to pattern in the answer there and buttons don't work nevertheless
UPDATE
When I removed fixed button height and width in layout file (from 50dp to wrap_content), all buttons started to work!
However, now it doesn't look as needed. There's space needed between text's on buttons.
And main question: WHY?
If you using weights in your layout, you are telling that it should be filled with some objects with some proportions. It just opposite to "wrap_content". With weights outer layout defines size of inner views, while "wrap_content" means that outer layout size is defined by inner views.
Please decide what approach is better in that case - removing weights or fixed inner views sizes.
set your Linear Layout's Width and Height to FILL_PARENT .it will work for sure.
Try to declare all your setOnClickListener for buttons outside of your adapter class and after initialization of weekdays array.
That will solve your problem.
Or try to set condition like this for all buttons :
if (btn_mon != null && position < weekdays.size()) {
btn_mon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_mon, weekdays.get(0).data); }});
}