in this program, I want to show on screen radio buttons with rooms name
buttonList = new RadioButton[roomNames.length];
btGroup = (RadioGroup) findViewById(R.id.roomList);
RadioGroup.LayoutParams rprms;
for(int i = 0; i < roomNames.length; i++)
{
//buttonList[i] = new RadioButton(this);
RadioButton tempBT = new RadioButton(this);
System.out.println("room name number :"+ i + roomNames[i]);
rprms= new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
//tempBT.setText(roomNames[i].subSequence(0,roomNames[i].length()));
//tempBT.setTextColor(0);
tempBT.setId(i);
buttonList[i] = tempBT;
btGroup.addView(tempBT, rprms);
//buttonList[i].setText(roomNames[i].subSequence(0,roomNames[i].length()));
//buttonList[i].setTextColor(0);
//btGroup.addView(buttonList[i], rprms);
}
for (int f = 0; f < btGroup.getChildCount(); f++) {
((RadioButton) btGroup.getChildAt(f)).setText(roomNames[f].subSequence(0,roomNames[f].length()));
((RadioButton) btGroup.getChildAt(f)).setTextColor(0);
}
//saving the chosen room name
btGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup arg0, int selectedId) {
for(int j = 0; j < buttonList.length; j++)
{
if(btGroup.getCheckedRadioButtonId() == buttonList[j].getId())
{
roomNameSelected = roomNames[j];
}
}
}
});
but when I run this program I see the radio buttons but there is no text inside
Instead of
setTextColor(0);
Try
setTextColor(Color.BLACK);
Related
Here is codes, and i am trying to receive a feedback from imageviews which are overlapped. One of the imageviews return isPressed method true, but the other one return me false. They are overlapped, so when i clicked one of them, both of their isPressed method should be true, i guess. Where am i wrong
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();
ImageView[] imageViews;
Button button;
int a = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridLayout gridLayout = findViewById(R.id.grid_layout);
GridLayout.LayoutParams[] layoutParams = new GridLayout.LayoutParams[9];
imageViews = new ImageView[18];
button = findViewById(R.id.button);
for (int i = 0, j = 0, k = -1, m = 0; j < 3 & k < 3 & m < imageViews.length || i < layoutParams.length; m++) {
if (m == 18) {
break;
}
if (m == 0) {
k++;
}
layoutParams[i] = new GridLayout.LayoutParams();
layoutParams[i].setGravity(Gravity.CENTER);
layoutParams[i].setMargins(25, 25, 25, 25);
layoutParams[i].columnSpec = GridLayout.spec(k, GridLayout.CENTER);
layoutParams[i].rowSpec = GridLayout.spec(j, GridLayout.CENTER);
imageViews[m] = new ImageView(MainActivity.this);
imageViews[m].setLayoutParams(layoutParams[i]);
gridLayout.addView(imageViews[m]); //---
if (m % 2 == 0) {
imageViews[m].setImageResource(R.drawable.first_image);
imageViews[m].setAlpha(1f);
} else {
imageViews[m].setImageResource(R.drawable.second_image);
imageViews[m].setAlpha(0.5f);
}
if (m % 2 == 1 & m != 0) {
j++;
i++;
}
if (m % 6 == 5 & m != 0) {
k++;
}
if (j == 3) {
j = 0;
}
}
actionView();
}
public void actionView() {
for (int m = 0; m < imageViews.length; m++) {
imageViews[m].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, " " + a + " " + "\n", Toast.LENGTH_SHORT).show();
Log.e(TAG, imageViews[1].isPressed() + "");
Log.e(TAG, imageViews[0].isPressed() + "");
Log.e(TAG, imageViews[1].isEnabled() + "");
Log.e(TAG, imageViews[0].isEnabled() + "");
Log.e(TAG, "--------------------");
a++;
for (int x = 0; x < imageViews.length; x++) {
if (imageViews[x].isPressed()) {
Log.e(TAG, x + " Image is clicked");
Log.e(TAG, " *****************");
}
}
}
});
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = 0;
}
});
}
}
If the views are overlapped and click is enabled for both only the view on top will receive the click event.
If you want to call on click event of both, call performClick of the view in background form the onclick listener of the view in front.
Like imageviewInBack.performClick()
In my app have twenty edit text,but I want to count filled edit text and that data goes in anther activity through an array. Like when I filled 3 edit text from twenty, that 3 edit text data goes next page and that 3 count goes to next page as an int.
this is my 1st java class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
mspin=(Spinner) findViewById(R.id.spinner1);
submit = (Button) findViewById(R.id.btn1);
layout = (LinearLayout) findViewById(R.id.linear);
lay = (LinearLayout) findViewById(R.id.li);
edt=(EditText) findViewById(R.id.ed2);
sc = (ScrollView) findViewById(R.id.sc1);
int no = 20;
allEds = new ArrayList<EditText>();
for (int i=1;i<=no;i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
edtAdd = new EditText(SelectActivity.this);
layout.setLayoutParams(params);
allEds.add(edtAdd);
edtAdd.setHint("Enter Name" + i);
edtAdd.setId(i);
layout.addView(edtAdd);
}
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (allEds.size()<=9) {
Intent data = new Intent(SelectActivity.this, HalfPieChartActivity.class);
String[] items = new String[allEds.size()];
String str = String.valueOf(allEds.size());
data.putExtra("size", str); //you don't need to keep this in loop as its same.
data.putExtra("edt", edt.getText().toString());
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData" + j, items[j]);
}
startActivity(data);
}
else {
Intent data = new Intent(SelectActivity.this, FullPieChartActivity.class);
String[] items = new String[allEds.size()];
String str = String.valueOf(allEds.size());
data.putExtra("size", str);// this is the line where I sent that count
data.putExtra("edt", edt.getText().toString());
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData" + j, items[j]);//here is filled data send line
}
startActivity(data);
}
}
});
}
}
When I click submit 20 show in next page as an int.I want to sent that 3data and 3 as an int.
Please help me
As you have asked for sample in comment, i am posting my answer with sample.
In onclicklistener of your submit button you can add following code.
myNoneEmptyEdittextCounter = 0;
for (int i=1;i<=no;i++)
{
myEt = (EditText) findViewById(i);
if(!TextUtils.isEmpty(myEt.getText().toString()))
{
myNoneEmptyEdittextCounter +=1;
}
}
//myNoneEmptyEdittextCounter is count of your filled edittexts
This method will return list of data and its size will give you the int you are looking for which is number of filled items.
In submit button click
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(edittextValues().isEmpty()) {
//nothing has been filled
} else {
//items in edittextValues() are your data.
edittextValues().size();//this is your int which is number of edit text filled.
}
}
}
I have mentioned in comments what each step does. Hope this helps.
Method edittextValues()
ArrayList<String> edittextValues() {
ArrayList<String> filledData = new ArrayList<>(); //initialise empty string array.
for (int i=1; i<=20; i++){ //loop through ids from 1 to 20
for (int j = 0; j< layout.getChildCount(); j++) { //loop through all the children of root layout
if(layout.getChildAt(j) instanceof EditText) { //filter EditText
EditText editText = (EditText) layout.getChildAt(j);
if(editText.getId() == i) { // filter the one which u programmatically added
if(!editText.getText().toString().isEmpty()) { // check if its not empty, that's the one you are looking
filledData.add(editText.getText().toString()); //add it to the list
}
}
}
}
}
return filledData; //return string list
}
I am working on my project where I want to show when application starts then calendar display, which date contain events, for instance if the date contain events, then the day button contains * symbol and day, And if the date doesn't contain any event then it only displays a day.
I wrote following code, but it only displays * symbol when I am clicking on that button, So how can I manage this code that display * symbol on the date which only contain events when the application starts or that page gonna be loaded.
Following is my code:-
public class Customised extends Calendar{
ArrayList<String[]> data = new ArrayList<>();
int i,j,columns;
#Override
protected void updateButtonDayDate(Button dayButton,int currentMonth, int day) {
dayButton.setText(""+day);
dayButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
//Check which date having how many number of events===============================================================
try{
ShowEvent.removeAll();
cur = db.executeQuery("SELECT Event, Description from CalendarData WHERE Date = ? ", dateLabel.getText());
columns = cur.getColumnCount();
if(columns > 0) {
boolean next = cur.next();
if(next) {
String[] columnNames = new String[columns];
for(int iter = 0 ; iter < columns ; iter++) {
columnNames[iter] = cur.getColumnName(iter);
}
while(next) {
Row currentRow = cur.getRow();
String[] currentRowArray = new String[columns];
for(int iter = 0 ; iter < columns ; iter++) {
currentRowArray[iter] = currentRow.getString(iter);
}
data.add(currentRowArray);
next = cur.next();
}
Object[][] arr = new Object[data.size()][];
data.toArray(arr);
}
}
}catch(IOException e){
e.printStackTrace();
}
for(i = 0 ; i< data.size(); i++){
Log.p(data.get(i)[0]);
}
Label a = new Label(dateLabel.getText());
Label b = new Label(" "+i);
Container container1 = TableLayout.encloseIn(2, a,b);
container1.setUIID("container1");
ShowEvent.add(container1);
for( i = 0 ; i< data.size(); i++){
for(j = 0; j<columns; j++){
Log.p(data.get(i)[j]);
SpanLabel spanData = new SpanLabel(data.get(i)[j]);
spanData.setUIID("SpanLabel");
ShowEvent.add(spanData);
}
Label space = new Label("=======================");
ShowEvent.add(space);
Log.p("###################");
}
data.clear();
if(i>0){
if(Dialog.show("Choose action", "What you want to do?", "Add Events","View Events")){
calendar.show();
}
else{
ShowEvent.show();
}
}else{
Dialog.show("Add event","There is no event to display, Please add events first","OK","");
}
//============================================================================================================
}
});
}
#Override
protected void initComponent(){
ArrayList<String[]> data1 = new ArrayList<>();
int k;
Log.p("initComponent");
try{
cur = db.executeQuery("select Date from CalendarData");
columns = cur.getColumnCount();
if(columns > 0) {
boolean next = cur.next();
if(next) {
String[] columnNames = new String[columns];
for(int iter = 0 ; iter < columns ; iter++) {
columnNames[iter] = cur.getColumnName(iter);
}
while(next) {
Row currentRow = cur.getRow();
String[] currentRowArray = new String[columns];
for(int iter = 0 ; iter < columns ; iter++) {
currentRowArray[iter] = currentRow.getString(iter);
}
data1.add(currentRowArray);
next = cur.next();
}
Object[][] arr = new Object[data1.size()][];
data1.toArray(arr);
}
}
}catch(IOException e){
e.printStackTrace();
}
for(k = 0 ; k< data1.size(); k++){
Log.p(data1.get(k)[0]);
}
if(k>0){
//cal.setUIID("CalendarSelectedDay");
}
}
/*
#Override
protected boolean isInitialized(){
boolean result = false;
Log.p("isInitialised");
return result;
}*/
public Customised(){
}
#Override
protected Button createDay() {
Button day = new Button();
day.setAlignment(CENTER);
day.setUIID("CalendarDay1");
day.setEndsWith3Points(false);
day.setTickerEnabled(false);
return day;
}
}
And the expected result will be:-
That's because you placed the code inside the actionPerformed method which is only triggered upon Button pressed/released.
Move your code to the updateButtonDayDate scope
I'm trying to add all the values from different arrays to one 2d array and display it as a table in JavaFX.
Here's my try in doing it.
private StackPane table(Stage stage) {
StackPane root = new StackPane();
String[][] staffArray = new String[365][365];
staffArray[0][0] = "No. ";
staffArray[0][1] = "Check in";
staffArray[0][2] = "Check out";
staffArray[0][3] = "Name";
staffArray[0][4] = "Surname";
System.out.println(String.valueOf(arrRoom1[0][0]));
for (int i = 0; i < arrRoom1.length; i++) {
for (int y = 1; y < arrRoom1.length; y++) {
if (arrRoom1[i] == null) {
break;
}
staffArray[y][0] = String.valueOf(y);
staffArray[y][1] = String.valueOf(arrRoom1[i][0]);
staffArray[y][2] = String.valueOf(arrRoom1[i][1]);
staffArray[y][3] = String.valueOf(names1[i]);
staffArray[y][4] = String.valueOf(surnames1[i]);
}
}
System.out.println(staffArray[1][1]);
ObservableList<String[]> data = FXCollections.observableArrayList();
data.addAll(Arrays.asList(staffArray));
data.remove(0);//remove titles from data
TableView<String[]> table = new TableView<>();
for (int i = 1; i < staffArray.length; i++) {
for (int y = 0; y < 5; y++) {
if ( staffArray[i][y] == null) {
break;
}
TableColumn tc = new TableColumn(staffArray[i][y]);
final int colNo = i;
tc.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<String[], String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<String[], String> p) {
return new SimpleStringProperty((p.getValue()[colNo]));
}
});
tc.setPrefWidth(90);
table.getColumns().add(tc);
}
}
table.setItems(data);
root.getChildren().add(table);
return root;
}
However, every time I get null values in staffArray after the loop. Where might the mistake be in the nested loop?
I have alertdialog with listview and listview header with "check all" and "uncheck all" button and listview footer with "Done" button. Listview is CHOICE_MODE_MULTIPLE with arrayList value EavesList. EavesList will be cross checked with another arrayList selectedItems to show those items as checked. the issue i am facing is Listview shows correct value but shows previous item as checked.
Here's my code:
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setTitle("Select Category List");
final ListView modeList = new ListView(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View rootView = inflater.inflate(
R.layout.listview_header_eavesdrop, null);
modeList.addHeaderView(rootView);
Button btnDone = new Button(getActivity());
btnDone.setText("Done");
btnDone.setTextColor(getActivity().getResources().getColor(
android.R.color.white));
final ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_multiple_choice,
android.R.id.text1, EavesList);
modeList.addFooterView(btnDone);
modeList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
modeList.setAdapter(modeAdapter);
if (selectedItems.size() != 0) {
for (int i = 0; i < EavesList.size(); i++) {
if (selectedItems.size() != 0) {
// modeList.setItemChecked(2, true);
for (int j = 0; j < selectedItems.size(); j++) {
if (EavesList.get(i).contains(
selectedItems.get(j))) {
modeList.setItemChecked(i, true);
Log.e("Selected Item ",
"there Values true "
+ selectedItems.get(j));
j = selectedItems.size() + 2;
}
}
} else {
Log.e("Selected Item ", "No Values");
}
}
} else {
for (int i = 0; i < EavesList.size(); i++) {
if (category.size() != 0) {
for (int j = 0; j < category.size(); j++) {
if (EavesList_id.get(i).contains(category.get(j))) {
modeList.setItemChecked(i, true);
Log.e("List " + EavesList_id.get(i), ""+ category.get(j));
Log.e("Selected category Item ",
"there Values true "
+ category.get(j));
} else {
if (j == category.size()) {
modeList.setItemChecked(i, false);
Log.e("Selected category Item ",
"there Values false "
+ category.get(j));
}
}
}
} else {
Log.e("Selected Item ", "No Values");
}
}
}
builder.setView(modeList);
final Dialog dialog = builder.create();
btnDone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
selectedItems.clear();
eavesdropping.setText("");
SparseBooleanArray checked = modeList
.getCheckedItemPositions();
selected_community_id = new String[checked.size()];
String[] evas_id;
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
selectedItems.add(EavesList.get(position));
selected_community_id[i] = EavesList_id
.get(position);
}
for (int i = 0; i < selected_community_id.length; i++) {
eavesdropping_hide.append(selected_community_id[i]);
if (i != selected_community_id.length ) {
eavesdropping_hide.append(",");
}
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
eavesdropping.append(outputStrArr[i]);
eavesdropping.append(" | ");
}
dialog.dismiss();
}
});
dialog.show();
P.S: Listview shows proper content but the checked item of listview is always one previous item is always checked and the item to be checked is unchecked.
Why don't try this method on your submit button click
SparseBooleanArray sparseBooleanArray = list.getCheckedItemPositions();
List<String> lstCheck= new ArrayList<String>();
for (int i = 0; i < list.getCount(); i++) {
if (sparseBooleanArray.get(i) == true) {
lstCheck.add(list.getItemAtPosition(i).toString());
}
}
Now as lstCheck will result in only the checked values compare it with your EavesList and find those items that do not exist and you will also have you non-selected values
Use the following: (it is pseudo code):
ArrayList<String> data;
ArrayList<String> checkboxes;
.....
listview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
if(chockBoxes.containds(data.get(position))){
chockBoxes.remove(data.get(position));
}else{
chockBoxes.add(data.get(position))
}
}
});
...
now in your adapter getView():
if(checkBoxes.contains(data.get(position))){
checkBox.setChecked(true);
}else{
checkBox.setChecked(false);
}