View.setBackgroundResource() is not setting the correct background resource - java

I am coding a "set pin" fragment for the second time and it's not working. Everything works great until I use switch blocks and View.OnTouchListener. I am trying to change the buttons background resource to a different drawable when pressed down and then back when finger is lifted.
My problem is that the background resource is not changing at all.
XML for layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/tertiary_dark"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#style/Theme.Design.NoActionBar"
>
<View
android:id="#+id/divider3"
android:layout_width="364.059dp"
android:layout_height="2dp"
android:background="#color/secondary_accent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tableLayout" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="#+id/pin7"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:background="#drawable/button_1_background_idle"
android:text="7"
android:textColor="#color/primary_light"
android:textSize="22sp" />
<Button
android:id="#+id/pin8"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="8"
android:textColor="#color/primary_light"
android:textSize="22sp" />
<Button
android:id="#+id/pin9"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="9"
android:textColor="#color/primary_light"
android:textSize="22sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/pin4"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="4"
android:textColor="#color/primary_light"
android:textSize="22sp" />
<Button
android:id="#+id/pin5"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="5"
android:textColor="#color/primary_light"
android:textSize="22sp" />
<Button
android:id="#+id/pin6"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="6"
android:textColor="#color/primary_light"
android:textSize="22sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/pin1"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="1"
android:textColor="#color/primary_light"
android:textSize="22sp" />
<Button
android:id="#+id/pin2"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="2"
android:textColor="#color/primary_light"
android:textSize="22sp" />
<Button
android:id="#+id/pin3"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="3"
android:textColor="#color/primary_light"
android:textSize="22sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/pina"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="A"
android:textColor="#color/primary_light"
android:textSize="22sp" />
<Button
android:id="#+id/pin0"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="0"
android:textColor="#color/primary_light"
android:textSize="22sp" />
<Button
android:id="#+id/pinb"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="B"
android:textColor="#color/primary_light"
android:textSize="22sp" />
</TableRow>
</TableLayout>
<View
android:id="#+id/view"
android:layout_width="364.059dp"
android:layout_height="75dp"
android:layout_marginTop="104dp"
android:background="#color/primary_dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.489"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/slatebook"
android:text="1234567890ab"
android:textColor="#color/primary_light"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="#+id/view"
app:layout_constraintEnd_toEndOf="#+id/view"
app:layout_constraintStart_toStartOf="#+id/view"
app:layout_constraintTop_toTopOf="#+id/view" />
<View
android:id="#+id/divider"
android:layout_width="364.059dp"
android:layout_height="2dp"
android:background="#color/secondary_accent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code for java class:
package com.example.portal;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.system.StructUtsname;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import java.util.ArrayList;
public class FirstFragment extends Fragment {
#Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.login_layout, container, false);
}
#SuppressLint("ClickableViewAccessibility")
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
load();
}
#SuppressLint("ClickableViewAccessibility")
private void load() {
ArrayList<Button> pinbtns = new ArrayList<>();
ArrayList<Character> vals = new ArrayList<>();
pinbtns.add((Button) getActivity().findViewById(R.id.pin0));
pinbtns.add((Button) getActivity().findViewById(R.id.pin1));
pinbtns.add((Button) getActivity().findViewById(R.id.pin2));
pinbtns.add((Button) getActivity().findViewById(R.id.pin3));
pinbtns.add((Button) getActivity().findViewById(R.id.pin4));
pinbtns.add((Button) getActivity().findViewById(R.id.pin5));
pinbtns.add((Button) getActivity().findViewById(R.id.pin6));
pinbtns.add((Button) getActivity().findViewById(R.id.pin7));
pinbtns.add((Button) getActivity().findViewById(R.id.pin8));
pinbtns.add((Button) getActivity().findViewById(R.id.pin9));
pinbtns.add((Button) getActivity().findViewById(R.id.pina));
pinbtns.add((Button) getActivity().findViewById(R.id.pinb));
vals.add('0');
vals.add('1');
vals.add('2');
vals.add('3');
vals.add('4');
vals.add('5');
vals.add('6');
vals.add('7');
vals.add('8');
vals.add('9');
vals.add('a');
vals.add('b');
for (int r = 0; r < pinbtns.size(); r++) {
Button btn = pinbtns.get(r);
btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
btn.setBackgroundResource(R.drawable.button_1_background_pressed);
System.out.println("down");
return true;
case MotionEvent.ACTION_UP:
Thread t = new Thread(() -> {
try {
Thread.sleep(50);
} catch (Exception ig) {
ig.printStackTrace();
}
getActivity().runOnUiThread(() -> {
btn.setBackgroundResource(R.drawable.button_1_background_idle);
});
});
t.start();
System.out.println("up");
return true;
}
return false;
}
});
}
}
}
Code for drawables:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/primary_dark"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/primary_accent"/>
</shape>
And the color resourcefile for good measure.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary_light">#dcdcdc </color>
<color name="primary_dark">#1c1d1f</color>
<color name="secondary_dark">#151515</color>
<color name="tertiary_dark">#323335</color>
<color name="secondary_light">#9ea09f</color>
<color name="primary_accent">#c41442</color>
<color name="secondary_accent">#711930</color>
</resources>
Just to clarify one more time. When I press a button (for example pin7) the system prints "down" to the console but the background does not change one bit. When I release I get "up" in the console. So it works as intended except for the background.
I have tried
changing the setBacgroundResource to setBackroundColor and same outcome.

I have spent 2 hours and am trying to find out why the setBackgroundResource() is not working. I added some modification on the shape and I noticed that the change only made on the dimensions except the Color. So I created a style to eliminate the Tinte I put the backgroundTint on #null and everything works correctly.
here is the style I added
<style name="NotTintedButton" parent="Widget.AppCompat.Button.Colored">
<item name="backgroundTint">#null</item>
</style>
And all you have to do is apply this style on each buttons. for example :
<Button
style="#style/NotTintedButton"
android:id="#+id/pin8"
android:layout_width="#dimen/button_1_width"
android:layout_height="#dimen/button_1_height"
android:layout_margin="#dimen/button_1_margin"
android:background="#color/primary_dark"
android:fontFamily="#font/slatebook"
android:padding="0dp"
android:text="8"
android:textColor="#color/primary_light"
android:textSize="22sp" />

Related

How to get the data from firebase and display inside the spinner?

I would like to display all the registerEventName inside the spinner in order to let the user to select from it. But how to get the data from firebase and display inside the spinner? Hope someone can help me on this.
XML File
For example i want to change EditText of eventTitle to spinner.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/createEventLayout"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:weightSum="10"
android:background="#89bcd4"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="4"
android:text="Title of event"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/eventTitle"
android:layout_width="0dp"
android:inputType="text"
android:maxLines="1"
android:lines="1"
android:layout_weight="6"
android:imeOptions="actionNext"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:weightSum="10"
android:background="#dce2e6"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="4"
android:text="#string/createEventDesc"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/eventDes"
android:layout_width="0dp"
android:layout_weight="6"
android:lines="1"
android:inputType="text"
android:maxLines="1"
android:imeOptions="actionNext"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:weightSum="10"
android:background="#89bcd4"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="4"
android:text="#string/createEventLocation"
android:layout_height="wrap_content" />
<EditText
android:maxLines="1"
android:lines="1"
android:id="#+id/eventLocation"
android:layout_width="0dp"
android:layout_weight="6"
android:inputType="text"
android:imeOptions="actionNext"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:weightSum="10"
android:background="#dce2e6"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="4"
android:text="Employee email"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/eventAttendee"
android:layout_width="0dp"
android:layout_weight="6"
android:hint="Place more attendee by comma seprated"
android:maxLines="1"
android:lines="1"
android:inputType="textEmailAddress"
android:imeOptions="actionNext"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#89bcd4"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/startAt"
android:layout_width="0dp"
android:layout_weight="3"
android:text="#string/createEventStart"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="wrap_content">
<DatePicker
android:id="#+id/startDate"
android:layout_width="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:layout_weight="4"
android:layout_height="wrap_content" />
<TimePicker
android:id="#+id/startTime"
android:layout_width="wrap_content"
android:layout_weight="4"
android:timePickerMode="spinner"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#dce2e6"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/endAt"
android:layout_width="0dp"
android:layout_weight="3"
android:text="#string/createEventEnd"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="0dp"
android:orientation="vertical"
android:layout_weight="7"
android:layout_height="wrap_content">
<DatePicker
android:id="#+id/endDate"
android:layout_width="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:layout_height="wrap_content" />
<TimePicker
android:id="#+id/endTime"
android:timePickerMode="spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:weightSum="10"
android:background="#89bcd4"
android:padding="10dp"
android:layout_height="wrap_content">
<Button
android:maxLines="1"
android:id="#+id/createEvent"
android:layout_width="0dp"
android:layout_weight="5"
android:text="Create Event"
android:layout_height="wrap_content" />
<Button
android:id="#+id/cancelEvent"
android:layout_width="0dp"
android:layout_weight="5"
android:text="Cancel"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Event List Adapter Java File
package com.example.edward.neweventmanagementsystem.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import com.example.edward.neweventmanagementsystem.R;
import com.example.edward.neweventmanagementsystem.Model.ScheduledEvents;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Khushvinders on 21-Oct-16.
*/
public class EventListAdapter extends BaseAdapter {
private Context context;
private List<ScheduledEvents> scheduledEvents;
private LayoutInflater inflater;
FirebaseDatabase database;
public EventListAdapter(Context context, List<ScheduledEvents> scheduledEvents){
this.context = context;
this.scheduledEvents = scheduledEvents;
inflater = LayoutInflater.from(this.context);
}
#Override
public int getCount() {
return scheduledEvents.size();
}
#Override
public Object getItem(int i) {
return scheduledEvents.get(i);
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
EventHolder eventHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.event_view_layout, parent, false);
eventHolder = new EventHolder(convertView);
convertView.setTag(eventHolder);
} else {
eventHolder = (EventHolder) convertView.getTag();
}
ScheduledEvents scheduledEvents = (ScheduledEvents) getItem(position);
eventHolder.eventTitle.setText(scheduledEvents.getEventSummery());
eventHolder.eventDes.setText(scheduledEvents.getDescription());
eventHolder.eventAttendee.setText(scheduledEvents.getAttendees());
eventHolder.eventStart.setText(scheduledEvents.getStartDate());
eventHolder.eventEnd.setText(scheduledEvents.getEndDate());
eventHolder.eventLocation.setText(scheduledEvents.getLocation());
return convertView;
}
private class EventHolder {
TextView eventDes, eventAttendee, eventStart, eventEnd, eventLocation, eventTitle;
public EventHolder(View item) {
eventTitle = (TextView) item.findViewById(R.id.eventTitle);
eventDes = (TextView) item.findViewById(R.id.eventDes);
eventAttendee = (TextView) item.findViewById(R.id.eventAttendee);
eventStart = (TextView) item.findViewById(R.id.eventStart);
eventEnd = (TextView) item.findViewById(R.id.eventEnd);
eventLocation = (TextView) item.findViewById(R.id.eventLocation);
}
}
}
Sample of firebase database

Android Studio: Unfortunately, app has stopped

My android calculator app seems to be failing at the moment, can anyone please fix it? I searched everywhere, but none of them seem to be working.
AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.koki.customizablecalculator"
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java file:
package com.example.koki.customizablecalculator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText Scr;
private float NumberBf;
private String Operation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Scr = (EditText) findViewById(R.id.button0);
int idList[] = {R.id.button0,R.id.button2,R.id.button3,R.id.button4,R.id.button5,
R.id.button6,R.id.button7,R.id.button8,R.id.buttonDot,R.id.buttonMul,
R.id.buttonDiv,R.id.buttonAdd,R.id.buttonSub,
};
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
content_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button"
android:layout_marginTop="83dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/button3"
android:layout_alignTop="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/button4"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/button5"
android:layout_alignTop="#+id/button4"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/button6"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="#+id/button7"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="#+id/button8"
android:layout_alignTop="#+id/button7"
android:layout_alignLeft="#+id/button5"
android:layout_alignStart="#+id/button5" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AC"
android:id="#+id/buttonC"
android:background="#FF0000"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/buttonSub"
android:layout_toStartOf="#+id/buttonSub" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:id="#+id/settings"
android:background="#10E0F8"
android:allowUndo="false"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/buttonAdd"
android:layout_toEndOf="#+id/buttonAdd" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/button0"
android:layout_below="#+id/button8"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/buttonAdd"
android:layout_marginBottom="38dp"
android:background="#FE9A2E"
android:layout_above="#+id/buttonC"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/buttonSub"
android:background="#FE9A2E"
android:layout_alignTop="#+id/buttonAdd"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="#+id/buttonDiv"
android:background="#FE9A2E"
android:layout_alignTop="#+id/buttonMul"
android:layout_toLeftOf="#+id/button0"
android:layout_toStartOf="#+id/button0" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="#+id/button9"
android:layout_below="#+id/button6"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:id="#+id/buttonMul"
android:background="#FE9A2E"
android:layout_alignTop="#+id/buttonSub"
android:layout_toLeftOf="#+id/buttonC"
android:layout_toStartOf="#+id/buttonC" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="#+id/buttonDot"
android:layout_below="#+id/button9"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/button0"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="#+id/buttonEq"
android:layout_below="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#1B9863" />
</RelativeLayout>
Any help would be greatly appreciated! :)
You are calling an EditText that you don't have.
Either in your XML change your TextView to an EditText
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/button0"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
OR Change
private EditText Scr;
To
private TextView Scr;
And
Scr = (EditText) findViewById(R.id.button0);
To
Scr = (TextView) findViewById(R.id.button0);

How to disable interaction between buttons of the same layout?

I have two sets of layouts, populated with buttons. Is there a way to disable any interaction between buttons of the same layout? I just need nothing to happen when I click one button and then the other from the same layout. I tagged each button with " and ", for buttons that belongs to upper and lower layouts. I have 5 of each kind.
I have some interaction beetwen buttons of oposite layouts and that's working fine (actually interaction works fine between any two given buttons and that's bad), but I don't want that interaction between buttons of the same layout.
Here's my game class, not all of it, just important parts. Also, I've removed code for rest of the buttons, only left for 4 buttons, 2 for each layout, to save some space here. I import some text from sqlite database, and randomly set it to my buttons.
final OnClickListener clickListener = new OnClickListener() {
private Button buttonClicked;
public void onClick(View v) {
Button button = (Button) v;
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));
if (buttonClicked == null) {
// first button is clicked
buttonClicked = button;
} else {
// second button is clicked
if (buttonClicked.getTag().equals(button.getTag())) {
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
buttonClicked.setEnabled(false);
button.setEnabled(false);
} else {
buttonClicked.setEnabled(false);
buttonClicked.setTextColor(Color.GRAY);
buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFCC99));
button.getBackground().clearColorFilter();
}
buttonClicked = null;
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
a1 = (Button) findViewById(R.id.bA1);
a2 = (Button) findViewById(R.id.bA2);
b1 = (Button) findViewById(R.id.bB1);
b2 = (Button) findViewById(R.id.bB2);
nextQuestion();
}
public void nextQuestion() {
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
try{
mDbHelper.open();
Cursor c = mDbHelper.getTestData(generateWhereClause());
mAnsweredQuestions.add(c.getLong(0));
ArrayList<MyStruct> labelsA = new ArrayList<MyStruct>();
ArrayList<MyStruct> labelsB = new ArrayList<MyStruct>();
labelsA.add(new MyStruct(c.getString(2), "1")); // this tag should be the same to button that matches
labelsB.add(new MyStruct(c.getString(3), "1"));
labelsA.add(new MyStruct(c.getString(4), "2"));
labelsB.add(new MyStruct(c.getString(5), "2"));
Collections.shuffle(labelsA);
Collections.shuffle(labelsB);
pitanje.setText(c.getString(1));
a1.setText(labelsA.get(0).label);
a1.setTag(labelsA.get(0).tag);
a1.setOnClickListener(clickListener);
a1.getBackground().clearColorFilter();
a1.setEnabled(true);
b1.setText(labelsB.get(0).label);
b1.setTag(labelsB.get(0).tag);
b1.setOnClickListener(clickListener);
b1.getBackground().clearColorFilter();
b1.setEnabled(true);
a2.setText(labelsA.get(1).label);
a2.setTag(labelsA.get(1).tag);
a2.setOnClickListener(clickListener);
a2.getBackground().clearColorFilter();
a2.setEnabled(true);
b2.setText(labelsB.get(1).label);
b2.setTag(labelsB.get(1).tag);
b2.setOnClickListener(clickListener);
b2.getBackground().clearColorFilter();
b2.setEnabled(true);
}
finally{
mDbHelper.close();
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="top|center"
android:orientation="vertical" >
<TextView
android:id="#+id/tvPitanje"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ovde ce biti postavljeno pitanje"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/kolona1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="top"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="2dp" >
<Button
android:id="#+id/bA1"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:layout_gravity="clip_horizontal"
android:layout_marginBottom="1.5dp"
android:background="#drawable/buttons"
android:gravity="center_vertical|center_horizontal"
android:padding="0dp"
android:tag="l"
android:text="A1"
android:textColor="#ffffff"
android:textSize="16sp" />
<Button
android:id="#+id/bA2"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:text="A2"
android:background="#drawable/buttons"
android:textColor="#ffffff"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:tag="l"
android:padding="0dp"
android:textSize="16sp" />
<Button
android:id="#+id/bA3"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:text="A3"
android:background="#drawable/buttons"
android:textColor="#ffffff"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:tag="l"
android:padding="0dp"
android:textSize="16sp" />
<Button
android:id="#+id/bA4"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:text="A4"
android:background="#drawable/buttons"
android:textColor="#ffffff"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:tag="l"
android:padding="0dp"
android:textSize="16sp" />
<Button
android:id="#+id/bA5"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:text="A5"
android:background="#drawable/buttons"
android:textColor="#ffffff"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:tag="l"
android:padding="0dp"
android:textSize="16sp" />
<Button
android:id="#+id/bA6"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:text="A6"
android:background="#drawable/buttons"
android:textColor="#ffffff"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:tag="l"
android:padding="0dp"
android:textSize="16sp" />
<Button
android:id="#+id/bA7"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:text="A7"
android:background="#drawable/buttons"
android:textColor="#ffffff"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:tag="l"
android:padding="0dp"
android:textSize="16sp" />
<Button
android:id="#+id/bA8"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:text="A8"
android:background="#drawable/buttons"
android:textColor="#ffffff"
android:layout_marginTop="1.5dp"
android:tag="l"
android:padding="0dp"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/kolona2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="top"
android:orientation="vertical"
android:paddingLeft="2dp"
android:paddingRight="3dp" >
<Button
android:id="#+id/bB1"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:layout_gravity="top|fill_horizontal"
android:layout_marginBottom="1.5dp"
android:background="#drawable/buttons"
android:gravity="top|center_horizontal"
android:tag="r"
android:text="B1"
android:textColor="#ffffff"
android:textSize="16sp" />
<Button
android:id="#+id/bB2"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:background="#drawable/buttons"
android:gravity="top|center_horizontal"
android:tag="r"
android:text="B2"
android:textColor="#ffffff"
android:textSize="16sp" />
<Button
android:id="#+id/bB3"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:background="#drawable/buttons"
android:gravity="top|center_horizontal"
android:tag="r"
android:text="B3"
android:textColor="#ffffff"
android:textSize="16sp" />
<Button
android:id="#+id/bB4"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:background="#drawable/buttons"
android:gravity="top|center_horizontal"
android:tag="r"
android:text="B4"
android:textColor="#ffffff"
android:textSize="16sp" />
<Button
android:id="#+id/bB5"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:background="#drawable/buttons"
android:gravity="top|center_horizontal"
android:tag="r"
android:text="B5"
android:textColor="#ffffff"
android:textSize="16sp" />
<Button
android:id="#+id/bB6"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:background="#drawable/buttons"
android:gravity="top|center_horizontal"
android:tag="r"
android:text="B6"
android:textColor="#ffffff"
android:textSize="16sp" />
<Button
android:id="#+id/bB7"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:layout_marginBottom="1.5dp"
android:layout_marginTop="1.5dp"
android:background="#drawable/buttons"
android:gravity="top|center_horizontal"
android:tag="r"
android:text="B7"
android:textColor="#ffffff"
android:textSize="16sp" />
<Button
android:id="#+id/bB8"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:layout_marginTop="1.5dp"
android:background="#drawable/buttons"
android:gravity="top|center_horizontal"
android:tag="r"
android:text="B8"
android:textColor="#ffffff"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="3" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="#+id/bIzlaz"
android:layout_width="80dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:background="#drawable/buttons"
android:gravity="center"
android:text="Izlaz!"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/tvPoeni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tvScore"
android:textColor="#ffffff"
android:textSize="21sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<TextView
android:id="#+id/tvVreme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30"
android:textColor="#ff0000"
android:textSize="21sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
You can use the getParent method and compare the parents of both views. If the parent is the same, then the buttons are in the same layout and nothing should happen.
Change your else:
} else {
// second button is clicked
... to an else if:
// only do stuff if buttons are in different layouts
} else if (button.getParent () != buttonClicked.getParent()) {
// second button is clicked

How do you pass data from FragmentA to hostActivity then update fragmentB

I have three fragments, Player1Tun, Player2Turn and TicTacToeLayout. Player1Turn and Player2Turn are inner-switching every time a player makes a move and TicTacToeLayout contains the tic tac toe table which update everytime the fragments switches. What I'm trying to do is to send the data from either player fragments to the host activity (while the program is still running) and then from there, I'll update the table in the TicTacToeLayout. Any ideas or method I can do it?
Player1:
package As2.packageTK;
//import android.app.Activity;
import java.util.ArrayList;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class PlayerTurn2 extends Fragment{
TextView p2Name;
TextView p2Icon;
Button doneP2;
Button resetP2;
EditText row;
EditText column;
TicTacToeLayout myObject2 = new TicTacToeLayout();
ArrayList<String> player2;
Bundle extras = new Bundle();
int turn = 2;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.playerturn2, container, false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
extras = getArguments();
player2 = new ArrayList<String>(extras.getStringArrayList("player2"));
//Toast.makeText(getActivity(), player2.get(0), Toast.LENGTH_LONG).show();
//Toast.makeText(getActivity(), player2.get(1), Toast.LENGTH_LONG).show();
p2Name = (TextView) getActivity().findViewById(R.id.p2NameInfo);
p2Icon = (TextView) getActivity().findViewById(R.id.p2IconInfo);
row = (EditText) getActivity().findViewById(R.id.rowP2);
column = (EditText) getActivity().findViewById(R.id.columnP2);
doneP2 = (Button) getActivity().findViewById(R.id.doneP2);
//resetP2 = (Button) getActivity().findViewById(R.id.resetP2);
setPlayer(); //sets all the information of player 2, name, icon, image, etc
doneP2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callPlayer1Fragment(); //switches with first player
}
});
}
public void callPlayer1Fragment()
{
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment Player1Frag = new PlayerTurn1();
Player1Frag.setArguments(extras);
ft.replace(R.id.fragment_container, Player1Frag);
ft.commit();
}
public boolean checkField()
{
if(row == null || column == null)
{
Toast.makeText(getActivity(), "Please input the row or column!", Toast.LENGTH_LONG).show();
return false;
}
else
return true;
}
public void setPlayer()
{
String name = player2.get(0);
if(!name.equals(""))
p2Name.setText("Player Name: " + name);
else
p2Name.setText("");
String icon = player2.get(1);
if(!icon.equals(""))
p2Icon.setText("Player Icon: " + icon);
else
p2Icon.setText("");
}
}
player 2 fragment is exactly the same code so I won't bother adding it.
TicTacToeLayout fragment class:
public class TicTacToeLayout extends Fragment {
TextView image1, image2, image3, image4, image5, image6, image7, image8, image9;
TextView[][] images;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tictactoe_layout, container, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
image1 = (TextView) getActivity().findViewById(R.id.Image1);
image2 = (TextView) getActivity().findViewById(R.id.Image2);
image3 = (TextView) getActivity().findViewById(R.id.Image3);
image4 = (TextView) getActivity().findViewById(R.id.Image4);
image5 = (TextView) getActivity().findViewById(R.id.Image5);
image6 = (TextView) getActivity().findViewById(R.id.Image6);
image7 = (TextView) getActivity().findViewById(R.id.Image7);
image8 = (TextView) getActivity().findViewById(R.id.Image8);
image9 = (TextView) getActivity().findViewById(R.id.Image9);
images = new TextView[][]{ {image1, image2, image3},
{image4, image5, image6},
{image7, image8, image9} };
toast();
}
public void toast()
{
Toast.makeText(getActivity(), images[0][0].getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), images[0][1].getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), images[0][2].getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), images[1][0].getText().toString(), Toast.LENGTH_LONG).show();
}
public void play(int row, int column, String icon)
{
images[row-1][column-1].setText(icon);
}
}
here's the XML layout for playerturn2
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="#+id/tablerow0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/player2Num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="100dp"
android:gravity="center_horizontal"
android:text="Player 2"
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="#+id/tablerow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/p2NameInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player Name: "
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="#+id/tablerow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/p2IconInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player icon: "
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="#+id/tablerow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/p2PicInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player picture:"
android:textSize="20dp" />
</TableRow>
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#android:drawable/editbox_background" />
<TableRow
android:id="#+id/rowNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="50dp"
android:gravity="center_horizontal"
android:text="Row (1-3):"
android:textSize="20dp" />
<EditText
android:id="#+id/rowP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:width="50dp" />
</TableRow>
<TableRow
android:id="#+id/colNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="50dp"
android:gravity="center_horizontal"
android:text="Col (1-3):"
android:textSize="20dp" />
<EditText
android:id="#+id/columnP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:width="50dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
<Button
android:id="#+id/doneP2"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="Done"
/>
<Button
android:id="#+id/resetP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
/>
</TableRow>
</TableLayout>
here's the layout for tictactoelayout.xml
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tictactoe"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" >
<TextView
android:id="#+id/Image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="1"
android:textSize="70dp" />
<TextView
android:id="#+id/Image2"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="2"
android:textSize="70dp" />
<TextView
android:id="#+id/Image3"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="3"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/Image4"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="4"
android:textSize="70dp" />
<TextView
android:id="#+id/Image5"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="5"
android:textSize="70dp" />
<TextView
android:id="#+id/Image6"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="6"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/Image7"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="7"
android:textSize="70dp" />
<TextView
android:id="#+id/Image8"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="8"
android:textSize="70dp" />
<TextView
android:id="#+id/Image9"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="9"
android:textSize="70dp" />
</TableRow>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/win"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Winner"
android:textSize="20dp" />
<EditText
android:id="#+id/winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:ems="10" />
</RelativeLayout>
</TableLayout>
and finally the host activity's xml layout: tictactoegame.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/frag2"
android:name="As2.packageTK.TicTacToeLayout"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
My friend did the same but with a implementing an interface he created in the fragments and having the main activity implement the interface. With that, he used a method to pass datas. Maybe it's not clear but if you know what I'm trying to say then you could clear it out for all of us :)
your friend is right, communicating with other fragments is nicely explained in the docs:
http://developer.android.com/training/basics/fragments/communicating.html

Clickable TableRow on Android

I have a simple program i have made in java for android but am having difficulty in making the tablerow clickable. When the user clicks a row i wish for a certain picture to be displayed in the imageview below. Here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF909090"
android:stretchColumns="1" >
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="I" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="A"
android:onClick="Achord" />
</TableRow>
<TableRow>
<TextView
android:layout_column="0"
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="II" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="Bm" />
</TableRow>
<TableRow>
<TextView
android:layout_column="0"
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="III" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="C#m" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow android:background="#0000ff" >
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="IV" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="D" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="V" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="E" />
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="VI" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="F#m" />
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="VII" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="G#dim" />
</TableRow>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:maxLines="10"
android:text="The relative minor of the Major key of A is F#m. The 12 bar blues progression in A is: \n A | A | A | A | D | D | A | A | E | D | A | E" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.03"
android:src="#drawable/android_focused" />
and here is my java code:
package com.coreservlets.widgets;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TableRow;
import com.welly.keychords.R;
public class keya extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keya);
}
}
what i want to happen is that imageview1 will display a different image depending on what row is clicked. Any help would be greatly appreciated
As sam suggested i have ammended my code to this:
public class keya extends Activity {
ImageView imagev = (ImageView) findViewById(R.id.imageView1);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keya);
}
public void rowClick(View view) {
switch(view.getId()) {
case R.id.one:
// Load image from Drawable folder
imagev.setImageResource(R.drawable.android_normal); // example file for now
break;
}
}
}
but now i have a null pointer exception on running that class. all compiles fine though
Simply give each TableRow element a unique id and define an onClick method:
<TableRow
android:id="#+id/one"
android:onClick="rowClick">
Add a class variable imageView that references the ImageView in your layout. Then load the image:
public void rowClick(View view) {
switch(view.getId()) {
case R.id.one:
// Load image from Drawable folder
imageView.setImageResource(R.id.imageOne);
break;
}
}
Understand that any elements inside a TableRow with their own onClick method will call its own method. For instance the TextView with android:onClick="Achord" will call Achord() not rowClick().
Addition
You need to call findViewById() after declaring your layout with setContentView() in onCreate().
ImageView imagev;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keya);
imagev = (ImageView) findViewById(R.id.imageView1);
}
Give each row an ID, then you can assign a listener to each one in your code:
TableRow row = (TableRow)findViewById( R.id.row1 );
row.setOnClickListener( new OnClickListener() {
#Override
public void onClick( View v ) {
//Do Stuff
}
} );
You need to call setOnClickListener() for each of your TableRow objects so that framework knows what to do when a row is clicked. In addition your Activity should implement View.OnClickListener() so that you can control what happens when a row gets clicked. Right now you aren't doing anything when the View gets clicked; at least according to your code you aren't registering a callback on any of the Views' onClickListeners
Assign id to Tablerow just like row1
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF909090"
android:stretchColumns="1" >
<TableRow
android:id="#+id/row1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="I" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="A"
android:onClick="Achord" />
</TableRow>
<TableRow>
<TextView
android:layout_column="0"
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="II" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="Bm" />
</TableRow>
<TableRow>
<TextView
android:layout_column="0"
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="III" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="C#m" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow android:background="#0000ff" >
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="IV" />
<TextView
android:layout_margin="2dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="D" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="V" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="E" />
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="VI" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="F#m" />
</TableRow>
<TableRow>
<TextView
android:layout_margin="2dip"
android:layout_marginRight="2dip"
android:background="#0000ff"
android:padding="3dip"
android:text="VII" />
<TextView
android:layout_margin="2dip"
android:layout_marginLeft="1dip"
android:background="#0000ff"
android:gravity="center"
android:padding="3dip"
android:text="G#dim" />
</TableRow>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:maxLines="10"
android:text="The relative minor of the Major key of A is F#m. The 12 bar blues progression in A is: \n A | A | A | A | D | D | A | A | E | D | A | E" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.03" />
</TableLayout>
Call it in activity like this.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TableRow;
import com.welly.keychords.R;
public class keya extends Activity {
TableRow row1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keya);
row1 = (TableRow) findViewById(R.id.row1);
row1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e("Click ", "Row 1");
}
});
}
}
i got it working by declaring the imageview variable within the public void rowclick
Assign an id to Tablerow in xml layout file:
<TableRow
android:id="#+id/rowL1"
...
</TableRow>
Add the following in your java file:
private TableRow rowL1;
//In onCreateView...
rowL1 = (TableRow) rootView.findViewById(R.id.rowL1);
rowL1.setOnClickListener(v -> {
//Your code here...
});

Categories