Friday, August 24, 2018

Setting TextView in Another Class

I have researched many problems like mine on the internet and none seem to be doing what I'm trying to do. What I am trying to do is get a textview that is currently blank in my high_risk.xml file, and make it have text based on if a button is clicked in another class. Here is what I have so far...

Question13Activity(if the yes button is clicked I want to be able to set the text in the HighRisk Activity)

    yes = (Button) findViewById(R.id.finalYes);
    yes.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent intent = getIntent();
            String text = intent.getStringExtra("New Text");
            t = (TextView) findViewById(R.id.abusedOrNah);
            t.setText(text);
            Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
            startActivity(myIntent);
        }
    });

This is how I have the static variable t defined in my highrisk activity class...

HighRiskActivity(this is where I want the text to be set and displayed)

public static TextView t;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.high_risk);

    Intent myIntent = new Intent(HighRiskActivity.this, Question12Activity.class);
    myIntent.putExtra("Text", "New Text");
    startActivity(myIntent);

Every time I try and access the contents of t in another class and change it's text, it always returns null. Any way I can fix this from happening? Any help would be greatly appreciated :)

Solved

You can use Bounds, to get the the data to the new Activity and set the text from there:

Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("", "");
startActivity(myIntent);

And in the Question13Activity.class:

Intent intent = getIntent();
String text= intent.getStringExtra("");

t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);

UPDATE:

Use it so:

HighRiskActivity.class:

 yes = (Button) findViewById(R.id.finalYes);
    yes.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
            myIntent.putExtra("TestKey", "My new Text");
            startActivity(myIntent);
        }
    });

Question13Activity.class:

TextView t;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.high_risk);
Intent intent = getIntent();
String text= intent.getStringExtra("TestKey");

t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);

In order to setText() from another class (for example, from your Custom class), you need to specify the activity on which you'd like to do that.

Unless a class does have a reference of your context, you cannot call 'findViewById()'

To do that, you use Context as a parameter for custom class constructor.

Just after you have set the context will you be able to call

TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
        txtView.setText("foo");
}

Monday, August 20, 2018

Strange tibble behavior?

This seems like strange tibble behavior to me. I want to output the sum of a subset of every row in a tibble.

I can't figure out why this works...

tbl <- tibble(x=c(1,2,3),y=c(9,10,11))  

for (i in 1:nrow(tbl)){
   print(sum(tbl[i, c('x','y')]))
}

[1] 10
[1] 12
[1] 14

But this doesn't work.

fun <- function(row){
    return (sum(row[c('x','y')]))
}
apply(tbl, 1, fun)


Error in sum(record[c("x", "y")]) : 
   invalid 'type' (character) of argument

As it turns out my numbers in tbl are being converted into chars.

fun2 <- function(row){
    print (row[c('x','y')])
}
apply(tbl,1,fun2)

   x    y 
 "1" " 9" 
   x    y 
 "2" "10" 
   x    y 
 "3" "11" 
  [,1] [,2] [,3]
x "1"  "2"  "3" 
y " 9" "10" "11"

Why does are these values being converted into strings?

Sunday, August 19, 2018

Sending multiple objects forward changes their order (z-index)

The following snippet has a green square above a red square

  1. Select both squares by dragging over them.
  2. Click the bring forward button

After clicking bring forward the squares have switched order. It is my understanding that the items should stay in the same order, but be moved increasingly above other non-selected items as the button is further clicked.

If you deselect, and repeat the experiment you will see that they switch again.

Any ideas?

var canvas = new fabric.Canvas('c', 
{
  preserveObjectStacking : true
});

var rect = new fabric.Rect({
  left: 10, top: 10,
  fill: 'red',
  width: 100, height: 100,
  hasControls: true
});

canvas.add(rect);

var rect2 = new fabric.Rect({
  left: 40, top: 40,
  fill: 'green',
  width: 100, height: 100,
  hasControls: true
});

canvas.add(rect2);

$("#bringForward").click(function()
{
 var items = canvas.getActiveObject() || canvas.getActiveGroup();

 if(items)
  items.bringForward();
});




Solved

This can be considered a bug or not, depending on what do you expect the function to do.

The documentation for the feature says: Moves an object or a selection up in stack of drawn objects And is actually doing so. The object on top cannot go more on top, the one under can and goes.

Still for a dev this can look like a weird behaviour, to me not really. But guess is personal.

Here is your widget with a modified snippet to try a better solution.

var removeFromArray = fabric.util.removeFromArray;

// modified function to avoid snapping
fabric.StaticCanvas.prototype.bringForward = function (object, intersecting) {
      if (!object) {
        return this;
      }
      var activeGroup = this._activeGroup,
          i, obj, idx, newIdx, objs, latestIndex;

      if (object === activeGroup) {
        objs = activeGroup._objects;
        latestIndex = this._objects.length;
        for (i = objs.length; i--;) {
          obj = objs[i];
          idx = this._objects.indexOf(obj);
          if (idx !== this._objects.length - 1 && idx < latestIndex - 1) {
            newIdx = idx + 1;
            latestIndex = newIdx;
            removeFromArray(this._objects, obj);
            this._objects.splice(newIdx, 0, obj);
          } else {
            latestIndex = idx;
          }
        }
      }
      else {
        idx = this._objects.indexOf(object);
        if (idx !== this._objects.length - 1) {
          // if object is not on top of stack (last item in an array)
          newIdx = this._findNewUpperIndex(object, idx, intersecting);
          removeFromArray(this._objects, object);
          this._objects.splice(newIdx, 0, object);
        }
      }
      this.renderAll && this.renderAll();
      return this;
    };


var canvas = new fabric.Canvas('c', 
{
  preserveObjectStacking : true
});

var rect = new fabric.Rect({
  left: 10, top: 10,
  fill: 'red',
  width: 100, height: 100,
  hasControls: true
});

canvas.add(rect);

var rect2 = new fabric.Rect({
  left: 40, top: 40,
  fill: 'green',
  width: 100, height: 100,
  hasControls: true
});

canvas.add(rect2);

var rect3 = new fabric.Rect({
  left: 70, top: 70,
  fill: 'blue',
  width: 100, height: 100,
  hasControls: true
});

canvas.add(rect3);

var rect4 = new fabric.Rect({
  left: 100, top: 100,
  fill: 'orange',
  width: 100, height: 100,
  hasControls: true
});

canvas.add(rect4);

$("#bringForward").click(function()
{
 var items = canvas.getActiveObject() || canvas.getActiveGroup();

 if(items)
  items.bringForward();
});





Saturday, August 18, 2018

How to set background color kendo angualr2 tabstrip Title

I am using Kendo-Tabstrip in my Angular 2 app, which works fine.

I now wish to set a background color for the tab-headers only, not the tab content (body) as well.

How can this be achieved?

Solved

You can do it using CSS:

.k-tabstrip-items {
    background: red;
}

.k-tabstrip-items .k-item {
    background: green;
}

k-tabstrip-items is the strip itself and k-item are the different tabs.