Change the background color of cell in table layout for showing selected and unselected on cell (text click event)clickevent in android - android-activity

Help me to change background color on text click event on each cell when i click other cell previous selected cell should be unselect means only one cell which is clicked should change the background color.In my code how many cell i clicked,all clicked cell are changing background.I Want single cell should change the color at a time .
where allValues is array list and i am creating 5 column table.
TableLayout tl = (TableLayout) findViewById(R.id.tlparent);
for (int j = 0, k = 0; j < allValues.size() / 5; j++)
{
final TableRow tableRow = new TableRow(this);
for (int y = 1; y <= 5; y++)
{
TextView tv = new TextView(this);
tv.setText(allValues.get(k));
tv.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
TextView tv = (TextView) v;
String text = tv.getText().toString();
v.setBackgroundColor(Color.GRAY);
}
});
tableRow.addView(tv);
k++;
}
tl.removeView(tableRow);
tl.addView(TABROW);
}

I found the answer of the question. first create the global text view with "null" value.where cell_shape is nothing just a border of text view.
Like public static TextView temp = null; then replace the text view click listener in above code only by the following.
ptv.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (temp != null)
{
temp.setBackgroundColor(Color.GREEN);
temp.setBackgroundResource(R.drawable.cell_shape);
temp = null;
}
if (temp == null) {
TextView ptv = (TextView) v;
String mdnStr =ptv.getText().toString().trim();
v.setBackgroundColor(Color.GRAY);
if (mdnStr.length()== 10) {
//do some thing whatever you want...
}
temp = ptv;
}
}
});

Related

How to add button in scroll View using script in unity

I am new to unity and am using unity 2020.3.26f1 . I am receiving an array of names from server. I want to make buttons dynamically using those names inside scroll view using c# script and assign on Click() that simply prints the name of button when its clicked. I just don't know how to create button dynamically. Following is the script I have attached to scroll View;
public string allNames;
void Start()
{
StartCoroutine(getNames());
}
IEnumerator getNames()
{
UnityWebRequest www = UnityWebRequest.Get("http://localhost:8080/names");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
allNames = www.downloadHandler.text;
allNames = allNames.Replace("[", "");
allNames = allNames.Replace("]", "");
allNames = allNames.Replace("\"","");
string[] separatedNames = allNames.Split(',');
for (int i = 0; i < separatedNames.Length; i++)
{
Debug.Log(separatedNames[i]);
//make buttons here and attach them to scroll View
}
}
}
I found a video that applied #ShafqatJamilKhan suggestion. Posting my code here for reference.
//content reference of scroll view
[SerializeField] Transform contentPanel;
[SerializeField] GameObject buttonPrefab;
void Start()
{
StartCoroutine(getNames());
}
IEnumerator getNames()
{
UnityWebRequest www = UnityWebRequest.Get("http://localhost:8080/names");
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
string allNames = www.downloadHandler.text;
allNames = allNames.Replace("[", "");
allNames = allNames.Replace("]", "");
allNames = allNames.Replace("\"","");
// prefab button y position and yPosition should be the same.
float yPosition = 115;
string[] separatedNames = allNames.Split(',');
for (int i = 0; i < separatedNames.Length; i++)
{
GameObject button = (GameObject)Instantiate(buttonPrefab);
string name = separatedNames[i];
button.GetComponentInChildren<Text>().text = name;
button.GetComponent<Button>().onClick.AddListener(
// your function whatever you want to do
() => { customFunction(name); });
// applying y positions so the buttons dont overlap
button.transform.SetPositionAndRotation(new Vector3(0.0f, yPosition, 0.0f), new Quaternion(0.0f, 0.0f, 0.0f,0.0f));
button.transform.SetParent(contentPanel,false);
yPosition -= 35;
}
}
}
Make a nice button and save it as prefab.
Instantiate it or Pool it.
Add onClick events.
GameObject buttonPrefab;
void MyAwesomeCreator()
{
GameObject go = Instantiate(buttonPrefab);
var button = GetComponent<UnityEngine.UI.Button>();
button.onClick.AddListener(() => FooOnClick());
}
void FooOnClick()
{
Debug.Log("Ta-Da!");
}
Copied from Unity Forum

Why Ag-grid cell renderer is getting re-initialized on horizontal scrolling (on moveable columns)?

While scrolling moveable columns cell renderer is getting re-initialized due to which the value in ag grid are getting reset. I need solution for this as I am not able to save row data as values gets reset on scrolling.
agInit(params): void {
this.params = params.data;
this.rowIndex = params.rowIndex;
this.selectedReason = params.data;
if (this.params.proposedCompletionDateInFormat) {
this.calender.selectedDateTime.startDate = new Date(params.data.proposedCompletionDateInFormat);
} else {
this.calender.selectedDateTime = null;
}
if(params.data.taskProgressState === "COMPLETED") {
this.disableField = true;
} else {
this.disableField = false;
}
}
refresh(params?: any): boolean {
this.formGroup = params.context.formGroup;
return true;
}
}
add
[suppressColumnVirtualisation]="true" flag

How can I display images on buttons that are attached to TilePane

-JavaFX
I created an array of buttons and attached them to tilePane. Now, I want to display an image on each button. I need some sort of an array of images that has the same size as the array buttons so that I can allocate an image for each button and display it. Meaning I need every button to display its own image.
I am coding in JavaFX. I'm struggling to create an array of images so that they can be displayed on the buttons. Please note that I want to display images on each button #not display images when I click the button.
public class Main extends Application {
private static Image[] deck = new Image[35];
private static ImageView[] imageView = new ImageView[35];
#Override
public void start(Stage LoginWindow) {
Button[] btn = new Button[35];
Controller control = new Controller();
for (int s = 0; s < 35; s++) {
btn[s] = new Button();
btn[s].setPrefSize(262, 200);
btn[s].setOnAction(e -> control.display());
btn[s].setStyle("-fx-border-color: #10fffa; -fx-border-width: 5px;");
}
TilePane pane = new TilePane();
pane.setPadding(new Insets(20, 10, 20, 10));
pane.setHgap(10);
pane.setVgap(10);
pane.setAlignment(Pos.CENTER);
pane.setMaxSize(500, 700);
pane.getChildren().addAll(btn);
Scene scene = new Scene(pane, 500, 700);
LoginWindow.setFullScreen(true);
LoginWindow.setScene(scene);
LoginWindow.show();
}
public void add() {
for (int i = 0; i < deck.length; i++) {
deck[i] = new Image(i + ".png");
imageView[i].setImage(deck[i]);
}
}
}
When I run the code it should display images on every button. Images can differ. No action Listeners. Just buttons with images on a TilePane.

Single and Double Click on Button in Android?

I want to create Single and Double click on Button in Android...
Thanks for help in Advance.
I have already tried using button.setOnClickListener() for single click on button but i couldn't find double click on button
Try this code : (btn is the button you want to check for single and double click)
int i = 0;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
i++;
Handler handler = new Handler();
Runnable r = new Runnable() {
#Override
public void run() {
i = 0;
}
};
if (i == 1) {
//Single click
handler.postDelayed(r, 250);
} else if (i == 2) {
//Double click
i = 0;
ShowDailog();
}
}
});

Hide the Title bar while scrolling and Show the TItle bar after Scrolling?

I want to hide the title bar when i scrolling the items in the ListView and i want to show the title bar after scrolling. Suggest any ideas to solve this issue.
First add the Xml View into ActionBar like this:
LayoutInflater inflater = (LayoutInflater) getActionBar()
.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customActionBarView = inflater.inflate(R.layout.main, null);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
actionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(R.layout.main);
Then do the changes in onScrollStateChanged() method:
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case SCROLL_STATE_IDLE:
actionBar.show();
break;
case SCROLL_STATE_TOUCH_SCROLL:
actionBar.hide();
break;
}
}
//declare this two globally
public static int ch = 0, cht = 1;
int myLastVisiblePos;
//Then add onScrollListener to your ListView
list.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int currentFirstVisPos = view.getFirstVisiblePosition();
if (currentFirstVisPos > myLastVisiblePos) {
if (ch == 1) {
ch++;
cht = 1;
getActionBar().hide();
} else if (ch == 0) {
getActionBar().show();
ch++;
}
}
if (currentFirstVisPos < myLastVisiblePos)
if (cht == 1)
getActionBar().show();
myLastVisiblePos = currentFirstVisPos;
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
});
This solution worked for me very good:
// mLastFirstVisibleItem defined globally
quranTextList.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
/**
* Hide actionbar when scroll down
*/
if (mLastFirstVisibleItem < firstVisibleItem)
if (getSupportActionBar().isShowing())
getSupportActionBar().hide();
if (mLastFirstVisibleItem > firstVisibleItem)
if (!getSupportActionBar().isShowing())
getSupportActionBar().show();
mLastFirstVisibleItem = firstVisibleItem;
}
});
Source: Android ActionBar hide/show when scrolling list view